View Javadoc

1   package org.apache.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
30  {
31      private static final long serialVersionUID = -3029163572847659450L;
32  
33      /*** Position of the cache data entry on disk. */
34      long pos;
35  
36      /*** Number of bytes the serialized form of the cache data takes. */
37      public int len;
38  
39      /***
40       * Set the offset (i.e. position, and the size of the element)
41       * <p>
42       * @param pos
43       * @param data
44       */
45      public void init( long pos, byte[] data )
46      {
47          this.pos = pos;
48          this.len = data.length;
49      }
50  
51      /*** Constructor for the DiskElementDescriptor object */
52      public IndexedDiskElementDescriptor()
53      {
54          super();
55      }
56  
57      /***
58       * Constructs a usable disk element descriptor.
59       * <p>
60       * @param pos
61       * @param len
62       */
63      public IndexedDiskElementDescriptor( long pos, int len )
64      {
65          this.pos = pos;
66          this.len = len;
67      }
68  
69      public String toString()
70      {
71          StringBuffer buf = new StringBuffer();
72          buf.append( "[DED: " );
73          buf.append( " pos = " + pos );
74          buf.append( " len = " + len );
75          buf.append( "]" );
76          return buf.toString();
77      }
78  
79      /***
80       * Compares based on length.
81       * <p>
82       * @param o Object
83       * @return int
84       */
85      public int compareTo( Object o )
86      {
87          if ( o == null )
88          {
89              return 1;
90          }
91  
92          int oLen = ( (IndexedDiskElementDescriptor) o ).len;
93          if ( oLen == len )
94          {
95              return 0;
96          }
97          else if ( oLen > len )
98          {
99              return -1;
100         }
101         else if ( oLen < len )
102         {
103             return 1;
104         }
105         return 0;
106     }
107 }