All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.cache.configuration.MutableConfiguration Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/**
 *  Copyright 2011 Terracotta, Inc.
 *  Copyright 2011 Oracle America Incorporated
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package javax.cache.configuration;

import javax.cache.expiry.EternalExpiryPolicy;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;
import javax.cache.transaction.IsolationLevel;
import javax.cache.transaction.Mode;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple mutable implementation of a {@link Configuration}.
 *
 * @param  the type of keys maintained the cache
 * @param  the type of cached values
 * @author Brian Oliver
 * @author Greg Luck
 * @since 1.0
 */
public class MutableConfiguration implements Configuration {

  /**
   * The serialVersionUID required for {@link java.io.Serializable}.
   */
  public static final long serialVersionUID = 201306200821L;

  /**
   * The type of keys for {@link javax.cache.Cache}s configured with this
   * {@link Configuration}.
   */
  protected Class keyType;

  /**
   * The type of values for {@link javax.cache.Cache}s configured with this
   * {@link Configuration}.
   */
  protected Class valueType;

  /**
   * The {@link CacheEntryListenerConfiguration}s for the {@link Configuration}.
   */
  protected ArrayList> listenerConfigurations;

  /**
   * The {@link Factory} for the {@link javax.cache.integration.CacheLoader}.
   */
  protected Factory> cacheLoaderFactory;

  /**
   * The {@link Factory} for the {@link javax.cache.integration.CacheWriter}.
   */
  protected Factory> cacheWriterFactory;

  /**
   * The {@link Factory} for the {@link javax.cache.expiry.ExpiryPolicy}.
   */
  protected Factory> expiryPolicyFactory;

  /**
   * A flag indicating if "read-through" mode is required.
   */
  protected boolean isReadThrough;

  /**
   * A flag indicating if "write-through" mode is required.
   */
  protected boolean isWriteThrough;

  /**
   * A flag indicating if statistics gathering is enabled.
   */
  protected boolean isStatisticsEnabled;

  /**
   * A flag indicating if the cache will be store-by-value or store-by-reference.
   */
  protected boolean isStoreByValue;

  /**
   * A flag indicating if the cache will use transactions.
   */
  protected boolean isTransactionsEnabled;

  /**
   * The transaction {@link IsolationLevel}.
   */
  protected IsolationLevel txnIsolationLevel;

  /**
   * The transaction {@link Mode}.
   */
  protected Mode txnMode;

  /**
   * Whether management is enabled
   */
  protected boolean isManagementEnabled;

  /**
   * Constructs a default {@link MutableConfiguration}.
   */
  public MutableConfiguration() {
    this.keyType = null;
    this.valueType = null;
    this.listenerConfigurations = new
        ArrayList>();
    this.cacheLoaderFactory = null;
    this.cacheWriterFactory = null;
    this.expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
    this.isReadThrough = false;
    this.isWriteThrough = false;
    this.isStatisticsEnabled = false;
    this.isStoreByValue = true;
    this.isManagementEnabled = false;
    this.isTransactionsEnabled = false;
    this.txnIsolationLevel = IsolationLevel.NONE;
    this.txnMode = Mode.NONE;
  }

  /**
   * Constructs a {@link MutableConfiguration} based on another
   * {@link Configuration}.
   *
   * @param configuration the {@link Configuration}
   */
  public MutableConfiguration(Configuration configuration) {

    this.keyType = configuration.getKeyType();
    this.valueType = configuration.getValueType();

    listenerConfigurations = new
        ArrayList>();
    for (CacheEntryListenerConfiguration definition : configuration
        .getCacheEntryListenerConfigurations()) {
      addCacheEntryListenerConfiguration(definition);
    }

    this.cacheLoaderFactory = configuration.getCacheLoaderFactory();
    this.cacheWriterFactory = configuration.getCacheWriterFactory();

    if (configuration.getExpiryPolicyFactory() == null) {
      this.expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
    } else {
      this.expiryPolicyFactory = configuration.getExpiryPolicyFactory();
    }

    this.isReadThrough = configuration.isReadThrough();
    this.isWriteThrough = configuration.isWriteThrough();

    this.isStatisticsEnabled = configuration.isStatisticsEnabled();

    this.isStoreByValue = configuration.isStoreByValue();

    this.isManagementEnabled = configuration.isManagementEnabled();

    this.isTransactionsEnabled = configuration.isTransactionsEnabled();
    this.txnIsolationLevel = configuration.getTransactionIsolationLevel();
    this.txnMode = configuration.getTransactionMode();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Class getKeyType() {
    return keyType;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Class getValueType() {
    return valueType;
  }

  /**
   * Sets the expected type of keys and values for a {@link javax.cache.Cache}
   * configured with this {@link Configuration}. Setting both to null
   * means type-safety checks are not required.
   *
   * @param keyType   the expected key type
   * @param valueType the expected value type
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   */
  public MutableConfiguration setTypes(Class keyType, Class valueType) {
    if ((keyType == null && valueType == null) ||
        (keyType != null && valueType != null)) {
      this.keyType = keyType;
      this.valueType = valueType;
      return this;
    } else {
      throw new IllegalArgumentException("Both keyType and valueType must be null or a type");
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List> getCacheEntryListenerConfigurations() {
    return listenerConfigurations;
  }

  /**
   * Add a configuration for a {@link javax.cache.event.CacheEntryListener}.
   *
   * @param cacheEntryListenerConfiguration the
   *  {@link CacheEntryListenerConfiguration}
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   * @throws IllegalArgumentException is the same CacheEntryListenerConfiguration
   * is used more than once
   */
  public MutableConfiguration addCacheEntryListenerConfiguration(
      CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {

    if (cacheEntryListenerConfiguration == null) {
      throw new NullPointerException("CacheEntryListenerConfiguration can't be null");
    }

    boolean alreadyExists = false;
    for (CacheEntryListenerConfiguration c : listenerConfigurations) {
      if (c.equals(cacheEntryListenerConfiguration)) {
        alreadyExists = true;
      }
    }

    if (!alreadyExists) {
      this.listenerConfigurations.add(cacheEntryListenerConfiguration);
    } else {
      throw new IllegalArgumentException("A CacheEntryListenerConfiguration can " +
          "be registered only once");
    }
    return this;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Factory> getCacheLoaderFactory() {
    return this.cacheLoaderFactory;
  }

  /**
   * Set the {@link CacheLoader} factory.
   *
   * @param factory the {@link CacheLoader} {@link Factory}
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   */
  public MutableConfiguration setCacheLoaderFactory(Factory> factory) {
    this.cacheLoaderFactory = (Factory>) factory;
    return this;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Factory> getCacheWriterFactory() {
    return this.cacheWriterFactory;
  }

  /**
   * Set the {@link CacheWriter} factory.
   *
   * @param factory the {@link CacheWriter} {@link Factory}
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   */
  public MutableConfiguration setCacheWriterFactory(Factory> factory) {
    this.cacheWriterFactory = (Factory>) factory;
    return this;
  }

  /**
   * {@inheritDoc}
   */
  public Factory> getExpiryPolicyFactory() {
    return this.expiryPolicyFactory;
  }

  /**
   * Set the {@link Factory} for the {@link ExpiryPolicy}.  If null
   * is specified the default {@link ExpiryPolicy} is used.
   *
   * @param factory the {@link ExpiryPolicy} {@link Factory}
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   */
  public MutableConfiguration setExpiryPolicyFactory(Factory> factory) {
    if (factory == null) {
      this.expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
    } else {
      this.expiryPolicyFactory = (Factory>) factory;
    }
    return this;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public IsolationLevel getTransactionIsolationLevel() {
    return this.txnIsolationLevel;
  }

  /**
   * Set the Transaction {@link IsolationLevel} and {@link Mode},
   * which also sets {@link #isTransactionsEnabled()} to true.
   *
   * @param level the {@link IsolationLevel}
   * @param mode  the {@link Mode}
   * @return the {@link MutableConfiguration} to permit fluent-style method calls
   */
  public MutableConfiguration setTransactions(IsolationLevel level,
                                                    Mode mode) {
    this.txnIsolationLevel = level;
    this.txnMode = mode;
    this.isTransactionsEnabled = true;
    return this;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Mode getTransactionMode() {
    return this.txnMode;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isReadThrough() {
    return this.isReadThrough;
  }

  /**
   * Set if read-through caching should be used.
   * 

* It is an invalid configuration to set this to true without specifying a * {@link CacheLoader} {@link Factory}. * * @param isReadThrough true if read-through is required * @return the {@link MutableConfiguration} to permit fluent-style method calls */ public MutableConfiguration setReadThrough(boolean isReadThrough) { this.isReadThrough = isReadThrough; return this; } /** * {@inheritDoc} */ @Override public boolean isWriteThrough() { return this.isWriteThrough; } /** * Set if write-through caching should be used. *

* It is an invalid configuration to set this to true without specifying a * {@link CacheWriter} {@link Factory}. * * @param isWriteThrough true if write-through is required * @return the {@link MutableConfiguration} to permit fluent-style method calls */ public MutableConfiguration setWriteThrough(boolean isWriteThrough) { this.isWriteThrough = isWriteThrough; return this; } /** * {@inheritDoc} */ @Override public boolean isStoreByValue() { return this.isStoreByValue; } /** * Set if a configured cache should use store-by-value or store-by-reference * semantics. * * @param isStoreByValue true if store-by-value is required, * false for store-by-reference * @return the {@link MutableConfiguration} to permit fluent-style method calls */ public MutableConfiguration setStoreByValue(boolean isStoreByValue) { this.isStoreByValue = isStoreByValue; return this; } /** * {@inheritDoc} */ @Override public boolean isStatisticsEnabled() { return this.isStatisticsEnabled; } /** * Sets whether statistics gathering is enabled on a cache. *

* Statistics may be enabled or disabled at runtime via * {@link javax.cache.CacheManager#enableStatistics(String, boolean)}. * * @param enabled true to enable statistics, false to disable. * @return the {@link MutableConfiguration} to permit fluent-style method calls */ public MutableConfiguration setStatisticsEnabled(boolean enabled) { this.isStatisticsEnabled = enabled; return this; } /** * {@inheritDoc} */ @Override public boolean isManagementEnabled() { return this.isManagementEnabled; } /** * Sets whether management is enabled on a cache. *

* Management may be enabled or disabled at runtime via * {@link javax.cache.CacheManager#enableManagement(String, boolean)}. * * @param enabled true to enable statistics, false to disable. * @return the {@link MutableConfiguration} to permit fluent-style method calls */ public MutableConfiguration setManagementEnabled(boolean enabled) { this.isManagementEnabled = enabled; return this; } /** * {@inheritDoc} */ @Override public boolean isTransactionsEnabled() { return this.isTransactionsEnabled; } /** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((keyType == null) ? 0 : keyType.hashCode()); result = prime * result + ((valueType == null) ? 0 : valueType.hashCode()); result = prime * result + ((listenerConfigurations == null) ? 0 : listenerConfigurations .hashCode()); result = prime * result + ((cacheLoaderFactory == null) ? 0 : cacheLoaderFactory.hashCode()); result = prime * result + ((cacheWriterFactory == null) ? 0 : cacheWriterFactory.hashCode()); result = prime * result + ((expiryPolicyFactory == null) ? 0 : expiryPolicyFactory.hashCode()); result = prime * result + (isReadThrough ? 1231 : 1237); result = prime * result + (isStatisticsEnabled ? 1231 : 1237); result = prime * result + (isStoreByValue ? 1231 : 1237); result = prime * result + (isWriteThrough ? 1231 : 1237); result = prime * result + ((txnIsolationLevel == null) ? 0 : txnIsolationLevel .hashCode()); result = prime * result + ((txnMode == null) ? 0 : txnMode.hashCode()); return result; } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (this == object) { return true; } if (object == null) { return false; } if (!(object instanceof MutableConfiguration)) { return false; } MutableConfiguration other = (MutableConfiguration) object; if ((keyType == null && other.keyType != null) || (keyType != null && other.keyType == null)) { return false; } else if (keyType != null && other.keyType != null && !keyType.equals(other.keyType)) { return false; } if ((valueType == null && other.valueType != null) || (valueType != null && other.valueType == null)) { return false; } else if (valueType != null && other.valueType != null && !valueType.equals(other.valueType)) { return false; } if (listenerConfigurations == null) { if (other.listenerConfigurations != null) { return false; } } else if (!listenerConfigurations.equals(other .listenerConfigurations)) { return false; } if (cacheLoaderFactory == null) { if (other.cacheLoaderFactory != null) { return false; } } else if (!cacheLoaderFactory.equals(other.cacheLoaderFactory)) { return false; } if (cacheWriterFactory == null) { if (other.cacheWriterFactory != null) { return false; } } else if (!cacheWriterFactory.equals(other.cacheWriterFactory)) { return false; } if (expiryPolicyFactory == null) { if (other.expiryPolicyFactory != null) { return false; } } else if (!expiryPolicyFactory.equals(other.expiryPolicyFactory)) { return false; } if (isReadThrough != other.isReadThrough) { return false; } if (isStatisticsEnabled != other.isStatisticsEnabled) { return false; } if (isStoreByValue != other.isStoreByValue) { return false; } if (isWriteThrough != other.isWriteThrough) { return false; } if (isTransactionsEnabled != other.isTransactionsEnabled) { return false; } if (txnIsolationLevel != other.txnIsolationLevel) { return false; } if (txnMode != other.txnMode) { return false; } return true; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy