View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.ConcurrentModificationException;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.NoSuchElementException;
28  
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.collection.VersionFilter;
31  import org.eclipse.aether.graph.Dependency;
32  import org.eclipse.aether.repository.ArtifactRepository;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.resolution.VersionRangeResult;
35  import org.eclipse.aether.version.Version;
36  import org.eclipse.aether.version.VersionConstraint;
37  
38  /**
39   * @see DefaultDependencyCollector
40   */
41  final class DefaultVersionFilterContext
42      implements VersionFilter.VersionFilterContext
43  {
44  
45      private final Iterator<Version> EMPTY = Collections.<Version>emptySet().iterator();
46  
47      private final RepositorySystemSession session;
48  
49      private Dependency dependency;
50  
51      VersionRangeResult result;
52  
53      int count;
54  
55      byte[] deleted = new byte[64];
56  
57      DefaultVersionFilterContext( RepositorySystemSession session )
58      {
59          this.session = session;
60      }
61  
62      public void set( Dependency dependency, VersionRangeResult result )
63      {
64          this.dependency = dependency;
65          this.result = result;
66          count = result.getVersions().size();
67          if ( deleted.length < count )
68          {
69              deleted = new byte[count];
70          }
71          else
72          {
73              for ( int i = count - 1; i >= 0; i-- )
74              {
75                  deleted[i] = (byte) 0;
76              }
77          }
78      }
79  
80      public List<Version> get()
81      {
82          if ( count == result.getVersions().size() )
83          {
84              return result.getVersions();
85          }
86          if ( count <= 1 )
87          {
88              if ( count <= 0 )
89              {
90                  return Collections.emptyList();
91              }
92              return Collections.singletonList( iterator().next() );
93          }
94          List<Version> versions = new ArrayList<Version>( count );
95          for ( Version version : this )
96          {
97              versions.add( version );
98          }
99          return versions;
100     }
101 
102     public RepositorySystemSession getSession()
103     {
104         return session;
105     }
106 
107     public Dependency getDependency()
108     {
109         return dependency;
110     }
111 
112     public VersionConstraint getVersionConstraint()
113     {
114         return result.getVersionConstraint();
115     }
116 
117     public int getCount()
118     {
119         return count;
120     }
121 
122     public ArtifactRepository getRepository( Version version )
123     {
124         return result.getRepository( version );
125     }
126 
127     public List<RemoteRepository> getRepositories()
128     {
129         return Collections.unmodifiableList( result.getRequest().getRepositories() );
130     }
131 
132     public Iterator<Version> iterator()
133     {
134         return ( count > 0 ) ? new VersionIterator() : EMPTY;
135     }
136 
137     @Override
138     public String toString()
139     {
140         return dependency + " " + result.getVersions();
141     }
142 
143     private class VersionIterator
144         implements Iterator<Version>
145     {
146 
147         private final List<Version> versions;
148 
149         private final int size;
150 
151         private int count;
152 
153         private int index;
154 
155         private int next;
156 
157         VersionIterator()
158         {
159             count = DefaultVersionFilterContext.this.count;
160             index = -1;
161             next = 0;
162             versions = result.getVersions();
163             size = versions.size();
164             advance();
165         }
166 
167         private void advance()
168         {
169             for ( next = index + 1; next < size && deleted[next] != (byte) 0; next++ )
170             {
171                 // just advancing index
172             }
173         }
174 
175         public boolean hasNext()
176         {
177             return next < size;
178         }
179 
180         public Version next()
181         {
182             if ( count != DefaultVersionFilterContext.this.count )
183             {
184                 throw new ConcurrentModificationException();
185             }
186             if ( next >= size )
187             {
188                 throw new NoSuchElementException();
189             }
190             index = next;
191             advance();
192             return versions.get( index );
193         }
194 
195         public void remove()
196         {
197             if ( count != DefaultVersionFilterContext.this.count )
198             {
199                 throw new ConcurrentModificationException();
200             }
201             if ( index < 0 || deleted[index] == (byte) 1 )
202             {
203                 throw new IllegalStateException();
204             }
205             deleted[index] = (byte) 1;
206             count = --DefaultVersionFilterContext.this.count;
207         }
208 
209         @Override
210         public String toString()
211         {
212             return ( index < 0 ) ? "null" : String.valueOf( versions.get( index ) );
213         }
214 
215     }
216 
217 }