    001/*
    002 * Copyright (c) 2007-2016 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
    021package cascading.pipe.assembly;
    022
    023import java.util.Properties;
    024
    025import cascading.property.Props;
    026import cascading.util.cache.BaseCacheFactory;
    027import cascading.util.cache.LRUHashMapCacheFactory;
    028
    029/**
    030 * Class AggregateByProps is a fluent helper for setting various properties related to the cache used in {@link AggregateBy}.
    031 */
    032public class AggregateByProps extends Props
    033  {
    034
    035  /** property to control the cache factory used in AggregateBy. */
    036  public static String AGGREGATE_BY_CACHE_FACTORY = "cascading.aggregateby.cachefactory.classname";
    037
    038  /** property to control the capacity of the cache to use. */
    039  public static final String AGGREGATE_BY_CAPACITY = "cascading.aggregateby.cache.capacity";
    040
    041  /** default capacity of caches used in AggregateBy. */
    042  public static int AGGREGATE_BY_DEFAULT_CAPACITY = BaseCacheFactory.DEFAULT_CAPACITY;
    043
    044  /** default factory class for creating caches. */
    045  public static final Class<? extends BaseCacheFactory> DEFAULT_CACHE_FACTORY_CLASS = LRUHashMapCacheFactory.class;
    046
    047  private Properties properties;
    048
    049  public static AggregateByProps aggregateByProps()
    050    {
    051    return new AggregateByProps();
    052    }
    053
    054  public AggregateByProps()
    055    {
    056    this.properties = new Properties();
    057    }
    058
    059  /**
    060   * Sets the CacheFactory class to use.
    061   *
    062   * @param cacheFactory The cache factory class to use.
    063   */
    064  public AggregateByProps setCacheFactoryClass( Class<? extends BaseCacheFactory> cacheFactory )
    065    {
    066    return setCacheFactoryClassName( cacheFactory.getName() );
    067    }
    068
    069  /**
    070   * Sets the name of the CacheFactory class to use.
    071   *
    072   * @param cacheFactoryClassName The full name of the cache factory class to use.
    073   */
    074  public AggregateByProps setCacheFactoryClassName( String cacheFactoryClassName )
    075    {
    076    properties.setProperty( AGGREGATE_BY_CACHE_FACTORY, cacheFactoryClassName );
    077    return this;
    078    }
    079
    080  /**
    081   * Sets the capacity of the cache.
    082   *
    083   * @param capacity The capacity of the cache.
    084   */
    085  public AggregateByProps setCapacity( int capacity )
    086    {
    087    properties.setProperty( AGGREGATE_BY_CAPACITY, String.valueOf( capacity ) );
    088    return this;
    089    }
    090
    091  /**
    092   * Returns the capacity.
    093   *
    094   * @return The capacity.
    095   */
    096  public int getCapacity()
    097    {
    098    String capacityValue = properties.getProperty( AGGREGATE_BY_CAPACITY );
    099
    100    if( capacityValue == null )
    101      return BaseCacheFactory.DEFAULT_CAPACITY;
    102
    103    return Integer.valueOf( capacityValue );
    104    }
    105
    106  /**
    107   * Returns the name of the cache factory.
    108   *
    109   * @return The cache class name.
    110   */
    111  public String getCacheFactoryClassName()
    112    {
    113    String className = properties.getProperty( AGGREGATE_BY_CACHE_FACTORY );
    114
    115    if( className == null )
    116      return DEFAULT_CACHE_FACTORY_CLASS.getName();
    117
    118    return className;
    119    }
    120
    121  @Override
    122  protected void addPropertiesTo( Properties properties )
    123    {
    124    properties.putAll( this.properties );
    125    }
    126  }