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