View Javadoc

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