View Javadoc

1   package org.apache.jcs.auxiliary.disk.block;
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.Externalizable;
23  import java.io.IOException;
24  import java.io.ObjectInput;
25  import java.io.ObjectOutput;
26  import java.io.Serializable;
27  
28  /***
29   * This represents an element on disk. This is used when we persist the keys. We only store the
30   * block addresses in memory. We don't need the length here, since all the blocks are the same size
31   * receyle bin.
32   * <p>
33   * @author Aaron Smuts
34   */
35  public class BlockDiskElementDescriptor
36      implements Serializable, Externalizable
37  {
38      /*** Don't change */
39      private static final long serialVersionUID = -1400659301208101411L;
40  
41      /*** The key */
42      private Serializable key;
43  
44      /*** The array of block numbers */
45      private int[] blocks;
46  
47      /***
48       * @param key The key to set.
49       */
50      public void setKey( Serializable key )
51      {
52          this.key = key;
53      }
54  
55      /***
56       * @return Returns the key.
57       */
58      public Serializable getKey()
59      {
60          return key;
61      }
62  
63      /***
64       * @param blocks The blocks to set.
65       */
66      public void setBlocks( int[] blocks )
67      {
68          this.blocks = blocks;
69      }
70  
71      /***
72       * This holds the block numbers. An item my be dispersed between multiple blocks.
73       * <p>
74       * @return Returns the blocks.
75       */
76      public int[] getBlocks()
77      {
78          return blocks;
79      }
80  
81      /***
82       * For debugging.
83       * <p>
84       * @return Info on the descriptor.
85       */
86      public String toString()
87      {
88          StringBuffer buf = new StringBuffer();
89          buf.append( "\nBlockDiskElementDescriptor" );
90          buf.append( "\n key [" + this.getKey() + "]" );
91          buf.append( "\n blocks [" );
92          if ( this.getBlocks() != null )
93          {
94              for ( int i = 0; i < blocks.length; i++ )
95              {
96                  buf.append( this.getBlocks()[i] );
97              }
98          }
99          buf.append( "]" );
100         return buf.toString();
101     }
102 
103     /***
104      * Saves on reflection.
105      * <p>
106      * (non-Javadoc)
107      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
108      */
109     public void readExternal( ObjectInput input )
110         throws IOException, ClassNotFoundException
111     {
112         this.key = (Serializable) input.readObject();
113         this.blocks = (int[]) input.readObject();
114     }
115 
116     /***
117      * Saves on reflection.
118      * <p>
119      * (non-Javadoc)
120      * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
121      */
122     public void writeExternal( ObjectOutput output )
123         throws IOException
124     {
125         output.writeObject( this.key );
126         output.writeObject( this.blocks );
127     }
128 }