View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Objects;
23  
24  import org.apache.commons.jcs3.engine.CacheElement;
25  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
26  import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
27  
28  /**
29   * Implementation of cache elements in purgatory.
30   *
31   * Elements are stored in purgatory when they are spooled to the auxiliary cache, but have not yet
32   * been written to disk.
33   */
34  public class PurgatoryElement<K, V>
35      extends CacheElement<K, V> // Remove this superclass in next major release
36  //    implements ICacheElement<K, V>
37  {
38      /** Don't change */
39      private static final long serialVersionUID = -8152034342684135628L;
40  
41      /** Is the element ready to be spooled? */
42      private boolean spoolable;
43  
44      /** Wrapped cache Element */
45      private final ICacheElement<K, V> cacheElement;
46  
47      /**
48       * Constructor for the PurgatoryElement&lt;K, V&gt; object
49       *
50       * @param cacheElement CacheElement
51       */
52      public PurgatoryElement( final ICacheElement<K, V> cacheElement )
53      {
54          super(cacheElement.getCacheName(), cacheElement.getKey(), cacheElement.getVal());
55          this.cacheElement = cacheElement;
56      }
57  
58      /**
59       * Gets the spoolable property.
60       *
61       * @return The spoolable value
62       */
63      public boolean isSpoolable()
64      {
65          return spoolable;
66      }
67  
68      /**
69       * Sets the spoolable property.
70       *
71       * @param spoolable The new spoolable value
72       */
73      public void setSpoolable( final boolean spoolable )
74      {
75          this.spoolable = spoolable;
76      }
77  
78      /**
79       * Get the wrapped cache element.
80       *
81       * @return ICacheElement
82       */
83      public ICacheElement<K, V> getCacheElement()
84      {
85          return cacheElement;
86      }
87  
88      // ------------------------------------------------ interface ICacheElement
89  
90      /**
91       * @return cacheElement.getCacheName();
92       * @see ICacheElement#getCacheName
93       */
94      @Override
95      public String getCacheName()
96      {
97          return cacheElement.getCacheName();
98      }
99  
100     /**
101      * @return cacheElement.getKey();
102      * @see ICacheElement#getKey
103      */
104     @Override
105     public K getKey()
106     {
107         return cacheElement.getKey();
108     }
109 
110     /**
111      * @return cacheElement.getVal();
112      * @see ICacheElement#getVal
113      */
114     @Override
115     public V getVal()
116     {
117         return cacheElement.getVal();
118     }
119 
120     /**
121      * @return cacheElement.getElementAttributes();
122      * @see ICacheElement#getElementAttributes
123      */
124     @Override
125     public IElementAttributes getElementAttributes()
126     {
127         return cacheElement.getElementAttributes();
128     }
129 
130     /**
131      * @param attr
132      * @see ICacheElement#setElementAttributes
133      */
134     @Override
135     public void setElementAttributes( final IElementAttributes attr )
136     {
137         cacheElement.setElementAttributes( attr );
138     }
139 
140     /**
141      * @param obj other object
142      * @return true if this object key equals the key of obj
143      */
144     @Override
145     public boolean equals(final Object obj)
146     {
147         if (this == obj)
148         {
149             return true;
150         }
151         if (!(obj instanceof PurgatoryElement))
152         {
153             return false;
154         }
155         final PurgatoryElement<?,?> other = (PurgatoryElement<?,?>) obj;
156         return Objects.equals(getKey(), other.getKey());
157     }
158 
159     /**
160      * @return a hash of the key only
161      */
162     @Override
163     public int hashCode()
164     {
165         return getKey().hashCode();
166     }
167 
168     /**
169      * @return debug string
170      */
171     @Override
172     public String toString()
173     {
174         final StringBuilder buf = new StringBuilder();
175         buf.append( "[PurgatoryElement: " );
176         buf.append( " isSpoolable = " + isSpoolable() );
177         buf.append( " CacheElement = " + getCacheElement() );
178         buf.append( " CacheName = " + getCacheName() );
179         buf.append( " Key = " + getKey() );
180         buf.append( " Value = " + getVal() );
181         buf.append( " ElementAttributes = " + getElementAttributes() );
182         buf.append( "]" );
183         return buf.toString();
184     }
185 }