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

org.ehcache.loaderwriter.writebehind.operations.WriteOperation Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * Copyright Terracotta, Inc.
 *
 * 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 org.ehcache.loaderwriter.writebehind.operations;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ehcache.spi.loaderwriter.CacheLoaderWriter;

/**
 * Implements the write operation for write behind
 * 
 * @author Geert Bevin
 * @author Tim wu
 *
 */

public class WriteOperation implements SingleOperation {

  private final K key;
  private final V value;
  private final long creationTime;
  
  /**
   * Create a new write operation for a particular element
   *
   */
  public WriteOperation(K k, V v) {
    this(k, v, System.nanoTime());
  }

  /**
   * Create a new write operation for a particular element and creation time
   *
   */
  public WriteOperation(K k, V v, long creationTime) {
    this.key = k;
    this.value = v;
    this.creationTime = creationTime;
  }

  @Override
  public void performSingleOperation(CacheLoaderWriter cacheWriter) throws Exception {
    cacheWriter.write(key, value);
  }

  @Override
  public BatchOperation createBatchOperation(List> operations) {
    final List> entries = new ArrayList>();
    for (final KeyBasedOperation operation : operations) {
      entries.add(new Map.Entry() {

        @Override
        public K getKey() {
          return ((WriteOperation)operation).key;
        }

        @Override
        public V getValue() {
          return ((WriteOperation)operation).value;
        }

        @Override
        public V setValue(V value) {
          throw new UnsupportedOperationException("Not Supported.");
        }
      });
    }
    return new WriteAllOperation(entries);
  }

  @Override
  public K getKey() {
    return this.key;
  }
  
  public V getValue(){
    return this.value;
  }

  @Override
  public long getCreationTime() {
    return creationTime;
  }

  @Override
  public SingleOperationType getType() {
    return SingleOperationType.WRITE;
  }

  @Override
  public int hashCode() {
    int hash = (int) getCreationTime();
    hash = hash * 31 + getKey().hashCode();
    return hash;
  }

  @Override
  public boolean equals(Object other) {
    if (other instanceof WriteOperation) {
      return getCreationTime() == ((WriteOperation) other).getCreationTime() && getKey().equals(((WriteOperation) other).getKey());
    } else {
      return false;
    }
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy