01: package org.databene.region;
02:
03: import java.util.Collection;
04: import java.util.ArrayList;
05:
06: /**
07: * Simple, atomic implementation of the Region class.<br/>
08: * <br/>
09: * Created: 25.06.2006 22:29:21
10: */
11: public class BasicRegion extends Region {
12:
13: public BasicRegion(String code) {
14: super (code);
15: }
16:
17: public boolean contains(Region other) {
18: if (other == null)
19: return false;
20: if (other instanceof BasicRegion) {
21: return ((BasicRegion) other).code.startsWith(code);
22: } else if (other instanceof AggregateRegion) {
23: AggregateRegion aggregate = (AggregateRegion) other;
24: for (BasicRegion subRegion : aggregate.getBasicRegions())
25: if (!this .contains(subRegion))
26: return false;
27: return true;
28: } else
29: throw new IllegalArgumentException("Unsupported type: "
30: + other);
31: }
32:
33: public Collection<Region> countries() {
34: BasicRegion country = new BasicRegion(code.substring(0, 2));
35: ArrayList<Region> list = new ArrayList<Region>();
36: list.add(country);
37: return list;
38: }
39:
40: // java.lang.Object overrides --------------------------------------------------------------------------------------
41:
42: public String toString() {
43: return code;
44: }
45:
46: public int hashCode() {
47: return code.hashCode();
48: }
49:
50: public boolean equals(Object obj) {
51: if (this == obj)
52: return true;
53: else if (obj == null)
54: return false;
55: else if (obj instanceof BasicRegion)
56: return code.equals(((BasicRegion) obj).code);
57: else if (obj instanceof AggregateRegion) {
58: AggregateRegion aggregate = (AggregateRegion) obj;
59: if (aggregate.getBasicRegions().size() != 1)
60: return false;
61: BasicRegion basic = aggregate.getBasicRegions().iterator()
62: .next();
63: return (this .equals(basic));
64: } else
65: return false;
66: }
67: }
|