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