001    /*
002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.property;
022    
023    import java.util.Map;
024    import java.util.Properties;
025    
026    /**
027     * Class Props is the base class for frameworks specific properties helper classes.
028     * <p/>
029     * Use the sub-classes to either create a {@link Properties} instance with custom or default values to be passed
030     * to any sub-system that requires a Map or Properties instance of properties and values.
031     */
032    public abstract class Props
033      {
034      /**
035       * Method buildProperties returns a new {@link Properties} instance with all property values for this type.
036       * <p/>
037       * If no values have been set, all default properties and values will be returned.
038       *
039       * @return a new Properties instance
040       */
041      public Properties buildProperties()
042        {
043        return buildProperties( (Properties) null );
044        }
045    
046      /**
047       * Method buildProperties returns a new {@link Properties} instance with all property values for this type
048       * using the given Map of property values as defaults. The given Map will not be modified.
049       * <p/>
050       * If no values have been set, all default properties and values will be returned.
051       *
052       * @return a new Properties instance
053       */
054      public Properties buildProperties( Map<Object, Object> defaultProperties )
055        {
056        return buildProperties( PropertyUtil.createProperties( defaultProperties, null ) );
057        }
058    
059      /**
060       * Method buildProperties returns a new {@link Properties} instance with all property values for this type
061       * using the given Iterable<Map.Entry<String, String>> of property values as defaults. The given Iterable will not be modified.
062       * <p/>
063       * If no values have been set, all default properties and values will be returned.
064       *
065       * @return a new Properties instance
066       */
067      public Properties buildProperties( Iterable<Map.Entry<String, String>> defaultProperties )
068        {
069        return buildProperties( PropertyUtil.createProperties( defaultProperties ) );
070        }
071    
072      /**
073       * Method buildProperties returns a new {@link Properties} instance with all property values for this type
074       * using the given Properties instance of property values as defaults. The given Map will not be modified.
075       * <p/>
076       * If no values have been set, all default properties and values will be returned.
077       *
078       * @return a new Properties instance
079       */
080      public Properties buildProperties( Properties defaultProperties )
081        {
082        defaultProperties = defaultProperties != null ? new Properties( defaultProperties ) : new Properties();
083    
084        addPropertiesTo( defaultProperties );
085    
086        return defaultProperties;
087        }
088    
089      public ConfigDef setProperties( ConfigDef configDef )
090        {
091        return setProperties( configDef, ConfigDef.Mode.REPLACE );
092        }
093    
094      public ConfigDef setProperties( ConfigDef configDef, ConfigDef.Mode mode )
095        {
096        Properties properties = buildProperties();
097    
098        for( String name : properties.stringPropertyNames() )
099          configDef.setProperty( mode, name, properties.getProperty( name ) );
100    
101        return configDef;
102        }
103    
104      protected abstract void addPropertiesTo( Properties properties );
105      }