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.Iterator;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.repository.ArtifactRepository;
27  import org.eclipse.aether.repository.LocalRepository;
28  import org.eclipse.aether.repository.RemoteRepository;
29  import org.eclipse.aether.repository.RepositoryPolicy;
30  import org.eclipse.aether.repository.WorkspaceReader;
31  import org.eclipse.aether.repository.WorkspaceRepository;
32  
33  /**
34   * @deprecated To be deleted without replacement.
35   */
36  @Deprecated
37  public final class CacheUtils
38  {
39  
40      public static <T> boolean eq( T s1, T s2 )
41      {
42          return s1 != null ? s1.equals( s2 ) : s2 == null;
43      }
44  
45      public static int hash( Object obj )
46      {
47          return obj != null ? obj.hashCode() : 0;
48      }
49  
50      public static int repositoriesHashCode( List<RemoteRepository> repositories )
51      {
52          int result = 17;
53          for ( RemoteRepository repository : repositories )
54          {
55              result = 31 * result + repositoryHashCode( repository );
56          }
57          return result;
58      }
59  
60      private static int repositoryHashCode( RemoteRepository repository )
61      {
62          int result = 17;
63          result = 31 * result + hash( repository.getUrl() );
64          return result;
65      }
66  
67      private static boolean repositoryEquals( RemoteRepository r1, RemoteRepository r2 )
68      {
69          if ( r1 == r2 )
70          {
71              return true;
72          }
73  
74          return eq( r1.getId(), r2.getId() ) && eq( r1.getUrl(), r2.getUrl() )
75              && policyEquals( r1.getPolicy( false ), r2.getPolicy( false ) )
76              && policyEquals( r1.getPolicy( true ), r2.getPolicy( true ) );
77      }
78  
79      private static boolean policyEquals( RepositoryPolicy p1, RepositoryPolicy p2 )
80      {
81          if ( p1 == p2 )
82          {
83              return true;
84          }
85          // update policy doesn't affect contents
86          return p1.isEnabled() == p2.isEnabled() && eq( p1.getChecksumPolicy(), p2.getChecksumPolicy() );
87      }
88  
89      public static boolean repositoriesEquals( List<RemoteRepository> r1, List<RemoteRepository> r2 )
90      {
91          if ( r1.size() != r2.size() )
92          {
93              return false;
94          }
95  
96          for ( Iterator<RemoteRepository> it1 = r1.iterator(), it2 = r2.iterator(); it1.hasNext(); )
97          {
98              if ( !repositoryEquals( it1.next(), it2.next() ) )
99              {
100                 return false;
101             }
102         }
103 
104         return true;
105     }
106 
107     public static WorkspaceRepository getWorkspace( RepositorySystemSession session )
108     {
109         WorkspaceReader reader = session.getWorkspaceReader();
110         return ( reader != null ) ? reader.getRepository() : null;
111     }
112 
113     public static ArtifactRepository getRepository( RepositorySystemSession session,
114                                                     List<RemoteRepository> repositories, Class<?> repoClass,
115                                                     String repoId )
116     {
117         if ( repoClass != null )
118         {
119             if ( WorkspaceRepository.class.isAssignableFrom( repoClass ) )
120             {
121                 return session.getWorkspaceReader().getRepository();
122             }
123             else if ( LocalRepository.class.isAssignableFrom( repoClass ) )
124             {
125                 return session.getLocalRepository();
126             }
127             else
128             {
129                 for ( RemoteRepository repository : repositories )
130                 {
131                     if ( repoId.equals( repository.getId() ) )
132                     {
133                         return repository;
134                     }
135                 }
136             }
137         }
138         return null;
139     }
140 
141 }