View Javadoc
1   package org.apache.maven.search.request;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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  {
32      private final String fieldName;
33  
34      private final boolean searchable;
35  
36      private Field( String fieldName, boolean searchable )
37      {
38          this.fieldName = requireNonNull( fieldName );
39          this.searchable = searchable;
40      }
41  
42      /**
43       * Returns the field name.
44       */
45      public String getFieldName()
46      {
47          return fieldName;
48      }
49  
50      /**
51       * Returns {@code true} if field may be used for {@link FieldQuery} (is searchable).
52       */
53      public boolean isSearchable()
54      {
55          return searchable;
56      }
57  
58      /**
59       * Returns the value of the field from given record instance, or {@code null} if field not present in record.
60       * See subclasses for proper return types.
61       */
62      public abstract Object getFieldValue( Map<Field, Object> record );
63  
64      @Override
65      public boolean equals( Object o )
66      {
67          if ( this == o )
68          {
69              return true;
70          }
71          if ( o == null || getClass() != o.getClass() )
72          {
73              return false;
74          }
75          Field fieldName1 = (Field) o;
76          return Objects.equals( getFieldName(), fieldName1.getFieldName() );
77      }
78  
79      @Override
80      public int hashCode()
81      {
82          return Objects.hash( getFieldName() );
83      }
84  
85      @Override
86      public String toString()
87      {
88          return fieldName;
89      }
90  
91      public static class StringField extends Field
92      {
93          public StringField( String fieldName, boolean searchable )
94          {
95              super( fieldName, searchable );
96          }
97  
98          @Override
99          public String getFieldValue( Map<Field, Object> record )
100         {
101             return (String) record.get( this );
102         }
103     }
104 
105     public static class NumberField extends Field
106     {
107         public NumberField( String fieldName, boolean searchable )
108         {
109             super( fieldName, searchable );
110         }
111 
112         @Override
113         public Number getFieldValue( Map<Field, Object> record )
114         {
115             return (Number) record.get( this );
116         }
117     }
118 
119     public static class BooleanField extends Field
120     {
121         public BooleanField( String fieldName, boolean searchable )
122         {
123             super( fieldName, searchable );
124         }
125 
126         @Override
127         public Boolean getFieldValue( Map<Field, Object> record )
128         {
129             return (Boolean) record.get( this );
130         }
131     }
132 }