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