Home | History | Annotate | Download | only in category
      1 /*******************************************************************************
      2  * Copyright (c) 2011 Google, Inc.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Google, Inc. - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.wb.internal.core.model.property.category;
     12 
     13 import org.eclipse.wb.internal.core.model.property.Property;
     14 import org.eclipse.wb.internal.core.model.property.PropertyManager;
     15 
     16 /**
     17  * Factory for {@link PropertyCategoryProvider} instances.
     18  *
     19  * @author scheglov_ke
     20  * @coverage core.model.property
     21  */
     22 public final class PropertyCategoryProviders {
     23   ////////////////////////////////////////////////////////////////////////////
     24   //
     25   // Simple providers
     26   //
     27   ////////////////////////////////////////////////////////////////////////////
     28   private static final PropertyCategoryProvider FROM_PROPERTY = new PropertyCategoryProvider() {
     29     public PropertyCategory getCategory(Property property) {
     30       return property.getCategory();
     31     }
     32   };
     33 
     34   /**
     35    * Returns result of {@link Property#getCategory()}, never <code>null</code>.
     36    */
     37   public static PropertyCategoryProvider fromProperty() {
     38     return FROM_PROPERTY;
     39   }
     40 
     41   private static final PropertyCategoryProvider FORCED_BY_USER = new PropertyCategoryProvider() {
     42     public PropertyCategory getCategory(Property property) {
     43       return PropertyManager.getCategoryForced(property);
     44     }
     45   };
     46 
     47   /**
     48    * Returns category forced by user, may be <code>null</code>.
     49    */
     50   public static PropertyCategoryProvider forcedByUser() {
     51     return FORCED_BY_USER;
     52   }
     53 
     54   ////////////////////////////////////////////////////////////////////////////
     55   //
     56   // Compound
     57   //
     58   ////////////////////////////////////////////////////////////////////////////
     59   /**
     60    * Returns first not <code>null</code> category returned by provider.
     61    */
     62   public static PropertyCategoryProvider combine(final PropertyCategoryProvider... providers) {
     63     return new PropertyCategoryProvider() {
     64       public PropertyCategory getCategory(Property property) {
     65         for (PropertyCategoryProvider provider : providers) {
     66           PropertyCategory category = provider.getCategory(property);
     67           if (category != null) {
     68             return category;
     69           }
     70         }
     71         throw new IllegalStateException("Can not provide category for " + property.getTitle());
     72       }
     73     };
     74   }
     75 
     76   private static final PropertyCategoryProvider DEF = combine(forcedByUser(), fromProperty());
     77 
     78   /**
     79    * Returns the default combination of {@link PropertyCategoryProvider}s - first
     80    * {@link #forcedByUser()}, then {@link #fromProperty()}.
     81    */
     82   public static PropertyCategoryProvider def() {
     83     return DEF;
     84   }
     85 }
     86