View Javadoc
1   package org.apache.commons.jcs3.engine;
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.behavior.ICacheElement;
25  import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
26  
27  /**
28   * Generic element wrapper. Often stuffed inside another.
29   */
30  public class CacheElement<K, V>
31      implements ICacheElement<K, V>
32  {
33      /** Don't change */
34      private static final long serialVersionUID = -6062305728297627263L;
35  
36      /** The name of the cache region. This is a namespace. */
37      private final String cacheName;
38  
39      /** This is the cache key by which the value can be referenced. */
40      private final K key;
41  
42      /** This is the cached value, reference by the key. */
43      private final V val;
44  
45      /**
46       * These attributes hold information about the element and what it is
47       * allowed to do.
48       */
49      private IElementAttributes attr;
50  
51      /**
52       * Constructor for the CacheElement object
53       * <p>
54       * @param cacheName
55       * @param key
56       * @param val
57       */
58      public CacheElement( final String cacheName, final K key, final V val )
59      {
60          this.cacheName = cacheName;
61          this.key = key;
62          this.val = val;
63      }
64  
65      /**
66       * Constructor for the CacheElement object
67       * <p>
68       * @param cacheName
69       * @param key
70       * @param val
71       * @param attrArg
72       */
73      public CacheElement( final String cacheName, final K key, final V val, final IElementAttributes attrArg )
74      {
75          this(cacheName, key, val);
76          this.attr = attrArg;
77      }
78  
79      /**
80       * Gets the cacheName attribute of the CacheElement object
81       * <p>
82       * @return The cacheName value
83       */
84      @Override
85      public String getCacheName()
86      {
87          return this.cacheName;
88      }
89  
90      /**
91       * Gets the key attribute of the CacheElement object
92       * <p>
93       * @return The key value
94       */
95      @Override
96      public K getKey()
97      {
98          return this.key;
99      }
100 
101     /**
102      * Gets the val attribute of the CacheElement object
103      * <p>
104      * @return The val value
105      */
106     @Override
107     public V getVal()
108     {
109         return this.val;
110     }
111 
112     /**
113      * Sets the attributes attribute of the CacheElement object
114      * <p>
115      * @param attr
116      *            The new IElementAttributes value
117      */
118     @Override
119     public void setElementAttributes( final IElementAttributes attr )
120     {
121         this.attr = attr;
122     }
123 
124     /**
125      * Gets the IElementAttributes attribute of the CacheElement object
126      * <p>
127      * @return The IElementAttributes value, never null
128      */
129     @Override
130     public IElementAttributes getElementAttributes()
131     {
132         // create default attributes if they are null
133         // this shouldn't happen, but could if a corrupt
134         // object was sent over the wire.
135         if ( this.attr == null )
136         {
137             this.attr = new ElementAttributes();
138         }
139         return this.attr;
140     }
141 
142     /**
143      * @param obj other object
144      * @return true if this object key equals the key of obj
145      */
146     @Override
147     public boolean equals(final Object obj)
148     {
149         if (this == obj)
150         {
151             return true;
152         }
153         if (!(obj instanceof CacheElement))
154         {
155             return false;
156         }
157         final CacheElement<?,?> other = (CacheElement<?,?>) obj;
158         return Objects.equals(key, other.key);
159     }
160 
161     /**
162      * @return a hash of the key only
163      */
164     @Override
165     public int hashCode()
166     {
167         return key.hashCode();
168     }
169 
170     /**
171      * For debugging only.
172      * <p>
173      * @return String representation
174      */
175     @Override
176     public String toString()
177     {
178         return "[CacheElement: cacheName [" + cacheName + "], key [" + key + "], val [" + val + "], attr [" + attr
179             + "]";
180     }
181 }