View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.indexed;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  
24  /**
25   * Disk objects are located by descriptor entries. These are saved on shutdown and loaded into
26   * memory on startup.
27   */
28  public class IndexedDiskElementDescriptor
29      implements Serializable, Comparable<IndexedDiskElementDescriptor>
30  {
31      /** Don't change */
32      private static final long serialVersionUID = -3029163572847659450L;
33  
34      /** Position of the cache data entry on disk. */
35      long pos;
36  
37      /** Number of bytes the serialized form of the cache data takes. */
38      int len;
39  
40      /**
41       * Constructs a usable disk element descriptor.
42       * <p>
43       * @param pos
44       * @param len
45       */
46      public IndexedDiskElementDescriptor( long pos, int len )
47      {
48          this.pos = pos;
49          this.len = len;
50      }
51  
52      /**
53       * @return debug string
54       */
55      @Override
56      public String toString()
57      {
58          StringBuilder buf = new StringBuilder();
59          buf.append( "[DED: " );
60          buf.append( " pos = " + pos );
61          buf.append( " len = " + len );
62          buf.append( "]" );
63          return buf.toString();
64      }
65  
66      /**
67       * @see java.lang.Object#hashCode()
68       */
69      @Override
70      public int hashCode()
71      {
72          return Long.valueOf(this.pos).hashCode() ^ Integer.valueOf(len).hashCode();
73      }
74  
75      /**
76       * @see java.lang.Object#equals(java.lang.Object)
77       */
78      @Override
79      public boolean equals(Object o)
80      {
81      	if (o == null)
82      	{
83      		return false;
84      	}
85      	else if (o instanceof IndexedDiskElementDescriptor)
86          {
87      		IndexedDiskElementDescriptor ided = (IndexedDiskElementDescriptor)o;
88              return pos == ided.pos && len == ided.len;
89          }
90  
91          return false;
92      }
93  
94      /**
95       * Compares based on length, then on pos descending.
96       * <p>
97       * @param o Object
98       * @return int
99       */
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 }