001package org.apache.commons.jcs.auxiliary.disk.indexed;
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.io.Serializable;
023
024/**
025 * Disk objects are located by descriptor entries. These are saved on shutdown and loaded into
026 * memory on startup.
027 */
028public class IndexedDiskElementDescriptor
029    implements Serializable, Comparable<IndexedDiskElementDescriptor>
030{
031    /** Don't change */
032    private static final long serialVersionUID = -3029163572847659450L;
033
034    /** Position of the cache data entry on disk. */
035    long pos;
036
037    /** Number of bytes the serialized form of the cache data takes. */
038    int len;
039
040    /**
041     * Constructs a usable disk element descriptor.
042     * <p>
043     * @param pos
044     * @param len
045     */
046    public IndexedDiskElementDescriptor( long pos, int len )
047    {
048        this.pos = pos;
049        this.len = len;
050    }
051
052    /**
053     * @return debug string
054     */
055    @Override
056    public String toString()
057    {
058        StringBuilder buf = new StringBuilder();
059        buf.append( "[DED: " );
060        buf.append( " pos = " + pos );
061        buf.append( " len = " + len );
062        buf.append( "]" );
063        return buf.toString();
064    }
065
066    /**
067     * @see java.lang.Object#hashCode()
068     */
069    @Override
070    public int hashCode()
071    {
072        return Long.valueOf(this.pos).hashCode() ^ Integer.valueOf(len).hashCode();
073    }
074
075    /**
076     * @see java.lang.Object#equals(java.lang.Object)
077     */
078    @Override
079    public boolean equals(Object o)
080    {
081        if (o == null)
082        {
083                return false;
084        }
085        else if (o instanceof IndexedDiskElementDescriptor)
086        {
087                IndexedDiskElementDescriptor ided = (IndexedDiskElementDescriptor)o;
088            return pos == ided.pos && len == ided.len;
089        }
090
091        return false;
092    }
093
094    /**
095     * Compares based on length, then on pos descending.
096     * <p>
097     * @param o Object
098     * @return int
099     */
100    @Override
101    public int compareTo( IndexedDiskElementDescriptor o )
102    {
103        if ( o == null )
104        {
105            return 1;
106        }
107
108        if ( o.len == len )
109        {
110                if ( o.pos == pos )
111                {
112                        return 0;
113                }
114                else if ( o.pos < pos )
115                {
116                        return -1;
117                }
118                else
119                {
120                        return 1;
121                }
122        }
123        else if ( o.len > len )
124        {
125            return -1;
126        }
127        else
128        {
129            return 1;
130        }
131    }
132}