01: package org.databene.region;
02:
03: import java.util.*;
04:
05: /**
06: * Created: 25.06.2006 22:46:09
07: * @deprecated non-final version
08: */
09: public class AggregateRegion extends Region {
10:
11: private Map<String, BasicRegion> basicRegions;
12:
13: public AggregateRegion(String code) {
14: super (code);
15: basicRegions = new HashMap<String, BasicRegion>();
16: }
17:
18: public void add(Region region) {
19: if (region instanceof BasicRegion)
20: basicRegions.put(region.getCode(), (BasicRegion) region);
21: else if (region instanceof AggregateRegion)
22: add(((AggregateRegion) region).getBasicRegions());
23: else
24: throw new UnsupportedOperationException(
25: "add() not supported for: ");
26: }
27:
28: public void add(Collection<? extends Region> regions) {
29: for (Region region : regions)
30: add(region);
31: }
32:
33: public Collection<BasicRegion> getBasicRegions() {
34: return basicRegions.values();
35: }
36:
37: public boolean contains(Region region) {
38: if (region instanceof BasicRegion)
39: return basicRegions.containsValue(region);
40: else if (region instanceof AggregateRegion) {
41: Collection<BasicRegion> regions = ((AggregateRegion) region)
42: .getBasicRegions();
43: for (BasicRegion basicRegion : regions)
44: if (!this .basicRegions.containsValue(basicRegion))
45: return false;
46: return true;
47: } else
48: throw new IllegalArgumentException("Unsupported type: "
49: + region);
50: }
51:
52: public Collection countries() {
53: Map<String, Region> countries = new HashMap<String, Region>();
54: for (BasicRegion region : basicRegions.values())
55: countries.put(region.getCode().substring(0, 2), region);
56: return countries.values();
57: }
58:
59: // java.lang.Object overrides --------------------------------------------------------------------------------------
60:
61: public String toString() {
62: return code;
63: }
64:
65: public boolean equals(Object o) {
66: if (this == o)
67: return true;
68: if (o instanceof BasicRegion)
69: return (o).equals(this );
70: if (o instanceof AggregateRegion) {
71: Collection<BasicRegion> oc = ((AggregateRegion) o)
72: .getBasicRegions();
73: if (basicRegions.size() != oc.size())
74: return false;
75: for (BasicRegion or : oc) {
76: if (!basicRegions.containsValue(or))
77: return false;
78: }
79: return true;
80: }
81: return false;
82: }
83:
84: public int hashCode() {
85: if (basicRegions.size() == 1)
86: return basicRegions.values().iterator().next().hashCode();
87: return (basicRegions != null ? basicRegions.hashCode() : 0);
88: }
89: }
|