001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Iterator;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.repository.ArtifactRepository;
027import org.eclipse.aether.repository.LocalRepository;
028import org.eclipse.aether.repository.RemoteRepository;
029import org.eclipse.aether.repository.RepositoryPolicy;
030import org.eclipse.aether.repository.WorkspaceReader;
031import org.eclipse.aether.repository.WorkspaceRepository;
032
033/**
034 * @deprecated To be deleted without replacement.
035 */
036@Deprecated
037public final class CacheUtils
038{
039
040    public static <T> boolean eq( T s1, T s2 )
041    {
042        return s1 != null ? s1.equals( s2 ) : s2 == null;
043    }
044
045    public static int hash( Object obj )
046    {
047        return obj != null ? obj.hashCode() : 0;
048    }
049
050    public static int repositoriesHashCode( List<RemoteRepository> repositories )
051    {
052        int result = 17;
053        for ( RemoteRepository repository : repositories )
054        {
055            result = 31 * result + repositoryHashCode( repository );
056        }
057        return result;
058    }
059
060    private static int repositoryHashCode( RemoteRepository repository )
061    {
062        int result = 17;
063        result = 31 * result + hash( repository.getUrl() );
064        return result;
065    }
066
067    private static boolean repositoryEquals( RemoteRepository r1, RemoteRepository r2 )
068    {
069        if ( r1 == r2 )
070        {
071            return true;
072        }
073
074        return eq( r1.getId(), r2.getId() ) && eq( r1.getUrl(), r2.getUrl() )
075            && policyEquals( r1.getPolicy( false ), r2.getPolicy( false ) )
076            && policyEquals( r1.getPolicy( true ), r2.getPolicy( true ) );
077    }
078
079    private static boolean policyEquals( RepositoryPolicy p1, RepositoryPolicy p2 )
080    {
081        if ( p1 == p2 )
082        {
083            return true;
084        }
085        // update policy doesn't affect contents
086        return p1.isEnabled() == p2.isEnabled() && eq( p1.getChecksumPolicy(), p2.getChecksumPolicy() );
087    }
088
089    public static boolean repositoriesEquals( List<RemoteRepository> r1, List<RemoteRepository> r2 )
090    {
091        if ( r1.size() != r2.size() )
092        {
093            return false;
094        }
095
096        for ( Iterator<RemoteRepository> it1 = r1.iterator(), it2 = r2.iterator(); it1.hasNext(); )
097        {
098            if ( !repositoryEquals( it1.next(), it2.next() ) )
099            {
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}