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

com.gemstone.gemfire.cache.ExpirationAttributes Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.cache;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import com.gemstone.gemfire.DataSerializable;
import com.gemstone.gemfire.DataSerializer;
import com.gemstone.gemfire.internal.InternalDataSerializer;

/** Immutable parameter object for accessing and setting the attributes associated with 
 * timeToLive and idleTimeout. If the expiration
 * action is not specified, it defaults to ExpirationAction.INVALIDATE.
 * If the timeout is not specified, it defaults to zero (which means to never timeout).
 *
 * @author Eric Zoerner
 *
 *
 * @see AttributesFactory
 * @see RegionAttributes
 * @see AttributesMutator
 * @since 3.0
 */
public class ExpirationAttributes implements DataSerializable { 
  private static final long serialVersionUID = 5956885652945706394L;
  /** convenience constant for a default instance */
  public static final ExpirationAttributes DEFAULT = new ExpirationAttributes();
  
  /** The number of seconds since this value or region was created before it expires. */  
  private  int timeout;
  
  /** The action that should take place when this object or region expires.
   */  
  private  ExpirationAction action;

  /** Constructs a default ExpirationAttributes, which indicates no expiration
   * will take place.
   */
  public ExpirationAttributes() {
    this.timeout = 0;
    this.action = ExpirationAction.INVALIDATE;
  }
  
  /** Constructs an ExpirationAttributes with the specified expiration time
   * and the default expiration action ExpirationAction.INVALIDATE.
   * @param expirationTime The number of seconds before expiration
   * @throws IllegalArgumentException if expirationTime is nonpositive
   */
  public ExpirationAttributes(int expirationTime) {
    this.timeout = expirationTime;
    this.action = ExpirationAction.INVALIDATE;
  }
  
  /** Constructs an ExpirationAttributes with the specified expiration time and
   * expiration action.
   * @param expirationTime The number of seconds for a value to live before it expires
   * @param expirationAction the action to take when the value expires
   * @throws IllegalArgumentException if expirationTime is nonpositive
   */
  public ExpirationAttributes(int expirationTime, ExpirationAction expirationAction) {
    this.timeout = expirationTime;
    this.action = expirationAction;
  }
  

  /** Returns the number of seconds before a region or value expires.
   *
   * @return the relative number of seconds before a region or value expires
   * or zero if it will never expire
   */  
  public int getTimeout() {
    return this.timeout;
  }
  
  /** Returns the action that should take place when this value or region expires.
   * 
   * @return the action to take when expiring
   */  
  public ExpirationAction getAction() {
    return this.action;
  }
  
  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof ExpirationAttributes)) {
      return false;
    }
    ExpirationAttributes ea = (ExpirationAttributes)obj;
    return this.timeout == ea.timeout && this.action == ea.action;
  }
  
  @Override
  public int hashCode() {
    return this.timeout ^ this.action.hashCode();
  }
  
  /** Returns a string representation of this ExpirationAttributes. If the timeout
   * is zero, returns "NO EXPIRATION".
   * @return the String representation of this expiration attribute
   */
  @Override
  public String toString() {
    if (this.timeout == 0) {
      return "NO EXPIRATION";
    }
    return "timeout: " + this.timeout +  ";action: " + this.action;
  }
  
  public static ExpirationAttributes createFromData(DataInput in)
      throws IOException, ClassNotFoundException {
    ExpirationAttributes result = new ExpirationAttributes();
    InternalDataSerializer.invokeFromData(result, in);
    return result;
  }


  public void fromData(DataInput in) throws IOException, ClassNotFoundException {
    this.timeout = in.readInt();
    this.action = (ExpirationAction)DataSerializer.readObject(in);

  }

  public void toData(DataOutput out) throws IOException {
    out.writeInt(this.timeout);
    DataSerializer.writeObject(this.action, out);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy