001package org.apache.commons.jcs3.engine;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Arrays;
023import java.util.Objects;
024
025import org.apache.commons.jcs3.engine.behavior.ICacheElementSerialized;
026import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
027
028/** Either serialized value or the value should be null; */
029public class CacheElementSerialized<K, V>
030    extends CacheElement<K, V>
031    implements ICacheElementSerialized<K, V>
032{
033    /** Don't change. */
034    private static final long serialVersionUID = -7265084818647601874L;
035
036    /** The serialized value. */
037    private final byte[] serializedValue;
038
039    /**
040     * Constructs a usable wrapper.
041     * <p>
042     * @param cacheNameArg
043     * @param keyArg
044     * @param serializedValueArg
045     * @param elementAttributesArg
046     */
047    public CacheElementSerialized( final String cacheNameArg, final K keyArg, final byte[] serializedValueArg,
048                                   final IElementAttributes elementAttributesArg )
049    {
050        super(cacheNameArg, keyArg, null, elementAttributesArg);
051        this.serializedValue = serializedValueArg;
052    }
053
054    /** @return byte[] */
055    @Override
056    public byte[] getSerializedValue()
057    {
058        return this.serializedValue;
059    }
060
061    /**
062     * @param obj other object
063     * @return true if this object key equals the key of obj
064     */
065    @Override
066    public boolean equals(final Object obj)
067    {
068        if (this == obj)
069        {
070            return true;
071        }
072        if (!(obj instanceof CacheElementSerialized))
073        {
074            return false;
075        }
076        final CacheElementSerialized<?,?> other = (CacheElementSerialized<?,?>) obj;
077        return Objects.equals(getKey(), other.getKey());
078    }
079
080    /**
081     * For debugging only.
082     * <p>
083     * @return debugging string.
084     */
085    @Override
086    public String toString()
087    {
088        final StringBuilder buf = new StringBuilder();
089        buf.append( "\n CacheElementSerialized: " );
090        buf.append( "\n CacheName = [" + getCacheName() + "]" );
091        buf.append( "\n Key = [" + getKey() + "]" );
092        buf.append( "\n SerializedValue = " + Arrays.toString(getSerializedValue()) );
093        buf.append( "\n ElementAttributes = " + getElementAttributes() );
094        return buf.toString();
095    }
096
097}