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.Objects;
023
024import org.apache.commons.jcs3.engine.behavior.ICacheElement;
025import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
026
027/**
028 * Generic element wrapper. Often stuffed inside another.
029 */
030public class CacheElement<K, V>
031    implements ICacheElement<K, V>
032{
033    /** Don't change */
034    private static final long serialVersionUID = -6062305728297627263L;
035
036    /** The name of the cache region. This is a namespace. */
037    private final String cacheName;
038
039    /** This is the cache key by which the value can be referenced. */
040    private final K key;
041
042    /** This is the cached value, reference by the key. */
043    private final V val;
044
045    /**
046     * These attributes hold information about the element and what it is
047     * allowed to do.
048     */
049    private IElementAttributes attr;
050
051    /**
052     * Constructor for the CacheElement object
053     * <p>
054     * @param cacheName
055     * @param key
056     * @param val
057     */
058    public CacheElement( final String cacheName, final K key, final V val )
059    {
060        this.cacheName = cacheName;
061        this.key = key;
062        this.val = val;
063    }
064
065    /**
066     * Constructor for the CacheElement object
067     * <p>
068     * @param cacheName
069     * @param key
070     * @param val
071     * @param attrArg
072     */
073    public CacheElement( final String cacheName, final K key, final V val, final IElementAttributes attrArg )
074    {
075        this(cacheName, key, val);
076        this.attr = attrArg;
077    }
078
079    /**
080     * Gets the cacheName attribute of the CacheElement object
081     * <p>
082     * @return The cacheName value
083     */
084    @Override
085    public String getCacheName()
086    {
087        return this.cacheName;
088    }
089
090    /**
091     * Gets the key attribute of the CacheElement object
092     * <p>
093     * @return The key value
094     */
095    @Override
096    public K getKey()
097    {
098        return this.key;
099    }
100
101    /**
102     * Gets the val attribute of the CacheElement object
103     * <p>
104     * @return The val value
105     */
106    @Override
107    public V getVal()
108    {
109        return this.val;
110    }
111
112    /**
113     * Sets the attributes attribute of the CacheElement object
114     * <p>
115     * @param attr
116     *            The new IElementAttributes value
117     */
118    @Override
119    public void setElementAttributes( final IElementAttributes attr )
120    {
121        this.attr = attr;
122    }
123
124    /**
125     * Gets the IElementAttributes attribute of the CacheElement object
126     * <p>
127     * @return The IElementAttributes value, never null
128     */
129    @Override
130    public IElementAttributes getElementAttributes()
131    {
132        // create default attributes if they are null
133        // this shouldn't happen, but could if a corrupt
134        // object was sent over the wire.
135        if ( this.attr == null )
136        {
137            this.attr = new ElementAttributes();
138        }
139        return this.attr;
140    }
141
142    /**
143     * @param obj other object
144     * @return true if this object key equals the key of obj
145     */
146    @Override
147    public boolean equals(final Object obj)
148    {
149        if (this == obj)
150        {
151            return true;
152        }
153        if (!(obj instanceof CacheElement))
154        {
155            return false;
156        }
157        final CacheElement<?,?> other = (CacheElement<?,?>) obj;
158        return Objects.equals(key, other.key);
159    }
160
161    /**
162     * @return a hash of the key only
163     */
164    @Override
165    public int hashCode()
166    {
167        return key.hashCode();
168    }
169
170    /**
171     * For debugging only.
172     * <p>
173     * @return String representation
174     */
175    @Override
176    public String toString()
177    {
178        return "[CacheElement: cacheName [" + cacheName + "], key [" + key + "], val [" + val + "], attr [" + attr
179            + "]";
180    }
181}