1 package org.apache.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 java.io.Serializable;
23
24 import org.apache.jcs.engine.behavior.ICacheElement;
25 import org.apache.jcs.engine.behavior.IElementAttributes;
26
27 /**
28 * Generic element wrapper. Often stuffed inside another.
29 */
30 public class CacheElement
31 implements ICacheElement, Serializable
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 public final String cacheName;
38
39 /** This is the cache key by which the value can be referenced. */
40 public final Serializable key;
41
42 /** This is the cached value, reference by the key. */
43 public final Serializable val;
44
45 /**
46 * These attributes hold information about the element and what it is
47 * allowed to do.
48 */
49 public 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( String cacheName, Serializable key, Serializable 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( String cacheName, Serializable key, Serializable val, IElementAttributes attrArg )
74 {
75 this.cacheName = cacheName;
76 this.key = key;
77 this.val = val;
78 this.attr = attrArg;
79 }
80
81 /**
82 * Constructor for the CacheElement object
83 * <p>
84 * @param cacheName
85 * @param key
86 * @param val
87 */
88 public CacheElement( String cacheName, Serializable key, Object val )
89 {
90 this( cacheName, key, (Serializable) val );
91 }
92
93 /**
94 * Gets the cacheName attribute of the CacheElement object
95 * <p>
96 * @return The cacheName value
97 */
98 public String getCacheName()
99 {
100 return this.cacheName;
101 }
102
103 /**
104 * Gets the key attribute of the CacheElement object
105 * <p>
106 * @return The key value
107 */
108 public Serializable getKey()
109 {
110 return this.key;
111 }
112
113 /**
114 * Gets the val attribute of the CacheElement object
115 * <p>
116 * @return The val value
117 */
118 public Serializable getVal()
119 {
120 return this.val;
121 }
122
123 /**
124 * Sets the attributes attribute of the CacheElement object
125 * <p>
126 * @param attr
127 * The new IElementAttributes value
128 */
129 public void setElementAttributes( IElementAttributes attr )
130 {
131 this.attr = (ElementAttributes) attr;
132 }
133
134 /**
135 * Gets the IElementAttributes attribute of the CacheElement object
136 * <p>
137 * @return The IElementAttributes value, never null
138 */
139 public IElementAttributes getElementAttributes()
140 {
141 // create default attributes if they are null
142 // this shouldn't happen, but could if a corrupt
143 // object was sent over the wire.
144 if ( this.attr == null )
145 {
146 this.attr = new ElementAttributes();
147 }
148 return this.attr;
149 }
150
151 /**
152 * @return a hash of the key only
153 */
154 public int hashCode()
155 {
156 return key.hashCode();
157 }
158
159 /**
160 * For debugging only.
161 * <p>
162 * @return String representation
163 */
164 public String toString()
165 {
166 return "[CacheElement: cacheName [" + cacheName + "], key [" + key + "], val [" + val + "], attr [" + attr
167 + "]";
168 }
169 }