1 package org.apache.commons.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<K>
36 implements Serializable, Externalizable
37 {
38 /** Don't change */
39 private static final long serialVersionUID = -1400659301208101411L;
40
41 /** The key */
42 private K 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( K key )
51 {
52 this.key = key;
53 }
54
55 /**
56 * @return Returns the key.
57 */
58 public K 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 @Override
87 public String toString()
88 {
89 StringBuilder buf = new StringBuilder();
90 buf.append( "\nBlockDiskElementDescriptor" );
91 buf.append( "\n key [" + this.getKey() + "]" );
92 buf.append( "\n blocks [" );
93 if ( this.getBlocks() != null )
94 {
95 for ( int i = 0; i < blocks.length; i++ )
96 {
97 buf.append( this.getBlocks()[i] );
98 }
99 }
100 buf.append( "]" );
101 return buf.toString();
102 }
103
104 /**
105 * Saves on reflection.
106 * <p>
107 * (non-Javadoc)
108 * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
109 */
110 @Override
111 @SuppressWarnings("unchecked") // Need cast to K
112 public void readExternal( ObjectInput input )
113 throws IOException, ClassNotFoundException
114 {
115 this.key = (K) input.readObject();
116 this.blocks = (int[]) input.readObject();
117 }
118
119 /**
120 * Saves on reflection.
121 * <p>
122 * (non-Javadoc)
123 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
124 */
125 @Override
126 public void writeExternal( ObjectOutput output )
127 throws IOException
128 {
129 output.writeObject( this.key );
130 output.writeObject( this.blocks );
131 }
132 }