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
34
35
36 public final class ChainedWorkspaceReader
37 implements WorkspaceReader
38 {
39
40 private List<WorkspaceReader> readers = new ArrayList<>();
41
42 private WorkspaceRepository repository;
43
44
45
46
47
48
49
50 public ChainedWorkspaceReader( WorkspaceReader... readers )
51 {
52 if ( readers != null )
53 {
54 Collections.addAll( this.readers, readers );
55 }
56
57 StringBuilder buffer = new StringBuilder();
58 for ( WorkspaceReader reader : this.readers )
59 {
60 if ( buffer.length() > 0 )
61 {
62 buffer.append( '+' );
63 }
64 buffer.append( reader.getRepository().getContentType() );
65 }
66
67 repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) );
68 }
69
70
71
72
73
74
75
76
77
78 public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 )
79 {
80 if ( reader1 == null )
81 {
82 return reader2;
83 }
84 else if ( reader2 == null )
85 {
86 return reader1;
87 }
88 return new ChainedWorkspaceReader( reader1, reader2 );
89 }
90
91 public File findArtifact( Artifact artifact )
92 {
93 File file = null;
94
95 for ( WorkspaceReader reader : readers )
96 {
97 file = reader.findArtifact( artifact );
98 if ( file != null )
99 {
100 break;
101 }
102 }
103
104 return file;
105 }
106
107 public List<String> findVersions( Artifact artifact )
108 {
109 Collection<String> versions = new LinkedHashSet<>();
110
111 for ( WorkspaceReader reader : readers )
112 {
113 versions.addAll( reader.findVersions( artifact ) );
114 }
115
116 return Collections.unmodifiableList( new ArrayList<>( 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<>();
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 }