001package org.apache.commons.jcs3.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; 027import java.util.Arrays; 028 029/** 030 * This represents an element on disk. This is used when we persist the keys. We only store the 031 * block addresses in memory. We don't need the length here, since all the blocks are the same size 032 * recycle bin. 033 */ 034public class BlockDiskElementDescriptor<K> 035 implements Serializable, Externalizable 036{ 037 /** Don't change */ 038 private static final long serialVersionUID = -1400659301208101411L; 039 040 /** The key */ 041 private K key; 042 043 /** The array of block numbers */ 044 private int[] blocks; 045 046 /** 047 * Default constructor 048 */ 049 public BlockDiskElementDescriptor() 050 { 051 super(); 052 } 053 054 /** 055 * Constructor 056 * 057 * @param key the key 058 * @param blocks the data 059 * 060 * @since 3.1 061 */ 062 public BlockDiskElementDescriptor(K key, int[] blocks) 063 { 064 super(); 065 this.key = key; 066 this.blocks = blocks; 067 } 068 069 /** 070 * @param key The key to set. 071 */ 072 public void setKey( final K key ) 073 { 074 this.key = key; 075 } 076 077 /** 078 * @return Returns the key. 079 */ 080 public K getKey() 081 { 082 return key; 083 } 084 085 /** 086 * @param blocks The blocks to set. 087 */ 088 public void setBlocks( final int[] blocks ) 089 { 090 this.blocks = blocks; 091 } 092 093 /** 094 * This holds the block numbers. An item my be dispersed between multiple blocks. 095 * <p> 096 * @return Returns the blocks. 097 */ 098 public int[] getBlocks() 099 { 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}