View Javadoc
1   package org.apache.commons.jcs3.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  import java.util.Arrays;
28  
29  /**
30   * This represents an element on disk. This is used when we persist the keys. We only store the
31   * block addresses in memory. We don't need the length here, since all the blocks are the same size
32   * recycle bin.
33   */
34  public class BlockDiskElementDescriptor<K>
35      implements Serializable, Externalizable
36  {
37      /** Don't change */
38      private static final long serialVersionUID = -1400659301208101411L;
39  
40      /** The key */
41      private K key;
42  
43      /** The array of block numbers */
44      private int[] blocks;
45  
46      /**
47       * Default constructor
48       */
49      public BlockDiskElementDescriptor()
50      {
51          super();
52      }
53  
54      /**
55       * Constructor
56       *
57       * @param key the key
58       * @param blocks the data
59       *
60       * @since 3.1
61       */
62      public BlockDiskElementDescriptor(K key, int[] blocks)
63      {
64          super();
65          this.key = key;
66          this.blocks = blocks;
67      }
68  
69      /**
70       * @param key The key to set.
71       */
72      public void setKey( final K key )
73      {
74          this.key = key;
75      }
76  
77      /**
78       * @return Returns the key.
79       */
80      public K getKey()
81      {
82          return key;
83      }
84  
85      /**
86       * @param blocks The blocks to set.
87       */
88      public void setBlocks( final int[] blocks )
89      {
90          this.blocks = blocks;
91      }
92  
93      /**
94       * This holds the block numbers. An item my be dispersed between multiple blocks.
95       * <p>
96       * @return Returns the blocks.
97       */
98      public int[] getBlocks()
99      {
100         return blocks;
101     }
102 
103     /**
104      * For debugging.
105      * <p>
106      * @return Info on the descriptor.
107      */
108     @Override
109     public String toString()
110     {
111         final StringBuilder buf = new StringBuilder();
112         buf.append( "\nBlockDiskElementDescriptor" );
113         buf.append( "\n key [" + this.getKey() + "]" );
114         buf.append( "\n blocks [" );
115         if ( this.getBlocks() != null )
116         {
117             Arrays.stream(this.getBlocks()).forEach(buf::append);
118         }
119         buf.append( "]" );
120         return buf.toString();
121     }
122 
123     /**
124      * Saves on reflection.
125      * <p>
126      * (non-Javadoc)
127      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
128      */
129     @Override
130     @SuppressWarnings("unchecked") // Need cast to K
131     public void readExternal( final ObjectInput input )
132         throws IOException, ClassNotFoundException
133     {
134         this.key = (K) input.readObject();
135         this.blocks = (int[]) input.readObject();
136     }
137 
138     /**
139      * Saves on reflection.
140      * <p>
141      * (non-Javadoc)
142      * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
143      */
144     @Override
145     public void writeExternal( final ObjectOutput output )
146         throws IOException
147     {
148         output.writeObject( this.key );
149         output.writeObject( this.blocks );
150     }
151 }