001package org.apache.commons.jcs.auxiliary.disk.block; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.Externalizable; 023import java.io.IOException; 024import java.io.ObjectInput; 025import java.io.ObjectOutput; 026import java.io.Serializable; 027 028/** 029 * This represents an element on disk. This is used when we persist the keys. We only store the 030 * block addresses in memory. We don't need the length here, since all the blocks are the same size 031 * receyle bin. 032 * <p> 033 * @author Aaron Smuts 034 */ 035public class BlockDiskElementDescriptor<K> 036 implements Serializable, Externalizable 037{ 038 /** Don't change */ 039 private static final long serialVersionUID = -1400659301208101411L; 040 041 /** The key */ 042 private K key; 043 044 /** The array of block numbers */ 045 private int[] blocks; 046 047 /** 048 * @param key The key to set. 049 */ 050 public void setKey( K key ) 051 { 052 this.key = key; 053 } 054 055 /** 056 * @return Returns the key. 057 */ 058 public K getKey() 059 { 060 return key; 061 } 062 063 /** 064 * @param blocks The blocks to set. 065 */ 066 public void setBlocks( int[] blocks ) 067 { 068 this.blocks = blocks; 069 } 070 071 /** 072 * This holds the block numbers. An item my be dispersed between multiple blocks. 073 * <p> 074 * @return Returns the blocks. 075 */ 076 public int[] getBlocks() 077 { 078 return blocks; 079 } 080 081 /** 082 * For debugging. 083 * <p> 084 * @return Info on the descriptor. 085 */ 086 @Override 087 public String toString() 088 { 089 StringBuilder buf = new StringBuilder(); 090 buf.append( "\nBlockDiskElementDescriptor" ); 091 buf.append( "\n key [" + this.getKey() + "]" ); 092 buf.append( "\n blocks [" ); 093 if ( this.getBlocks() != null ) 094 { 095 for ( int i = 0; i < blocks.length; i++ ) 096 { 097 buf.append( this.getBlocks()[i] ); 098 } 099 } 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}