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