/** * A component of a library for * MoSeS. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package uk.ac.leeds.ccg.andyt.projects.moses.io; import java.io.IOException; import java.io.RandomAccessFile; import java.io.Serializable; /** * Abstract class for a data record. */ public abstract class AbstractDataRecord implements Serializable, Comparable { @Override public int hashCode() { int hash = 7; hash = 53 * hash + (int) (this._RecordID ^ (this._RecordID >>> 32)); return hash; } /** * @return 8 (assumed to be the number of bits in a byte). */ public static long getNumberOfBitsInByte() { return 8L; } /** * An individual sequencial identifier. */ protected long _RecordID; /** * @return A copy of this._RecordID */ public long get_RecordID() { return this._RecordID; } /** * Initialise from aAbstractDataRecord. */ protected void _Init(AbstractDataRecord aAbstractDataRecord) { this._RecordID = aAbstractDataRecord._RecordID; } /** * Initialise. */ protected void _Init() { this._RecordID = Long.MIN_VALUE; } /** * @return A String description of this. */ @Override public String toString() { String result = new String( "_RecordID " + this._RecordID); return result; } /** * @return A Comma Seperated Version (CSV) String of the values * of the Fields of this. */ public String toCSVString() { String result = new String( "" + this._RecordID); return result; } /** * @return A Comma Seperated Version (CSV) String of the names * of the Variables as returned in toString(). */ public String toCSVStringFields() { // // Becasue this.getClass().getFields() is not guaranteed to return an // array in a particular order this is not used. // String result = new String(); // Field[] tFields = this.getClass().getFields(); // // Add name of first Field in tFields // result += tFields[ 0 ].getName(); // // Add names of all but the first Field in tFields preceeded by a // comma // for ( int i0 = 1; i0 < tFields.length; i0 ++ ) { // result += "," + tFields[ i0 ]; // } String result = new String("_RecordID"); return result; // String result = new String(); // String[] toStringSplit = toString().split( "," ); // String[] stringVariableNameAndValue; // stringVariableNameAndValue = toStringSplit[ 0 ].split( " " ); // result += stringVariableNameAndValue[ 0 ]; // for ( int i = 1; i < toStringSplit.length; i ++ ) { // stringVariableNameAndValue = toStringSplit[ i ].split( " " ); // if ( stringVariableNameAndValue.length != 0 ) { // result += "," + stringVariableNameAndValue[ 0 ]; // } // } // return result; } /** * Writes out: * * to aRandomAccessFile. * * @param aRandomAccessFile * The RandomAccessFile written to. */ public void write( RandomAccessFile aRandomAccessFile) throws IOException { aRandomAccessFile.writeLong(this._RecordID); } /** * @see java.lang.Object#equals(Object) */ @Override public boolean equals(Object object) { if (this == object) { return true; } if ((object == null) || (object.getClass() != this.getClass())) { return false; } AbstractDataRecord aAbstractDataRecord = (AbstractDataRecord) object; return (this._RecordID == aAbstractDataRecord._RecordID); } /** * Method required by Comparable * @see java.lang.Comparable#compareTo(Object) */ public int compareTo(Object object) { if (object instanceof AbstractDataRecord) { AbstractDataRecord aDataRecord = (AbstractDataRecord) object; if (aDataRecord._RecordID == this._RecordID) { return 0; } if (aDataRecord._RecordID > this._RecordID) { return 1; } } return -1; } /** * @return The size (in bytes) of this as a long. */ public long getSizeInBytes() { return ((long) Long.SIZE) / getNumberOfBitsInByte(); } }