1 package org.eclipse.aether.util.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.repository.WorkspaceReader;
31 import org.eclipse.aether.repository.WorkspaceRepository;
32
33 import static java.util.Objects.requireNonNull;
34
35
36
37
38 public final class ChainedWorkspaceReader
39 implements WorkspaceReader
40 {
41
42 private final List<WorkspaceReader> readers = new ArrayList<>();
43
44 private WorkspaceRepository repository;
45
46
47
48
49
50
51
52 public ChainedWorkspaceReader( WorkspaceReader... readers )
53 {
54 if ( readers != null )
55 {
56 Collections.addAll( this.readers, readers );
57 }
58
59 StringBuilder buffer = new StringBuilder();
60 for ( WorkspaceReader reader : this.readers )
61 {
62 if ( buffer.length() > 0 )
63 {
64 buffer.append( '+' );
65 }
66 buffer.append( reader.getRepository().getContentType() );
67 }
68
69 repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) );
70 }
71
72
73
74
75
76
77
78
79
80 public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 )
81 {
82 if ( reader1 == null )
83 {
84 return reader2;
85 }
86 else if ( reader2 == null )
87 {
88 return reader1;
89 }
90 return new ChainedWorkspaceReader( reader1, reader2 );
91 }
92
93 public File findArtifact( Artifact artifact )
94 {
95 requireNonNull( artifact, "artifact cannot be null" );
96 File file = null;
97
98 for ( WorkspaceReader reader : readers )
99 {
100 file = reader.findArtifact( artifact );
101 if ( file != null )
102 {
103 break;
104 }
105 }
106
107 return file;
108 }
109
110 public List<String> findVersions( Artifact artifact )
111 {
112 requireNonNull( artifact, "artifact cannot be null" );
113 Collection<String> versions = new LinkedHashSet<>();
114
115 for ( WorkspaceReader reader : readers )
116 {
117 versions.addAll( reader.findVersions( artifact ) );
118 }
119
120 return Collections.unmodifiableList( new ArrayList<>( versions ) );
121 }
122
123 public WorkspaceRepository getRepository()
124 {
125 Key key = new Key( readers );
126 if ( !key.equals( repository.getKey() ) )
127 {
128 repository = new WorkspaceRepository( repository.getContentType(), key );
129 }
130 return repository;
131 }
132
133 private static class Key
134 {
135
136 private final List<Object> keys = new ArrayList<>();
137
138 Key( List<WorkspaceReader> readers )
139 {
140 for ( WorkspaceReader reader : readers )
141 {
142 keys.add( reader.getRepository().getKey() );
143 }
144 }
145
146 @Override
147 public boolean equals( Object obj )
148 {
149 if ( this == obj )
150 {
151 return true;
152 }
153 if ( obj == null || !getClass().equals( obj.getClass() ) )
154 {
155 return false;
156 }
157 return keys.equals( ( (Key) obj ).keys );
158 }
159
160 @Override
161 public int hashCode()
162 {
163 return keys.hashCode();
164 }
165
166 }
167
168 }