View Javadoc
1   /*
2    * Copyright 2014 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.components.io.resources.proxy;
17  
18  import java.io.Closeable;
19  import java.io.IOException;
20  import java.util.Iterator;
21  import java.util.NoSuchElementException;
22  
23  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
24  
25  abstract class ForwardingIterator implements Iterator<PlexusIoResource>, Closeable {
26      private final Object possiblyCloseable;
27  
28      private PlexusIoResource next = null;
29  
30      ForwardingIterator(Object possiblyCloseable) {
31          this.possiblyCloseable = possiblyCloseable;
32      }
33  
34      public boolean hasNext() {
35          if (next == null) {
36              try {
37                  next = getNextResource();
38              } catch (IOException e) {
39                  throw new RuntimeException(e);
40              }
41          }
42          return next != null;
43      }
44  
45      public PlexusIoResource next() {
46          if (!hasNext()) {
47              throw new NoSuchElementException();
48          }
49          PlexusIoResource ret = next;
50          next = null;
51          return ret;
52      }
53  
54      public void remove() {
55          throw new UnsupportedOperationException();
56      }
57  
58      public void close() throws IOException {
59          if (possiblyCloseable instanceof Closeable) {
60              ((Closeable) possiblyCloseable).close();
61          }
62      }
63  
64      /**
65       * Returns the next resource or null if no next resource;
66       */
67      protected abstract PlexusIoResource getNextResource() throws IOException;
68  }