View Javadoc

1   package com.terradue.jcatalogue.client.internal.collections;
2   
3   /*
4    *    Copyright 2011-2012 Terradue srl
5    *
6    *    Licensed under the Apache License, Version 2.0 (the "License");
7    *    you may not use this file except in compliance with the License.
8    *    You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *    Unless required by applicable law or agreed to in writing, software
13   *    distributed under the License is distributed on an "AS IS" BASIS,
14   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *    See the License for the specific language governing permissions and
16   *    limitations under the License.
17   */
18  
19  import java.net.URI;
20  import java.util.Comparator;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.NoSuchElementException;
25  import java.util.Set;
26  import java.util.TreeMap;
27  
28  public final class Enclosures
29      implements Comparator<Integer>, Iterable<URI>
30  {
31  
32      private final Map<Integer, Set<URI>> enclosures = new TreeMap<Integer, Set<URI>>( this );
33  
34      public void addEnclosure( URI uri, Integer priority )
35      {
36          Set<URI> enclosuresSet = enclosures.get( priority );
37  
38          if ( enclosuresSet == null )
39          {
40              enclosuresSet = new HashSet<URI>();
41              enclosures.put( priority, enclosuresSet );
42          }
43  
44          enclosuresSet.add( uri );
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public Iterator<URI> iterator()
52      {
53          return new Iterator<URI>()
54          {
55  
56              private Iterator<Integer> keys = enclosures.keySet().iterator();
57  
58              private Iterator<URI> pending = null;
59  
60              private URI next = null;
61  
62              @Override
63              public boolean hasNext()
64              {
65                  if ( next != null )
66                  {
67                      return true;
68                  }
69  
70                  while ( ( pending == null ) || !pending.hasNext() )
71                  {
72                      if ( !keys.hasNext() )
73                      {
74                          return false;
75                      }
76                      pending = enclosures.get( keys.next() ).iterator();
77                  }
78  
79                  next = pending.next();
80                  return true;
81              }
82  
83              @Override
84              public URI next()
85              {
86                  if ( !hasNext() )
87                  {
88                      throw new NoSuchElementException();
89                  }
90                  URI returned = next;
91                  next = null;
92                  return returned;
93              }
94  
95              @Override
96              public void remove()
97              {
98                  pending.remove();
99              }
100 
101         };
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public int compare( Integer o1, Integer o2 )
109     {
110         return o1.compareTo( o2 );
111     }
112 
113 }