View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.writer.writebehind.operations;
17  
18  import net.sf.ehcache.Element;
19  import net.sf.ehcache.writer.CacheWriter;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /***
25   * Implements the write operation for write behind
26   *
27   * @author Geert Bevin
28   * @version $Id: WriteOperation.html 13146 2011-08-01 17:12:39Z oletizi $
29   */
30  public class WriteOperation implements SingleOperation {
31      private final Element element;
32      private final long creationTime;
33  
34      /***
35       * Create a new write operation for a particular element
36       *
37       * @param element the element to write
38       */
39      public WriteOperation(Element element) {
40          this(element, System.currentTimeMillis());
41      }
42  
43      /***
44       * Create a new write operation for a particular element and creation time
45       *
46       * @param element      the element to write
47       * @param creationTime the creation time of the operation
48       */
49      public WriteOperation(Element element, long creationTime) {
50          this.element = new Element(element.getObjectKey(), element.getObjectValue(), element.getVersion(),
51                  element.getCreationTime(), element.getLastAccessTime(), element.getHitCount(), false,
52                  element.getTimeToLive(), element.getTimeToIdle(), element.getLastUpdateTime());
53          this.creationTime = creationTime;
54      }
55  
56      /***
57       * {@inheritDoc}
58       */
59      public void performSingleOperation(CacheWriter cacheWriter) {
60          cacheWriter.write(element);
61      }
62  
63      /***
64       * {@inheritDoc}
65       */
66      public BatchOperation createBatchOperation(List<SingleOperation> operations) {
67          final List<Element> elements = new ArrayList<Element>();
68          for (KeyBasedOperation operation : operations) {
69              elements.add(((WriteOperation) operation).element);
70          }
71          return new WriteAllOperation(elements);
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public Object getKey() {
78          return element.getObjectKey();
79      }
80  
81      /***
82       * {@inheritDoc}
83       */
84      public long getCreationTime() {
85          return creationTime;
86      }
87  
88      /***
89       * Retrieves the element that will be used for this operation
90       */
91      public Element getElement() {
92          return element;
93      }
94  
95      /***
96       * {@inheritDoc}
97       */
98      public SingleOperationType getType() {
99          return SingleOperationType.WRITE;
100     }
101 }