View Javadoc
1   package org.eclipse.aether.internal.impl.collect;
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      private final RepositorySystemSession session;
45  
46      private Dependency dependency;
47  
48      VersionRangeResult result;
49  
50      int count;
51  
52      byte[] deleted = new byte[64];
53  
54      DefaultVersionFilterContext( RepositorySystemSession session )
55      {
56          this.session = session;
57      }
58  
59      public void set( Dependency dependency, VersionRangeResult result )
60      {
61          this.dependency = dependency;
62          this.result = result;
63          count = result.getVersions().size();
64          if ( deleted.length < count )
65          {
66              deleted = new byte[count];
67          }
68          else
69          {
70              for ( int i = count - 1; i >= 0; i-- )
71              {
72                  deleted[i] = (byte) 0;
73              }
74          }
75      }
76  
77      public List<Version> get()
78      {
79          if ( count == result.getVersions().size() )
80          {
81              return result.getVersions();
82          }
83          if ( count <= 1 )
84          {
85              if ( count <= 0 )
86              {
87                  return Collections.emptyList();
88              }
89              return Collections.singletonList( iterator().next() );
90          }
91          List<Version> versions = new ArrayList<>( count );
92          for ( Version version : this )
93          {
94              versions.add( version );
95          }
96          return versions;
97      }
98  
99      public RepositorySystemSession getSession()
100     {
101         return session;
102     }
103 
104     public Dependency getDependency()
105     {
106         return dependency;
107     }
108 
109     public VersionConstraint getVersionConstraint()
110     {
111         return result.getVersionConstraint();
112     }
113 
114     public int getCount()
115     {
116         return count;
117     }
118 
119     public ArtifactRepository getRepository( Version version )
120     {
121         return result.getRepository( version );
122     }
123 
124     public List<RemoteRepository> getRepositories()
125     {
126         return Collections.unmodifiableList( result.getRequest().getRepositories() );
127     }
128 
129     public Iterator<Version> iterator()
130     {
131         return ( count > 0 ) ? new VersionIterator() : Collections.<Version>emptySet().iterator();
132     }
133 
134     @Override
135     public String toString()
136     {
137         return dependency + " " + result.getVersions();
138     }
139 
140     private class VersionIterator
141         implements Iterator<Version>
142     {
143 
144         private final List<Version> versions;
145 
146         private final int size;
147 
148         private int count;
149 
150         private int index;
151 
152         private int next;
153 
154         VersionIterator()
155         {
156             count = DefaultVersionFilterContext.this.count;
157             index = -1;
158             next = 0;
159             versions = result.getVersions();
160             size = versions.size();
161             advance();
162         }
163 
164         @SuppressWarnings( "StatementWithEmptyBody" )
165         private void advance()
166         {
167             for ( next = index + 1; next < size && deleted[next] != (byte) 0; next++ )
168             {
169                 // just advancing index
170             }
171         }
172 
173         public boolean hasNext()
174         {
175             return next < size;
176         }
177 
178         public Version next()
179         {
180             if ( count != DefaultVersionFilterContext.this.count )
181             {
182                 throw new ConcurrentModificationException();
183             }
184             if ( next >= size )
185             {
186                 throw new NoSuchElementException();
187             }
188             index = next;
189             advance();
190             return versions.get( index );
191         }
192 
193         public void remove()
194         {
195             if ( count != DefaultVersionFilterContext.this.count )
196             {
197                 throw new ConcurrentModificationException();
198             }
199             if ( index < 0 || deleted[index] == (byte) 1 )
200             {
201                 throw new IllegalStateException();
202             }
203             deleted[index] = (byte) 1;
204             count = --DefaultVersionFilterContext.this.count;
205         }
206 
207         @Override
208         public String toString()
209         {
210             return ( index < 0 ) ? "null" : String.valueOf( versions.get( index ) );
211         }
212 
213     }
214 
215 }