1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.search.backend.remoterepository;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.maven.search.api.MAVEN;
25 import org.apache.maven.search.api.SearchRequest;
26 import org.apache.maven.search.api.request.BooleanQuery;
27 import org.apache.maven.search.api.request.Field;
28 import org.apache.maven.search.api.request.FieldQuery;
29 import org.apache.maven.search.api.request.Query;
30
31 import static java.util.Objects.requireNonNull;
32
33
34
35
36 public final class Context {
37 private final SearchRequest searchRequest;
38
39 private final Map<Field, String> fields;
40
41 public Context(SearchRequest searchRequest) {
42 this.searchRequest = requireNonNull(searchRequest);
43 this.fields = new HashMap<>();
44 populateFields(searchRequest.getQuery());
45 }
46
47 private void populateFields(Query query) {
48 if (query instanceof BooleanQuery) {
49 populateFields(((BooleanQuery) query).getLeft());
50 populateFields(((BooleanQuery) query).getRight());
51 } else if (query instanceof FieldQuery) {
52 fields.put(((FieldQuery) query).getField(), query.getValue());
53 } else {
54 throw new IllegalArgumentException("Unsupported Query type: " + query.getClass());
55 }
56 }
57
58 public SearchRequest getSearchRequest() {
59 return searchRequest;
60 }
61
62 public String getFieldValue(Field field) {
63 return fields.get(field);
64 }
65
66 public String getGroupId() {
67 return getFieldValue(MAVEN.GROUP_ID);
68 }
69
70 public String getArtifactId() {
71 return getFieldValue(MAVEN.ARTIFACT_ID);
72 }
73
74 public String getVersion() {
75 return getFieldValue(MAVEN.VERSION);
76 }
77
78 public String getClassifier() {
79 return getFieldValue(MAVEN.CLASSIFIER);
80 }
81
82 public String getFileExtension() {
83 return getFieldValue(MAVEN.FILE_EXTENSION);
84 }
85
86 public String getSha1() {
87 return getFieldValue(MAVEN.SHA1);
88 }
89 }