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.Arrays;
23  import java.util.Objects;
24  
25  import org.apache.commons.jcs3.engine.behavior.ICacheElementSerialized;
26  import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
27  
28  /** Either serialized value or the value should be null; */
29  public class CacheElementSerialized<K, V>
30      extends CacheElement<K, V>
31      implements ICacheElementSerialized<K, V>
32  {
33      /** Don't change. */
34      private static final long serialVersionUID = -7265084818647601874L;
35  
36      /** The serialized value. */
37      private final byte[] serializedValue;
38  
39      /**
40       * Constructs a usable wrapper.
41       * <p>
42       * @param cacheNameArg
43       * @param keyArg
44       * @param serializedValueArg
45       * @param elementAttributesArg
46       */
47      public CacheElementSerialized( final String cacheNameArg, final K keyArg, final byte[] serializedValueArg,
48                                     final IElementAttributes elementAttributesArg )
49      {
50          super(cacheNameArg, keyArg, null, elementAttributesArg);
51          this.serializedValue = serializedValueArg;
52      }
53  
54      /** @return byte[] */
55      @Override
56      public byte[] getSerializedValue()
57      {
58          return this.serializedValue;
59      }
60  
61      /**
62       * @param obj other object
63       * @return true if this object key equals the key of obj
64       */
65      @Override
66      public boolean equals(final Object obj)
67      {
68          if (this == obj)
69          {
70              return true;
71          }
72          if (!(obj instanceof CacheElementSerialized))
73          {
74              return false;
75          }
76          final CacheElementSerialized<?,?> other = (CacheElementSerialized<?,?>) obj;
77          return Objects.equals(getKey(), other.getKey());
78      }
79  
80      /**
81       * For debugging only.
82       * <p>
83       * @return debugging string.
84       */
85      @Override
86      public String toString()
87      {
88          final StringBuilder buf = new StringBuilder();
89          buf.append( "\n CacheElementSerialized: " );
90          buf.append( "\n CacheName = [" + getCacheName() + "]" );
91          buf.append( "\n Key = [" + getKey() + "]" );
92          buf.append( "\n SerializedValue = " + Arrays.toString(getSerializedValue()) );
93          buf.append( "\n ElementAttributes = " + getElementAttributes() );
94          return buf.toString();
95      }
96  
97  }