Source for org.jfree.util.ObjectTable

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------
  28:  * ObjectTable.java
  29:  * ----------------
  30:  * (C) Copyright 2003, 2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ObjectTable.java,v 1.8 2005/10/18 13:24:19 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 29-Apr-2003 : Version 1, based on PaintTable class (DG);
  40:  * 21-May-2003 : Copied the array based implementation of StrokeTable and
  41:  *               fixed the serialisation behaviour (TM).
  42:  */
  43: 
  44: package org.jfree.util;
  45: 
  46: import java.io.IOException;
  47: import java.io.ObjectInputStream;
  48: import java.io.ObjectOutputStream;
  49: import java.io.Serializable;
  50: import java.util.Arrays;
  51: 
  52: /**
  53:  * A lookup table for objects. This implementation is not synchronized,
  54:  * it is up to the caller to synchronize it properly.
  55:  *
  56:  * @author Thomas Morgner
  57:  */
  58: public class ObjectTable implements Serializable {
  59: 
  60:     /** For serialization. */
  61:     private static final long serialVersionUID = -3968322452944912066L;
  62:     
  63:     /** The number of rows. */
  64:     private int rows;
  65: 
  66:     /** The number of columns. */
  67:     private int columns;
  68: 
  69:     /** An array of objects.  The array may contain <code>null</code> values. */
  70:     private transient Object[][] data;
  71: 
  72:     /** 
  73:      * Defines how many object-slots get reserved each time we run out of 
  74:      * space. 
  75:      */
  76:     private int rowIncrement;
  77: 
  78:     /** 
  79:      * Defines how many object-slots get reserved each time we run out of 
  80:      * space. 
  81:      */
  82:     private int columnIncrement;
  83: 
  84:     /**
  85:      * Creates a new table.
  86:      */
  87:     public ObjectTable() {
  88:         this (5, 5);
  89:     }
  90: 
  91:     /**
  92:      * Creates a new table.
  93:      *
  94:      * @param increment  the row and column size increment.
  95:      */
  96:     public ObjectTable(final int increment) {
  97:         this (increment, increment);
  98:     }
  99:     /**
 100:      * Creates a new table.
 101:      * 
 102:      * @param rowIncrement  the row size increment.
 103:      * @param colIncrement  the column size increment.
 104:      */
 105:     public ObjectTable(final int rowIncrement, final int colIncrement) {
 106:         if (rowIncrement < 1) {
 107:             throw new IllegalArgumentException("Increment must be positive.");
 108:         }
 109: 
 110:         if (colIncrement < 1) {
 111:             throw new IllegalArgumentException("Increment must be positive.");
 112:         }
 113: 
 114:         this.rows = 0;
 115:         this.columns = 0;
 116:         this.rowIncrement = rowIncrement;
 117:         this.columnIncrement = colIncrement;
 118: 
 119:         this.data = new Object[rowIncrement][];
 120:     }
 121: 
 122:     /**
 123:      * Returns the column size increment.
 124:      * 
 125:      * @return the increment.
 126:      */
 127:     public int getColumnIncrement() {
 128:         return this.columnIncrement;
 129:     }
 130: 
 131:     /**
 132:      * Returns the row size increment.
 133:      * 
 134:      * @return the increment.
 135:      */
 136:     public int getRowIncrement() {
 137:         return this.rowIncrement;
 138:     }
 139: 
 140:     /**
 141:      * Checks that there is storage capacity for the specified row and resizes 
 142:      * if necessary.
 143:      * 
 144:      * @param row  the row index.
 145:      */
 146:     protected void ensureRowCapacity (final int row) {
 147: 
 148:         // does this increase the number of rows?  if yes, create new storage
 149:         if (row >= this.data.length) {
 150: 
 151:             final Object[][] enlarged = new Object[row + this.rowIncrement][];
 152:             System.arraycopy(this.data, 0, enlarged, 0, this.data.length);
 153:             // do not create empty arrays - this is more expensive than checking
 154:             // for null-values.
 155:             this.data = enlarged;
 156:         }
 157:     }
 158: 
 159:     /**
 160:      * Ensures that there is storage capacity for the specified item.
 161:      * 
 162:      * @param row  the row index.
 163:      * @param column  the column index.
 164:      */
 165:     public void ensureCapacity (final int row, final int column) {
 166: 
 167:         if (row < 0) {
 168:             throw new IndexOutOfBoundsException("Row is invalid. " + row);
 169:         }
 170:         if (column < 0) {
 171:             throw new IndexOutOfBoundsException("Column is invalid. " + column);
 172:         }
 173: 
 174:         ensureRowCapacity(row);
 175: 
 176:         final Object[] current = this.data[row];
 177:         if (current == null) {
 178:             final Object[] enlarged 
 179:                 = new Object[Math.max (column + 1, this.columnIncrement)];
 180:             this.data[row] = enlarged;
 181:         }
 182:         else if (column >= current.length) {
 183:             final Object[] enlarged = new Object[column + this.columnIncrement];
 184:             System.arraycopy(current, 0, enlarged, 0, current.length);
 185:             this.data[row] = enlarged;
 186:         }
 187:     }
 188: 
 189:     /**
 190:      * Returns the number of rows in the table.
 191:      *
 192:      * @return The row count.
 193:      */
 194:     public int getRowCount() {
 195:         return this.rows;
 196:     }
 197: 
 198:     /**
 199:      * Returns the number of columns in the table.
 200:      *
 201:      * @return The column count.
 202:      */
 203:     public int getColumnCount() {
 204:         return this.columns;
 205:     }
 206: 
 207:     /**
 208:      * Returns the object from a particular cell in the table.
 209:      * Returns null, if there is no object at the given position.
 210:      * <P>
 211:      * Note: throws IndexOutOfBoundsException if row or column is negative.
 212:      *
 213:      * @param row  the row index (zero-based).
 214:      * @param column  the column index (zero-based).
 215:      *
 216:      * @return The object.
 217:      */
 218:     protected Object getObject(final int row, final int column) {
 219: 
 220:         if (row < this.data.length) {
 221:             final Object[] current = this.data[row];
 222:             if (current == null) {
 223:                 return null;
 224:             }
 225:             if (column < current.length) {
 226:                 return current[column];
 227:             }
 228:         }
 229:         return null;
 230: 
 231:     }
 232: 
 233:     /**
 234:      * Sets the object for a cell in the table.  The table is expanded if 
 235:      * necessary.
 236:      *
 237:      * @param row  the row index (zero-based).
 238:      * @param column  the column index (zero-based).
 239:      * @param object  the object.
 240:      */
 241:     protected void setObject(final int row, final int column, 
 242:                              final Object object) {
 243: 
 244:         ensureCapacity(row, column);
 245: 
 246:         this.data[row][column] = object;
 247:         this.rows = Math.max (this.rows, row + 1);
 248:         this.columns = Math.max (this.columns, column + 1);
 249:     }
 250: 
 251:     /**
 252:      * Tests this paint table for equality with another object (typically also
 253:      * an <code>ObjectTable</code>).
 254:      *
 255:      * @param o  the other object.
 256:      *
 257:      * @return A boolean.
 258:      */
 259:     public boolean equals(final Object o) {
 260: 
 261:         if (o == null) {
 262:             return false;
 263:         }
 264: 
 265:         if (this == o) {
 266:             return true;
 267:         }
 268: 
 269:         if ((o instanceof ObjectTable) == false) {
 270:             return false;
 271:         }
 272: 
 273:         final ObjectTable ot = (ObjectTable) o;
 274:         if (getRowCount() != ot.getRowCount()) {
 275:             return false;
 276:         }
 277: 
 278:         if (getColumnCount() != ot.getColumnCount()) {
 279:             return false;
 280:         }
 281: 
 282:         for (int r = 0; r < getRowCount(); r++) {
 283:             for (int c = 0; c < getColumnCount(); c++) {
 284:                 if (ObjectUtilities.equal(getObject(r, c),
 285:                     ot.getObject(r, c)) == false) {
 286:                     return false;
 287:                 }
 288:             }
 289:         }
 290:         return true;
 291:     }
 292: 
 293:     /**
 294:      * Returns a hash code value for the object.
 295:      *
 296:      * @return the hashcode
 297:      */
 298:     public int hashCode() {
 299:         int result;
 300:         result = this.rows;
 301:         result = 29 * result + this.columns;
 302:         return result;
 303:     }
 304: 
 305:     /**
 306:      * Handles serialization.
 307:      *
 308:      * @param stream  the output stream.
 309:      *
 310:      * @throws java.io.IOException if there is an I/O problem.
 311:      */
 312:     private void writeObject(final ObjectOutputStream stream) 
 313:             throws IOException {
 314:         stream.defaultWriteObject();
 315:         final int rowCount = this.data.length;
 316:         stream.writeInt(rowCount);
 317:         for (int r = 0; r < rowCount; r++) {
 318:             final Object[] column = this.data[r];
 319:             stream.writeBoolean(column != null);
 320:             if (column != null) {
 321:                 final int columnCount = column.length;
 322:                 stream.writeInt(columnCount);
 323:                 for (int c = 0; c < columnCount; c++) {
 324:                     writeSerializedData(stream, column[c]);
 325:                 }
 326:             }
 327:         }
 328:     }
 329: 
 330:     /**
 331:      * Handles the serialization of an single element of this table.
 332:      *
 333:      * @param stream the stream which should write the object
 334:      * @param o the object that should be serialized
 335:      * @throws IOException if an IO error occured
 336:      */
 337:     protected void writeSerializedData(final ObjectOutputStream stream, 
 338:                                        final Object o)
 339:         throws IOException {
 340:         stream.writeObject(o);
 341:     }
 342: 
 343:     /**
 344:      * Restores a serialized object.
 345:      *
 346:      * @param stream  the input stream.
 347:      *
 348:      * @throws java.io.IOException if there is an I/O problem.
 349:      * @throws ClassNotFoundException if a class cannot be found.
 350:      */
 351:     private void readObject(final ObjectInputStream stream) 
 352:             throws IOException, ClassNotFoundException {
 353:         stream.defaultReadObject();
 354:         final int rowCount = stream.readInt();
 355:         this.data = new Object[rowCount][];
 356:         for (int r = 0; r < rowCount; r++) {
 357:             final boolean isNotNull = stream.readBoolean();
 358:             if (isNotNull) {
 359:                 final int columnCount = stream.readInt();
 360:                 final Object[] column = new Object[columnCount];
 361:                 this.data[r] = column;
 362:                 for (int c = 0; c < columnCount; c++) {
 363:                     column[c] = readSerializedData(stream);
 364:                 }
 365:             }
 366:         }
 367:     }
 368: 
 369:     /**
 370:      * Handles the deserialization of a single element of the table.
 371:      *
 372:      * @param stream the object input stream from which to read the object.
 373:      *
 374:      * @return the deserialized object
 375:      *
 376:      * @throws ClassNotFoundException if a class cannot be found.
 377:      * @throws IOException Any of the usual Input/Output related exceptions.
 378:      */
 379:     protected Object readSerializedData(final ObjectInputStream stream)
 380:         throws ClassNotFoundException, IOException {
 381:         return stream.readObject();
 382:     }
 383: 
 384:     /**
 385:      * Clears the table.
 386:      */
 387:     public void clear () {
 388:         this.rows = 0;
 389:         this.columns = 0;
 390:         for (int i = 0; i < this.data.length; i++) {
 391:             if (this.data[i] != null) {
 392:                 Arrays.fill(this.data[i], null);
 393:             }
 394:         }
 395:     }
 396: 
 397:   /**
 398:    * Copys the contents of the old column to the new column.
 399:    *
 400:    * @param oldColumn the index of the old (source) column
 401:    * @param newColumn the index of the new column
 402:    */
 403:   protected void copyColumn (final int oldColumn, final int newColumn)
 404:   {
 405:     for (int i = 0; i < getRowCount(); i++)
 406:     {
 407:       setObject(i, newColumn, getObject(i, oldColumn));
 408:     }
 409:   }
 410: 
 411:   /**
 412:    * Copys the contents of the old row to the new row. This uses raw access
 413:    * to the data and is remarkably faster than manual copying.
 414:    *
 415:    * @param oldRow the index of the old row
 416:    * @param newRow the index of the new row
 417:    */
 418:   protected void copyRow (final int oldRow, final int newRow)
 419:   {
 420:     this.ensureCapacity(newRow, getColumnCount());
 421:     final Object[] oldRowStorage = this.data[oldRow];
 422:     if (oldRowStorage == null)
 423:     {
 424:       final Object[] newRowStorage = this.data[newRow];
 425:       if (newRowStorage != null)
 426:       {
 427:         Arrays.fill(newRowStorage, null);
 428:       }
 429:     }
 430:     else
 431:     {
 432:       this.data[newRow] = (Object[]) oldRowStorage.clone();
 433:     }
 434:   }
 435: }