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 org.codehaus.plexus.components.io.resources.PlexusIoResource;
19  
20  import java.io.Closeable;
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.NoSuchElementException;
24  
25  abstract class ForwardingIterator
26      implements Iterator<PlexusIoResource>, Closeable
27  {
28      private final Object possiblyCloseable;
29  
30      private PlexusIoResource next = null;
31  
32      ForwardingIterator( Object possiblyCloseable )
33      {
34          this.possiblyCloseable = possiblyCloseable;
35      }
36  
37      public boolean hasNext()
38      {
39          if ( next == null )
40          {
41              try
42              {
43                  next = getNextResource();
44              }
45              catch ( IOException e )
46              {
47                  throw new RuntimeException( e );
48              }
49          }
50          return next != null;
51      }
52  
53      public PlexusIoResource next()
54      {
55          if ( !hasNext() )
56          {
57              throw new NoSuchElementException();
58          }
59          PlexusIoResource ret = next;
60          next = null;
61          return ret;
62      }
63  
64      public void remove()
65      {
66          throw new UnsupportedOperationException();
67      }
68  
69      public void close()
70          throws IOException
71      {
72          if ( possiblyCloseable instanceof Closeable )
73          {
74              ( (Closeable) possiblyCloseable ).close();
75          }
76  
77      }
78  
79      /**
80       * Returns the next resource or null if no next resource;
81       */
82      protected abstract PlexusIoResource getNextResource() throws IOException;
83  }