001package org.eclipse.aether.util.repository;
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.io.File;
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.LinkedHashSet;
027import java.util.List;
028
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.repository.WorkspaceReader;
031import org.eclipse.aether.repository.WorkspaceRepository;
032
033/**
034 * A workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
035 */
036public final class ChainedWorkspaceReader
037    implements WorkspaceReader
038{
039
040    private List<WorkspaceReader> readers = new ArrayList<WorkspaceReader>();
041
042    private WorkspaceRepository repository;
043
044    /**
045     * Creates a new workspace reader by chaining the specified readers.
046     * 
047     * @param readers The readers to chain, may be {@code null}.
048     * @see #newInstance(WorkspaceReader, WorkspaceReader)
049     */
050    public ChainedWorkspaceReader( WorkspaceReader... readers )
051    {
052        if ( readers != null )
053        {
054            Collections.addAll( this.readers, readers );
055        }
056
057        StringBuilder buffer = new StringBuilder();
058        for ( WorkspaceReader reader : this.readers )
059        {
060            if ( buffer.length() > 0 )
061            {
062                buffer.append( '+' );
063            }
064            buffer.append( reader.getRepository().getContentType() );
065        }
066
067        repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) );
068    }
069
070    /**
071     * Creates a new workspace reader by chaining the specified readers. In contrast to the constructor, this factory
072     * method will avoid creating an actual chained reader if one of the specified readers is actually {@code null}.
073     * 
074     * @param reader1 The first workspace reader, may be {@code null}.
075     * @param reader2 The second workspace reader, may be {@code null}.
076     * @return The chained reader or {@code null} if no workspace reader was supplied.
077     */
078    public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 )
079    {
080        if ( reader1 == null )
081        {
082            return reader2;
083        }
084        else if ( reader2 == null )
085        {
086            return reader1;
087        }
088        return new ChainedWorkspaceReader( reader1, reader2 );
089    }
090
091    public File findArtifact( Artifact artifact )
092    {
093        File file = null;
094
095        for ( WorkspaceReader reader : readers )
096        {
097            file = reader.findArtifact( artifact );
098            if ( file != null )
099            {
100                break;
101            }
102        }
103
104        return file;
105    }
106
107    public List<String> findVersions( Artifact artifact )
108    {
109        Collection<String> versions = new LinkedHashSet<String>();
110
111        for ( WorkspaceReader reader : readers )
112        {
113            versions.addAll( reader.findVersions( artifact ) );
114        }
115
116        return Collections.unmodifiableList( new ArrayList<String>( versions ) );
117    }
118
119    public WorkspaceRepository getRepository()
120    {
121        Key key = new Key( readers );
122        if ( !key.equals( repository.getKey() ) )
123        {
124            repository = new WorkspaceRepository( repository.getContentType(), key );
125        }
126        return repository;
127    }
128
129    private static class Key
130    {
131
132        private final List<Object> keys = new ArrayList<Object>();
133
134        Key( List<WorkspaceReader> readers )
135        {
136            for ( WorkspaceReader reader : readers )
137            {
138                keys.add( reader.getRepository().getKey() );
139            }
140        }
141
142        @Override
143        public boolean equals( Object obj )
144        {
145            if ( this == obj )
146            {
147                return true;
148            }
149            if ( obj == null || !getClass().equals( obj.getClass() ) )
150            {
151                return false;
152            }
153            return keys.equals( ( (Key) obj ).keys );
154        }
155
156        @Override
157        public int hashCode()
158        {
159            return keys.hashCode();
160        }
161
162    }
163
164}