/** * 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.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.Serializable; import java.io.StreamTokenizer; import java.util.HashMap; import java.util.Random; import java.util.Vector; import uk.ac.leeds.ccg.andyt.projects.moses.utilities.ErrorAndExceptionHandler; import uk.ac.leeds.ccg.andyt.projects.moses.utilities.StaticIO; /** * For accessing ISARDataRecords and information about them. */ public class ISARDataHandler extends AbstractDataHandler { /** * For storing ISARDataRecords. */ protected ISARDataRecord[] _ISARDataRecordArray; protected HashMap _AgeSexType_ISARDataRecordVector_HashMap_0; protected HashMap _AgeSexType_ISARDataRecordVector_HashMap_1; /** Creates a new instance of ISARDataHandler */ public ISARDataHandler() { } /** * Creates a new instance of ISARDataHandler from aFile. * @param aFile */ public ISARDataHandler( File aFile) { _Directory = aFile.getParentFile(); init(_Directory); if (aFile.getName().endsWith(".dat")) { init(aFile.getParentFile()); load(aFile); this._RecordLength = new ISARDataRecord().getSizeInBytes(); loadIntoCache(); File thisFile = new File( _Directory, this.getClass().getCanonicalName() + ".thisFile"); init_AgeSexType_ISARDataRecordVector_HashMap_0(); StaticIO.writeObject( this, thisFile); } else { Object object = StaticIO.readObject(aFile); ISARDataHandler aISARDataHandler = (ISARDataHandler) object; load(aFile); this._RecordLength = aISARDataHandler._RecordLength; //this._RecordLength = new ISARDataRecord().getSizeInBytes(); this._ISARDataRecordArray = aISARDataHandler._ISARDataRecordArray; this._AgeSexType_ISARDataRecordVector_HashMap_0 = aISARDataHandler._AgeSexType_ISARDataRecordVector_HashMap_0; } } /** * Loads ISARDataRecords into the cache. */ public void loadIntoCache() { _Logger.entering(this.getClass().getCanonicalName(), "loadIntoCache()"); long nDataRecords = super.getNDataRecords(); if (nDataRecords > Integer.MAX_VALUE) { log("nDataRecords>Integer.MAX_VALUE"); System.exit(ErrorAndExceptionHandler.NumberFormatException); } this._ISARDataRecordArray = new ISARDataRecord[(int) nDataRecords]; try { this._RandomAccessFile.seek(0); } catch (IOException ioe0) { ioe0.printStackTrace(); } for (int _ISARRecordID = 0; _ISARRecordID < nDataRecords; _ISARRecordID++) { this._ISARDataRecordArray[_ISARRecordID] = new ISARDataRecord( this._RandomAccessFile); if (_ISARRecordID % 10000 == 0) { log("loadIntoCache " + _ISARRecordID); } } init_AgeSexType_ISARDataRecordVector_HashMap_0(); _Logger.exiting(this.getClass().getCanonicalName(), "loadIntoCache()"); } /** * Loads from source file */ public void formatSource( File sourceFile, File formattedFile) throws IOException { _Logger.entering( this.getClass().getCanonicalName(), "formatSource(File,File)"); _File = formattedFile; // _File = new File ( // _Directory, // ISARDataRecords.dat); if (!_File.exists()) { this._File.createNewFile(); } this._RandomAccessFile = new RandomAccessFile(this._File, "rw"); //File sourceFile = new File( // "C:/work/data/census/2001/SAR/01uklicind-20050401.dat"); BufferedReader aBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(sourceFile))); StreamTokenizer aStreamTokenizer = new StreamTokenizer(aBufferedReader); StaticIO.setStreamTokenizerSyntax2(aStreamTokenizer); String line; long RecordID = 0L; ISARDataRecord aISARDataRecord = new ISARDataRecord(); // Skip the first line int tokenType = aStreamTokenizer.nextToken(); while (tokenType != StreamTokenizer.TT_EOL) { tokenType = aStreamTokenizer.nextToken(); } tokenType = aStreamTokenizer.nextToken(); boolean parsed = false; while (tokenType != aStreamTokenizer.TT_EOF) { switch (tokenType) { case StreamTokenizer.TT_EOL: if (RecordID % 10000 == 0) { log(aISARDataRecord.toString()); } // Write out householdSARRecord if (parsed) { aISARDataRecord.write(this._RandomAccessFile); // log( "this.tRandomAccessFile.length() " + // this.tRandomAccessFile.length() ); RecordID++; } break; case StreamTokenizer.TT_WORD: line = aStreamTokenizer.sval; parsed = aISARDataRecord.parse( RecordID, line); break; } tokenType = aStreamTokenizer.nextToken(); } log("Number of ISARDataRecords loaded " + (RecordID + 1L)); this._RandomAccessFile.close(); this._RandomAccessFile = new RandomAccessFile(this._File, "r"); _Logger.exiting( this.getClass().getCanonicalName(), "formatSource(File,File)"); } /** * @param RecordID * The RecordID of the ISARDataRecord * to be returned. */ public AbstractDataRecord getDataRecord(long RecordID) { return getISARDataRecord(RecordID); } /** * @param RecordID * The RecordID of the ISARDataRecord * to be returned. */ public ISARDataRecord getISARDataRecord(long RecordID) { return this._ISARDataRecordArray[(int) RecordID]; } /** * @return _AgeSexType_ISARDataRecordArray_HashMap first initialising it if * it is null. */ public HashMap get_AgeSexType_ISARDataRecordVector_HashMap_0() { if (_AgeSexType_ISARDataRecordVector_HashMap_0 == null) { init_AgeSexType_ISARDataRecordVector_HashMap_0(); } return _AgeSexType_ISARDataRecordVector_HashMap_0; } /** * Initialises _AgeSexType_ISARDataRecordArray_HashMap by going through * _ISARDataRecordArray. */ protected void init_AgeSexType_ISARDataRecordVector_HashMap_0() { _AgeSexType_ISARDataRecordVector_HashMap_0 = get_AgeSexType_ISARDataRecordVector_HashMap_0(_ISARDataRecordArray); } /** * Method to be used to look up ISARDataRecord from ISARDataRecord._ID. * @return a HashMap for looking up RecordID from ID */ public HashMap get_ID_RecordID_HashMap() { HashMap tID_RecordID_HashMap = new HashMap(); ISARDataRecord aISARDataRecord; for (long RecordID = 0; RecordID < getNDataRecords(); RecordID++) { aISARDataRecord = getISARDataRecord(RecordID); tID_RecordID_HashMap.put( aISARDataRecord.get_ID(), RecordID); } return tID_RecordID_HashMap; } @Override public long getNDataRecords() { return this._ISARDataRecordArray.length; } /** * @return HashMap _AgeSexType_ISARDataRecordArray_HashMap with keys that * are AgeSexType and values that are Vecots containing ISARDataRecords. */ public HashMap get_AgeSexType_ISARDataRecordVector_HashMap_0( ISARDataRecord[] aISARDataRecordArray) { HashMap aAgeSexType_ISARDataRecordVector_HashMap_0 = new HashMap(); ISARDataRecord aISARDataRecord; AgeSexType _AgeSexType; Object object; Vector _ISARDataRecordVector; for (int aISARDataRecordID = 0; aISARDataRecordID < aISARDataRecordArray.length; aISARDataRecordID++) { aISARDataRecord = aISARDataRecordArray[aISARDataRecordID]; _AgeSexType = new AgeSexType(aISARDataRecord); object = aAgeSexType_ISARDataRecordVector_HashMap_0.get(_AgeSexType); if (object == null) { _ISARDataRecordVector = new Vector(); } else { _ISARDataRecordVector = (Vector) object; } _ISARDataRecordVector.add(aISARDataRecord); aAgeSexType_ISARDataRecordVector_HashMap_0.put( _AgeSexType, _ISARDataRecordVector); } return aAgeSexType_ISARDataRecordVector_HashMap_0; } /** * a return integer count of the number of other ISARDataRecords in * _ISARDataRecordArray with the same * * AgeSexType(aISARDataRecord) * */ public int getAgeSexTypeCount_0(ISARDataRecord aISARDataRecord) { AgeSexType aAgeSexType = new AgeSexType(aISARDataRecord); HashMap aAgeSexType_ISARDataRecordVector_HashMap = get_AgeSexType_ISARDataRecordVector_HashMap_0(); Object object = aAgeSexType_ISARDataRecordVector_HashMap.get(aAgeSexType); if (object == null) { return 0; } else { Vector _ISARDataRecordVector = (Vector) object; return _ISARDataRecordVector.size(); } } /** * a return integer count of the number of other ISARDataRecords in * _ISARDataRecordArray with the same * * AgeSexType(aISARDataRecord) * */ public int getAgeSexTypeCount_0(AgeSexType aAgeSexType) { HashMap aAgeSexType_ISARDataRecordVector_HashMap = get_AgeSexType_ISARDataRecordVector_HashMap_0(); Object object = aAgeSexType_ISARDataRecordVector_HashMap.get(aAgeSexType); if (object == null) { return 0; } else { Vector _ISARDataRecordVector = (Vector) object; return _ISARDataRecordVector.size(); } } /** * * @param aISARDataRecord * @param aRandom * @return ISARDataRecord with the same AgeSexType as aISARDataRecord or * null if there is no such ISARDataRecord */ public ISARDataRecord getISARDataRecord( Random aRandom, ISARDataRecord aISARDataRecord) { ISARDataRecord result; HashMap aAgeSexType_ISARDataRecordVector_HashMap = get_AgeSexType_ISARDataRecordVector_HashMap_0(); AgeSexType aAgeSexType = new AgeSexType(aISARDataRecord); int aAgeSexTypeCount = getAgeSexTypeCount_0(aAgeSexType); int index; if (aAgeSexTypeCount == 0) { return null; } else { index = aRandom.nextInt(aAgeSexTypeCount); } Vector aAgeSexType_ISARDataRecordVector = (Vector) aAgeSexType_ISARDataRecordVector_HashMap.get(aAgeSexType); result = (ISARDataRecord) aAgeSexType_ISARDataRecordVector.elementAt(index); return result; } /** * @return ISARDataRecord with the same AgeSexType as aISARDataRecord or * null if there is no such ISARDataRecord */ public ISARDataRecord getISARDataRecord( Random aRandom, AgeSexType aAgeSexType) { ISARDataRecord result; HashMap aAgeSexType_ISARDataRecordVector_HashMap = get_AgeSexType_ISARDataRecordVector_HashMap_0(); int aAgeSexTypeCount = getAgeSexTypeCount_0(aAgeSexType); int index; if (aAgeSexTypeCount == 0) { return null; } else { index = aRandom.nextInt(aAgeSexTypeCount); } Vector aAgeSexType_ISARDataRecordVector = (Vector) aAgeSexType_ISARDataRecordVector_HashMap.get(aAgeSexType); result = (ISARDataRecord) aAgeSexType_ISARDataRecordVector.elementAt(index); return result; } public static short getRELTOHRType1( ISARDataRecord aISARDataRecord) { short _RELTOHR = aISARDataRecord.get_RELTOHR(); if (_RELTOHR == 1) { return 1; } if (_RELTOHR == -9) { return -9; } return 2; } // /** // * @return an ISARDataRecord[] of randomly selected ISARDataRecords // * with the same aggregate AgeSexType counts as aISARDataRecords. // */ // public ISARDataRecord[] getISARDataRecords( // ISARDataRecord[] aISARDataRecords, // Random aRandom) { // HashMap aAgeSexType_ISARDataRecordVector_HashMap_0 = // get_AgeSexType_ISARDataRecordVector_HashMap_0(aISARDataRecords); // } /** * A simple class for distinguishing ISARDataRecords into those with the * same of Age, Sex and Type characteristics where Type can be either: * RELTOHR = 1 for HRP * RELTOHR = -9 for Communal Establishment Populations * RELTOHR = other for general household person */ public class AgeSexType implements Serializable, Comparable { /** * For storing _AGE */ protected short _Age; /** * For storing the _SEX */ protected boolean _Sex; /** * For storing Type */ protected short _Type; public AgeSexType() { } public AgeSexType( short _Age, boolean _Sex, short _Type) { this._Age = _Age; this._Sex = _Sex; this._Type = _Type; } public AgeSexType( AgeSexType aAgeSexType) { this._Age = aAgeSexType._Age; this._Sex = aAgeSexType._Sex; this._Type = aAgeSexType._Type; } public AgeSexType( ISARDataRecord aISARDataRecord) { _Age = aISARDataRecord._AGE0; _Sex = aISARDataRecord._SEX; _Type = getRELTOHRType1(aISARDataRecord); } public short get_Age() { return _Age; } public boolean is_Sex() { return _Sex; } public short get_Type() { return _Type; } @Override public boolean equals(Object o) { return (compareTo(o) == 0); } @Override public int hashCode() { int hash = 7; hash = 89 * hash + this._Age; hash = 89 * hash + (this._Sex ? 1 : 0); hash = 89 * hash + this._Type; return hash; } public int compareTo(Object o) { if (o != null) { if (o instanceof AgeSexType) { AgeSexType aAgeSexType = (AgeSexType) o; if (aAgeSexType._Age > _Age) { return 1; } if (aAgeSexType._Sex == true && _Sex == false) { return 1; } if (aAgeSexType._Sex == false && _Sex == true) { return -1; } if (aAgeSexType._Type > _Type) { return 1; } if (aAgeSexType._Type < _Type) { return -1; } return 0; } } return -1; } } }