001 package ps6.tigerdb;
002
003 import java.io.Serializable;
004
005 import ps2.GeoPoint;
006
007 /**
008 * TigerRecord.java
009 *
010 * @author Felix S. Klock II
011 */
012
013 public class TigerRecord implements Serializable {
014 public static final long serialVersionUID = 4534;
015 protected static GeoPoint makeGP(int lng, int lat) {
016 GeoPoint gp = new GeoPoint(lat, lng);
017 return gp;
018 }
019
020 /** Special ctor to work around predefined record length (Record
021 * Type C seems to be flawed.
022 */
023 public TigerRecord() { }
024
025 public TigerRecord(String s, int recordSize) {
026 if (s.length() != recordSize)
027 die("length must be "+recordSize+
028 ", not "+s.length());
029 }
030
031 protected void die(String reason) {throw new RuntimeException(reason);}
032
033 /** Parses `s' as an int, with the following additions to the
034 * standard Integer.parseInt() method:
035 * 1. White space around `s' is allowed (and ignored)
036 * 2. If s is an empty string after trimming, throws NoInt
037 * 3. The number may be prefixed by a '+' character.
038 * 4. If s is contains non-numeric data, throws NonNumericDataException
039 */
040 protected int toInt(String s) throws NoInt, NonNumericDataException {
041 s = s.trim();
042 if (s.length() == 0) throw new NoInt();
043 if (s.charAt(0) == '+') {
044 s = s.substring(1);
045 }
046 try {
047 return Integer.parseInt(s);
048 } catch (NumberFormatException e) {
049 throw new NonNumericDataException(s);
050 }
051 }
052
053 protected static class NoInt extends Throwable {
054 public static final long serialVersionUID = 4534;
055 }
056
057 public DirectedStreetNumberRange
058 parseAddrRange(String frStr, String toStr) throws BadRecordException {
059
060 try {
061 int fraddl = toInt(frStr);
062 int toaddl = toInt(toStr);
063 if ((fraddl % 2) != (toaddl % 2))
064 throw new BadRecordException
065 ("bad left address ["+fraddl+","+toaddl+"]",frStr+toStr);
066 int low = Math.min(fraddl, toaddl);
067 int high = Math.max(fraddl, toaddl);
068
069 boolean b = (fraddl < toaddl);
070 IntSet s = new IntSet(low, high);
071 return new DirectedStreetNumberRange(s, b);
072 } catch (NoInt e) {
073 return new DirectedStreetNumberRange();
074 } catch (NumberFormatException e) {
075 throw new NonNumericDataException(frStr+","+toStr);
076 }
077 }
078
079
080 } // TigerRecord