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  import java.util.Arrays;
24  
25  import org.apache.jcs.engine.behavior.ICacheElementSerialized;
26  import org.apache.jcs.engine.behavior.IElementAttributes;
27  
28  /** Either serialized value or the value should be null; */
29  public class CacheElementSerialized<K extends Serializable, V extends Serializable>
30      implements ICacheElementSerialized<K, V>
31  {
32      /** Don't change. */
33      private static final long serialVersionUID = -7265084818647601874L;
34  
35      /** The name of the cache region. This is a namespace. */
36      private final String cacheName;
37  
38      /** This is the cache key by which the value can be referenced. */
39      private final K key;
40  
41      /** The serialized value. */
42      private final byte[] serializedValue;
43  
44      /**
45       * These attributes hold information about the element and what it is allowed to do.
46       */
47      private IElementAttributes elementAttributes;
48  
49      /**
50       * Constructs a usable wrapper.
51       * <p>
52       * @param cacheNameArg
53       * @param keyArg
54       * @param serializedValueArg
55       * @param elementAttributesArg
56       */
57      public CacheElementSerialized( String cacheNameArg, K keyArg, byte[] serializedValueArg,
58                                     IElementAttributes elementAttributesArg )
59      {
60          this.cacheName = cacheNameArg;
61          this.key = keyArg;
62          this.serializedValue = serializedValueArg;
63          this.elementAttributes = elementAttributesArg;
64      }
65  
66      /**
67       * Returns the name of the cache. This is the name of the region.
68       * <p>
69       * @return this.cacheName;
70       */
71      public String getCacheName()
72      {
73          return this.cacheName;
74      }
75  
76      /** @return Serializable */
77      public K getKey()
78      {
79          return this.key;
80      }
81  
82      /** @return byte[] */
83      public byte[] getSerializedValue()
84      {
85          return this.serializedValue;
86      }
87  
88      /** @return IElementAttributes */
89      public IElementAttributes getElementAttributes()
90      {
91          return this.elementAttributes;
92      }
93  
94      /**
95       * @param attr
96       */
97      public void setElementAttributes( IElementAttributes attr )
98      {
99          this.elementAttributes = attr;
100     }
101 
102     /**
103      * Backward compatibility.
104      * <p>
105      * @return Serializable
106      */
107     public V getVal()
108     {
109         return null;
110     }
111 
112     /**
113      * For debugging only.
114      * <p>
115      * @return debugging string.
116      */
117     @Override
118     public String toString()
119     {
120         StringBuffer buf = new StringBuffer();
121         buf.append( "\n CacheElementSerialized: " );
122         buf.append( "\n CacheName = [" + getCacheName() + "]" );
123         buf.append( "\n Key = [" + getKey() + "]" );
124         buf.append( "\n SerializedValue = " + Arrays.toString(getSerializedValue()) );
125         buf.append( "\n ElementAttributes = " + getElementAttributes() );
126         return buf.toString();
127     }
128 
129 }