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.api.request;
20  
21  import java.time.Instant;
22  import java.util.Map;
23  import java.util.Objects;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * Field, that is used as key in record.
29   */
30  public abstract class Field {
31      private final String fieldName;
32  
33      private Field(String fieldName) {
34          this.fieldName = requireNonNull(fieldName);
35      }
36  
37      /**
38       * Returns the field name.
39       */
40      public String getFieldName() {
41          return fieldName;
42      }
43  
44      /**
45       * Returns the value of the field from given record instance, or {@code null} if field not present in record.
46       * See subclasses for proper return types.
47       */
48      public abstract Object getFieldValue(Map<Field, Object> record);
49  
50      @Override
51      public boolean equals(Object o) {
52          if (this == o) {
53              return true;
54          }
55          if (o == null || getClass() != o.getClass()) {
56              return false;
57          }
58          Field fieldName1 = (Field) o;
59          return Objects.equals(getFieldName(), fieldName1.getFieldName());
60      }
61  
62      @Override
63      public int hashCode() {
64          return Objects.hash(getFieldName());
65      }
66  
67      @Override
68      public String toString() {
69          return fieldName;
70      }
71  
72      public static class StringField extends Field {
73          public StringField(String fieldName) {
74              super(fieldName);
75          }
76  
77          @Override
78          public String getFieldValue(Map<Field, Object> record) {
79              return (String) record.get(this);
80          }
81      }
82  
83      public static class NumberField extends Field {
84          public NumberField(String fieldName) {
85              super(fieldName);
86          }
87  
88          @Override
89          public Number getFieldValue(Map<Field, Object> record) {
90              return (Number) record.get(this);
91          }
92      }
93  
94      public static class BooleanField extends Field {
95          public BooleanField(String fieldName) {
96              super(fieldName);
97          }
98  
99          @Override
100         public Boolean getFieldValue(Map<Field, Object> record) {
101             return (Boolean) record.get(this);
102         }
103     }
104 
105     /**
106      * @since 7.0.4
107      */
108     public static class InstantField extends Field {
109         public InstantField(String fieldName) {
110             super(fieldName);
111         }
112 
113         public Instant getFieldValue(Map<Field, Object> record) {
114             return (Instant) record.get(this);
115         }
116     }
117 }