001package org.apache.commons.jcs3.auxiliary.disk;
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.CacheElement;
025import org.apache.commons.jcs3.engine.behavior.ICacheElement;
026import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
027
028/**
029 * Implementation of cache elements in purgatory.
030 *
031 * Elements are stored in purgatory when they are spooled to the auxiliary cache, but have not yet
032 * been written to disk.
033 */
034public class PurgatoryElement<K, V>
035    extends CacheElement<K, V> // Remove this superclass in next major release
036//    implements ICacheElement<K, V>
037{
038    /** Don't change */
039    private static final long serialVersionUID = -8152034342684135628L;
040
041    /** Is the element ready to be spooled? */
042    private boolean spoolable;
043
044    /** Wrapped cache Element */
045    private final ICacheElement<K, V> cacheElement;
046
047    /**
048     * Constructor for the PurgatoryElement&lt;K, V&gt; object
049     *
050     * @param cacheElement CacheElement
051     */
052    public PurgatoryElement( final ICacheElement<K, V> cacheElement )
053    {
054        super(cacheElement.getCacheName(), cacheElement.getKey(), cacheElement.getVal());
055        this.cacheElement = cacheElement;
056    }
057
058    /**
059     * Gets the spoolable property.
060     *
061     * @return The spoolable value
062     */
063    public boolean isSpoolable()
064    {
065        return spoolable;
066    }
067
068    /**
069     * Sets the spoolable property.
070     *
071     * @param spoolable The new spoolable value
072     */
073    public void setSpoolable( final boolean spoolable )
074    {
075        this.spoolable = spoolable;
076    }
077
078    /**
079     * Get the wrapped cache element.
080     *
081     * @return ICacheElement
082     */
083    public ICacheElement<K, V> getCacheElement()
084    {
085        return cacheElement;
086    }
087
088    // ------------------------------------------------ interface ICacheElement
089
090    /**
091     * @return cacheElement.getCacheName();
092     * @see ICacheElement#getCacheName
093     */
094    @Override
095    public String getCacheName()
096    {
097        return cacheElement.getCacheName();
098    }
099
100    /**
101     * @return cacheElement.getKey();
102     * @see ICacheElement#getKey
103     */
104    @Override
105    public K getKey()
106    {
107        return cacheElement.getKey();
108    }
109
110    /**
111     * @return cacheElement.getVal();
112     * @see ICacheElement#getVal
113     */
114    @Override
115    public V getVal()
116    {
117        return cacheElement.getVal();
118    }
119
120    /**
121     * @return cacheElement.getElementAttributes();
122     * @see ICacheElement#getElementAttributes
123     */
124    @Override
125    public IElementAttributes getElementAttributes()
126    {
127        return cacheElement.getElementAttributes();
128    }
129
130    /**
131     * @param attr
132     * @see ICacheElement#setElementAttributes
133     */
134    @Override
135    public void setElementAttributes( final IElementAttributes attr )
136    {
137        cacheElement.setElementAttributes( attr );
138    }
139
140    /**
141     * @param obj other object
142     * @return true if this object key equals the key of obj
143     */
144    @Override
145    public boolean equals(final Object obj)
146    {
147        if (this == obj)
148        {
149            return true;
150        }
151        if (!(obj instanceof PurgatoryElement))
152        {
153            return false;
154        }
155        final PurgatoryElement<?,?> other = (PurgatoryElement<?,?>) obj;
156        return Objects.equals(getKey(), other.getKey());
157    }
158
159    /**
160     * @return a hash of the key only
161     */
162    @Override
163    public int hashCode()
164    {
165        return getKey().hashCode();
166    }
167
168    /**
169     * @return debug string
170     */
171    @Override
172    public String toString()
173    {
174        final StringBuilder buf = new StringBuilder();
175        buf.append( "[PurgatoryElement: " );
176        buf.append( " isSpoolable = " + isSpoolable() );
177        buf.append( " CacheElement = " + getCacheElement() );
178        buf.append( " CacheName = " + getCacheName() );
179        buf.append( " Key = " + getKey() );
180        buf.append( " Value = " + getVal() );
181        buf.append( " ElementAttributes = " + getElementAttributes() );
182        buf.append( "]" );
183        return buf.toString();
184    }
185}