1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2006 - 2009 Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.libraries.fonts.tools;
import java.io.Serializable;
import java.util.Arrays;
/**
* A lookup table for objects. This implementation is not synchronized, it is up
* to the caller to synchronize it properly.
*
* @author Thomas Morgner
*/
public class ByteTable implements Serializable
{
/**
* For serialization.
*/
private static final long serialVersionUID = -276004279213053063L;
/**
* The number of rows.
*/
private int rows;
/**
* The number of columns.
*/
private int columns;
/**
* An array of objects. The array may contain <code>null</code> values.
*/
private byte[][] data;
/**
* Creates a new table.
*
* @param rows the inital number of rows.
* @param cols the initial number of columns.
*/
public ByteTable(final int rows, final int cols)
{
if (rows < 1)
{
throw new IllegalArgumentException("Increment must be positive.");
}
if (cols < 1)
{
throw new IllegalArgumentException("Increment must be positive.");
}
this.rows = rows;
this.columns = cols;
this.data = new byte[rows][];
}
/**
* Ensures that there is storage capacity for the specified item.
*
* @param row the row index.
* @param column the column index.
*/
public void ensureCapacity(final int row, final int column)
{
if (row < 0 || row >= this.rows)
{
throw new IndexOutOfBoundsException("Row is invalid. " + row);
}
if (column < 0 || column >= this.columns)
{
throw new IndexOutOfBoundsException("Column is invalid. " + column);
}
final byte[] current = this.data[row];
if (current == null)
{
this.data[row] = new byte[Math.max(column + 1, this.columns)];
}
}
/**
* Returns the number of rows in the table.
*
* @return The row count.
*/
public int getRowCount()
{
return this.rows;
}
/**
* Returns the number of columns in the table.
*
* @return The column count.
*/
public int getColumnCount()
{
return this.columns;
}
/**
* Returns the object from a particular cell in the table. Returns null, if
* there is no object at the given position.
* <p/>
* Note: throws IndexOutOfBoundsException if row or column is negative.
*
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @return The object.
*/
public byte getByte (final int row, final int column, final byte defaultValue)
{
if (row < this.data.length)
{
final byte[] current = this.data[row];
if (current == null)
{
return defaultValue;
}
if (column < current.length)
{
return current[column];
}
}
return defaultValue;
}
/**
* Sets the object for a cell in the table. The table is expanded if
* necessary.
*
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @param object the object.
*/
public void setByte(final int row, final int column, final byte object)
{
ensureCapacity(row, column);
this.data[row][column] = object;
}
/**
* Tests this paint table for equality with another object (typically also
* an <code>ObjectTable</code>).
*
* @param o the other object.
* @return A boolean.
*/
public boolean equals(final Object o)
{
if (o == null)
{
return false;
}
if (this == o)
{
return true;
}
if ((o instanceof ByteTable) == false)
{
return false;
}
final ByteTable ot = (ByteTable) o;
if (getRowCount() != ot.getRowCount())
{
return false;
}
if (getColumnCount() != ot.getColumnCount())
{
return false;
}
for (int r = 0; r < getRowCount(); r++)
{
for (int c = 0; c < getColumnCount(); c++)
{
if (getByte(r, c, (byte) -1) == ot.getByte(r, c, (byte) -1) == false)
{
return false;
}
}
}
return true;
}
/**
* Returns a hash code value for the object.
*
* @return the hashcode
*/
public int hashCode()
{
int result = this.rows;
result = 29 * result + this.columns;
return result;
}
/**
* Clears the table.
*/
public void clear(final byte value)
{
this.rows = 0;
this.columns = 0;
final int dataLength = this.data.length;
for (int i = 0; i < dataLength; i++)
{
if (this.data[i] != null)
{
Arrays.fill(this.data[i], value);
}
}
}
//
// protected void setData(final byte[][] data, final int colCount)
// {
// if (data == null) {
// throw new NullPointerException();
// }
// if (colCount < 0) {
// throw new IndexOutOfBoundsException();
// }
//
// this.data = data;
// this.rows = data.length;
// this.columns = colCount;
// }
protected byte[][] getData()
{
return data;
}
}
|