001    package org.apache.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    
022    import java.io.Externalizable;
023    import java.io.IOException;
024    import java.io.ObjectInput;
025    import java.io.ObjectOutput;
026    import 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     */
035    public class BlockDiskElementDescriptor<K extends Serializable>
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            StringBuffer buf = new StringBuffer();
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        @SuppressWarnings("unchecked") // Need cast to K
111        public void readExternal( ObjectInput input )
112            throws IOException, ClassNotFoundException
113        {
114            this.key = (K) input.readObject();
115            this.blocks = (int[]) input.readObject();
116        }
117    
118        /**
119         * Saves on reflection.
120         * <p>
121         * (non-Javadoc)
122         * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
123         */
124        public void writeExternal( ObjectOutput output )
125            throws IOException
126        {
127            output.writeObject( this.key );
128            output.writeObject( this.blocks );
129        }
130    }