1616 */
1717public class Networks <T > implements Iterator <DatabaseRecord <T >> {
1818 private final Reader reader ;
19- private ArrayList <NetworkNode > nodes ;
19+ private final ArrayList <NetworkNode > nodes ;
2020 private NetworkNode lastNode ;
21- private boolean skipAliasedNetworks ;
22- private Exception err ;
23- private ByteBuffer buffer ; /* Stores the buffer for Next() calls */
21+ private final boolean skipAliasedNetworks ;
22+ private final ByteBuffer buffer ; /* Stores the buffer for Next() calls */
2423 private Class <T > typeParameterClass ;
2524
2625 /**
@@ -57,14 +56,6 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
5756 this (reader , false );
5857 }
5958
60- /**
61- * Returns if Networks had any errors.
62- * @return Exception The exception to the Networks iteration.
63- */
64- public Exception getErr () {
65- return this .err ;
66- }
67-
6859 /**
6960 * Sets the Class for the data type in DataRecord.
7061 * @param cls The class object. ( For example, Map.class )
@@ -82,10 +73,6 @@ public void setDataClass(Class<T> cls) {
8273 */
8374 @ Override
8475 public DatabaseRecord <T > next () {
85- if (this .err != null ) {
86- return null ;
87- }
88-
8976 try {
9077 T data = this .reader .resolveDataPointer (
9178 this .buffer , this .lastNode .pointer , this .typeParameterClass );
@@ -112,8 +99,7 @@ public DatabaseRecord<T> next() {
11299
113100 return new DatabaseRecord <T >(data , InetAddress .getByAddress (ip ), prefixLength );
114101 } catch (IOException e ) {
115- this .err = e ;
116- return null ;
102+ throw new NetworksIterationException (e .getMessage ());
117103 }
118104 }
119105
@@ -136,9 +122,6 @@ public boolean isInIpv4Subtree(byte[] ip) {
136122 */
137123 @ Override
138124 public boolean hasNext () {
139- if (this .err != null ) {
140- return false ;
141- }
142125 while (!this .nodes .isEmpty ()) {
143126 // Pop the last one.
144127 NetworkNode node = this .nodes .remove (this .nodes .size () - 1 );
@@ -160,8 +143,7 @@ public boolean hasNext() {
160143
161144 byte [] ipRight = Arrays .copyOf (node .ip , node .ip .length );
162145 if (ipRight .length <= (node .prefix >> 3 )) {
163- this .err = new InvalidDatabaseException ("Invalid search tree" );
164- return false ;
146+ throw new NetworksIterationException ("Invalid search tree" );
165147 }
166148
167149 ipRight [node .prefix >> 3 ] |= 1 << (7 - (node .prefix % 8 ));
@@ -173,8 +155,7 @@ public boolean hasNext() {
173155 this .nodes .add (new NetworkNode (ipRight , node .prefix , rightPointer ));
174156 node .pointer = this .reader .readNode (this .buffer , node .pointer , 0 );
175157 } catch (InvalidDatabaseException e ) {
176- this .err = e ;
177- return false ;
158+ throw new NetworksIterationException (e .getMessage ());
178159 }
179160 }
180161 }
0 commit comments