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