3434class SubArea {
3535 private static final Logger LOG = LoggerFactory .getLogger (SubArea .class );
3636
37- private static final ArrayList <SubArea > subAreas = new ArrayList <>();
38- private static final TreeMap <Integer , ArrayList <SubArea >> lonMap = new TreeMap <>();
39- private static final TreeMap <Integer , ArrayList <SubArea >> latMap = new TreeMap <>();
37+ private static final int SUB_AREAS_INITIAL_CAPACITY = 16250 ;
4038
41- private static final Range <Integer > latBoundingRange =
42- new Range <>(Point .LAT_MICRODEG_MIN , Point .LAT_MICRODEG_MAX );
43- private static final Range <Integer > lonBoundingRange =
44- new Range <>(Point .LON_MICRODEG_MIN , Point .LON_MICRODEG_MAX );
39+ private static final List <SubArea > SUB_AREAS = new ArrayList <>(SUB_AREAS_INITIAL_CAPACITY );
40+ private static final TreeMap <Integer , ArrayList <SubArea >> LON_MAP = new TreeMap <>();
41+ private static final TreeMap <Integer , ArrayList <SubArea >> LAT_MAP = new TreeMap <>();
42+
43+ private static final Range <Integer > LAT_BOUNDING_RANGE = new Range <>(Point .LAT_MICRODEG_MIN , Point .LAT_MICRODEG_MAX );
44+ private static final Range <Integer > LON_BOUNDING_RANGE = new Range <>(Point .LON_MICRODEG_MIN , Point .LON_MICRODEG_MAX );
4545
4646 static {
47+ LOG .info ("SubArea: Initialize sub-areas for {} territories" , Territory .values ().length );
4748 for (final Territory territory : Territory .values ()) {
4849 final int territoryCode = territory .getCode ();
4950 final int first = DataAccess .dataFirstRecord (territoryCode );
5051 final int last = DataAccess .dataLastRecord (territoryCode );
5152
5253 // Add a number sub areas.
53- for (int i = subAreas .size (); i <= last ; i ++) {
54- subAreas .add (null );
54+ for (int i = SUB_AREAS .size (); i <= last ; i ++) {
55+ SUB_AREAS .add (null );
5556 }
5657 for (int i = last ; i >= first ; i --) {
57- final SubArea newSubArea = new SubArea (i , territory , subAreas .get (last ));
58- subAreas .set (i , newSubArea );
58+ final SubArea newSubArea = new SubArea (i , territory , SUB_AREAS .get (last ));
59+ SUB_AREAS .set (i , newSubArea );
5960
6061 if ((newSubArea .boundedLatRange == null ) || (newSubArea .boundedLonRange == null )) {
6162 continue ;
6263 }
6364
6465 for (final Range <Integer > longitudeRange : newSubArea .boundedLonRange ) {
65- if (!lonMap .containsKey (longitudeRange .getMin ())) {
66- lonMap .put (longitudeRange .getMin (), new ArrayList <SubArea >());
66+ if (!LON_MAP .containsKey (longitudeRange .getMin ())) {
67+ LON_MAP .put (longitudeRange .getMin (), new ArrayList <SubArea >());
6768 }
68- if (!lonMap .containsKey (longitudeRange .getMax ())) {
69- lonMap .put (longitudeRange .getMax (), new ArrayList <SubArea >());
69+ if (!LON_MAP .containsKey (longitudeRange .getMax ())) {
70+ LON_MAP .put (longitudeRange .getMax (), new ArrayList <SubArea >());
7071 }
7172 }
7273
7374 for (final Range <Integer > latitudeRange : newSubArea .boundedLatRange ) {
74- if (!latMap .containsKey (latitudeRange .getMin ())) {
75- latMap .put (latitudeRange .getMin (), new ArrayList <SubArea >());
75+ if (!LAT_MAP .containsKey (latitudeRange .getMin ())) {
76+ LAT_MAP .put (latitudeRange .getMin (), new ArrayList <SubArea >());
7677 }
77- if (!latMap .containsKey (latitudeRange .getMax ())) {
78- latMap .put (latitudeRange .getMax (), new ArrayList <SubArea >());
78+ if (!LAT_MAP .containsKey (latitudeRange .getMax ())) {
79+ LAT_MAP .put (latitudeRange .getMax (), new ArrayList <SubArea >());
7980 }
8081 }
8182 }
8283 }
83- for (final SubArea subArea : subAreas ) {
84+ LOG .info ("SubArea: Created {} sub-areas" , SUB_AREAS .size ());
85+ for (final SubArea subArea : SUB_AREAS ) {
8486 if ((subArea .boundedLatRange == null ) || (subArea .boundedLonRange == null )) {
8587 continue ;
8688 }
8789 SortedMap <Integer , ArrayList <SubArea >> subMap ;
8890
8991 for (final Range <Integer > longitudeRange : subArea .boundedLonRange ) {
90- subMap = lonMap .subMap (longitudeRange .getMin (), longitudeRange .getMax () + 1 );
92+ subMap = LON_MAP .subMap (longitudeRange .getMin (), longitudeRange .getMax () + 1 );
9193 for (final ArrayList <SubArea > areaList : subMap .values ()) {
9294 areaList .add (subArea );
9395 }
9496 }
9597
9698 for (final Range <Integer > latitudeRange : subArea .boundedLatRange ) {
97- subMap = latMap .subMap (latitudeRange .getMin (), latitudeRange .getMax () + 1 );
99+ subMap = LAT_MAP .subMap (latitudeRange .getMin (), latitudeRange .getMax () + 1 );
98100 for (final ArrayList <SubArea > areaList : subMap .values ()) {
99101 areaList .add (subArea );
100102 }
101103 }
102104 }
103- LOG .debug ("SubArea (init): lat =[{}, {}], lon=[{}, {}]" ,
104- Point .microDegToDeg (latMap .firstKey ()), Point .microDegToDeg (latMap .lastKey ()),
105- Point .microDegToDeg (lonMap .firstKey ()), Point .microDegToDeg (lonMap .lastKey ()));
105+ LOG .info ("SubArea: sub-areas initialized: aslat =[{}, {}], lon=[{}, {}]" ,
106+ Point .microDegToDeg (LAT_MAP .firstKey ()), Point .microDegToDeg (LAT_MAP .lastKey ()),
107+ Point .microDegToDeg (LON_MAP .firstKey ()), Point .microDegToDeg (LON_MAP .lastKey ()));
106108 }
107109
108110 static SubArea getArea (final int i ) {
109- return subAreas .get (i );
111+ return SUB_AREAS .get (i );
110112 }
111113
112114
@@ -115,19 +117,19 @@ static SubArea getArea(final int i) {
115117 static List <SubArea > getAreasForPoint (@ Nonnull final Point point ) {
116118 final ArrayList <ArrayList <SubArea >> areaLists = new ArrayList <>();
117119 ArrayList <SubArea > list ;
118- list = latMap .get (point .getLatMicroDeg ());
120+ list = LAT_MAP .get (point .getLatMicroDeg ());
119121
120122 if (list != null ) {
121123 areaLists .add (list );
122124 } else {
123- Entry <Integer , ArrayList <SubArea >> entry = latMap .lowerEntry (point .getLatMicroDeg ());
125+ Entry <Integer , ArrayList <SubArea >> entry = LAT_MAP .lowerEntry (point .getLatMicroDeg ());
124126 if (entry == null ) {
125127 return Collections .EMPTY_LIST ;
126128 }
127129 list = entry .getValue ();
128130 assert list != null ;
129131 areaLists .add (list );
130- entry = latMap .higherEntry (point .getLatMicroDeg ());
132+ entry = LAT_MAP .higherEntry (point .getLatMicroDeg ());
131133 if (entry == null ) {
132134 return Collections .EMPTY_LIST ;
133135 }
@@ -136,18 +138,18 @@ static List<SubArea> getAreasForPoint(@Nonnull final Point point) {
136138 areaLists .add (list );
137139 }
138140
139- list = lonMap .get (point .getLonMicroDeg ());
141+ list = LON_MAP .get (point .getLonMicroDeg ());
140142 if (list != null ) {
141143 areaLists .add (list );
142144 } else {
143- Entry <Integer , ArrayList <SubArea >> entry = lonMap .lowerEntry (point .getLonMicroDeg ());
145+ Entry <Integer , ArrayList <SubArea >> entry = LON_MAP .lowerEntry (point .getLonMicroDeg ());
144146 if (entry == null ) {
145147 return Collections .EMPTY_LIST ;
146148 }
147149 list = entry .getValue ();
148150 assert list != null ;
149151 areaLists .add (list );
150- entry = lonMap .higherEntry (point .getLonMicroDeg ());
152+ entry = LON_MAP .higherEntry (point .getLonMicroDeg ());
151153 if (entry == null ) {
152154 return Collections .EMPTY_LIST ;
153155 }
@@ -161,14 +163,13 @@ static List<SubArea> getAreasForPoint(@Nonnull final Point point) {
161163
162164 mainLoop :
163165 for (final SubArea subArea : list ) {
164- for (int i = 1 ; i < areaLists . size (); i ++ ) {
165- if (!areaLists . get ( i ) .contains (subArea )) {
166+ for (final ArrayList < SubArea > subAreas : areaLists ) {
167+ if (!subAreas .contains (subArea )) {
166168 continue mainLoop ;
167169 }
168170 }
169171 result .add (subArea );
170172 }
171-
172173 return result ;
173174 }
174175
@@ -219,8 +220,8 @@ private SubArea(final int i, @Nonnull final Territory territory, @Nullable final
219220 if (latRange .getMax () != 90000000 ) {
220221 trimmedLatRange = trimRange (latRange );
221222 }
222- final ArrayList <Range <Integer >> normalisedLonRange = normaliseRange (trimmedLonRange , lonBoundingRange );
223- final ArrayList <Range <Integer >> normalisedLatRange = normaliseRange (trimmedLatRange , latBoundingRange );
223+ final ArrayList <Range <Integer >> normalisedLonRange = normaliseRange (trimmedLonRange , LON_BOUNDING_RANGE );
224+ final ArrayList <Range <Integer >> normalisedLatRange = normaliseRange (trimmedLatRange , LAT_BOUNDING_RANGE );
224225 if (territoryBounds == null ) {
225226 boundedLonRange = normalisedLonRange ;
226227 boundedLatRange = normalisedLatRange ;
0 commit comments