Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/City.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Jan 1, 2008
16 */
17 package uk.me.parabola.imgfmt.app.lbl;
18
19 import uk.me.parabola.imgfmt.app.ImgFileWriter;
20 import uk.me.parabola.imgfmt.app.Label;
21 import uk.me.parabola.imgfmt.app.trergn.Subdivision;
22
23 /**
24 * A city is in a region. It also has (or can have anyway) a reference to
25 * an indexed point within the map itself.
26 *
27 * @author Steve Ratcliffe
28 */
29 public class City {
30 private static final int POINT_REF = 0x8000;
31 private static final int REGION_IS_COUNTRY = 0x4000;
32
33 private int index = -1;
34
35 private final Region region;
36 private final Country country;
37
38 // This determines if a label is being used or a subdivision and point
39 // combo.
40 private boolean pointRef;
41
42 // The location of the city. These could both be zero if we are using a
43 // label instead.
44 private Subdivision subdivision;
45 private int pointIndex;
46
47
48 // You can have either a label or a subdivision and point. This will be
49 // null if the location is being specified.
50 private Label label;
51
52 public City(Region region) {
53 this.region = region;
54 this.country = null;
55 }
56
57 public City(Country country) {
58 this.country = country;
59 this.region = null;
60 }
61
62 void write(ImgFileWriter writer) {
63 if (pointRef) {
64 writer.put1u(pointIndex);
65 writer.put2u(subdivision.getNumber());
66 } else {
67 writer.put3u(label.getOffset());
68 }
69
70 int info;
71 if(region != null)
72 info = region.getIndex() & 0x3fff;
73 else
74 info = REGION_IS_COUNTRY | (country.getIndex() & 0x3fff);
75 if (pointRef)
76 info |= POINT_REF;
77
78 writer.put2u(info);
79 }
80
81 public int getIndex() {
82 if (index == -1)
83 throw new IllegalStateException("Offset not known yet.");
84 return index;
85 }
86
87 public void setIndex(int index) {
88 this.index = index;
89 }
90
91 public void setLabel(Label label) {
92 pointRef = false;
93 this.label = label;
94 }
95
96 public void setPointIndex(int pointIndex) {
97 pointRef = true;
98 this.pointIndex = pointIndex;
99 }
100
101 public void setSubdivision(Subdivision subdivision) {
102 pointRef = true;
103 this.subdivision = subdivision;
104 }
105
106 public String getName() {
107 return label == null ? "" : label.getText();
108 }
109
110 public int getLblOffset() {
111 return label == null ? 0 : label.getOffset();
112 }
113
114 public String toString() {
115 String result = "";
116 if(label != null)
117 result += label.getText();
118 if (subdivision != null)
119 result += " " + subdivision.getNumber() + "/" + pointIndex;
120 if(country != null)
121 result += " in country " + country.getIndex();
122 if(region != null)
123 result += " in region " + region.getIndex();
124
125 return result;
126 }
127
128 public int getSubdivNumber() {
129 return subdivision.getNumber();
130 }
131
132 public int getPointIndex() {
133 return pointIndex;
134 }
135
136 /**
137 * Get the region or country number.
138 * @return The region number if there is one, else the country number
139 * with a flag bit set to indicate that it is one.
140 */
141 public int getRegionCountryNumber() {
142 if (region == null) {
143 if (country != null)
144 return country.getIndex() | 0x4000;
145 } else {
146 return region.getIndex();
147 }
148 return 0;
149 }
150
151 public int getRegionNumber() {
152 return region == null? 0: region.getIndex();
153 }
154
155 public int getCountryNumber() {
156 return country == null ? 0 : country.getIndex();
157 }
158
159 public Label getLabel() {
160 return label;
161 }
162 }