View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Class that "disassembles" the query and populates fields and values and exposes them.
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  }