View Javadoc
1   package org.apache.maven.search;
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.Collections;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.maven.search.request.Field;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * A search response record.
32   */
33  public final class Record
34  {
35      private final String backendId;
36  
37      private final String repositoryId;
38  
39      private final String uid;
40  
41      private final Long lastUpdated;
42  
43      private final Map<Field, Object> fields;
44  
45      public Record( String backendId,
46                     String repositoryId,
47                     String uid,
48                     Long lastUpdated,
49                     Map<Field, Object> fields )
50      {
51          this.backendId = requireNonNull( backendId );
52          this.repositoryId = requireNonNull( repositoryId );
53          this.uid = uid;
54          this.lastUpdated = lastUpdated;
55          this.fields = Collections.unmodifiableMap( fields );
56      }
57  
58      /**
59       * Returns {@link SearchBackend#getBackendId()} of originating search backend. Never {@code null}.
60       */
61      public String getBackendId()
62      {
63          return backendId;
64      }
65  
66      /**
67       * Returns {@link SearchBackend#getRepositoryId()}) of originating search backend. Never {@code null}.
68       */
69      public String getRepositoryId()
70      {
71          return repositoryId;
72      }
73  
74      /**
75       * Returns UID (unique if combined with {@link #getBackendId()}) of search result record, if provided by backend.
76       * May be {@code null} if not provided.
77       */
78      public String getUid()
79      {
80          return uid;
81      }
82  
83      /**
84       * Returns {@link Long}, representing "last updated" timestamp as epoch millis if provided by backend. May be
85       * {@code null} if not provided.
86       */
87      public Long getLastUpdated()
88      {
89          return lastUpdated;
90      }
91  
92      /**
93       * Returns unmodifiable map of all values keyed by {@link Field} backing this record.
94       */
95      public Map<Field, Object> getFields()
96      {
97          return fields;
98      }
99  
100     /**
101      * Returns unmodifiable set of present fields in this record, never {@code null}.
102      */
103     public Set<Field> fieldSet()
104     {
105         return fields.keySet();
106     }
107 
108     /**
109      * Returns {@code true} if given field is present in this record.
110      */
111     public boolean hasField( Field field )
112     {
113         return fields.containsKey( field );
114     }
115 
116     /**
117      * Returns the value belonging to given field in this record, or {@code null} if field not present.
118      */
119     public String getValue( Field.StringField field )
120     {
121         return field.getFieldValue( fields );
122     }
123 
124     /**
125      * Returns the value belonging to given field in this record, or {@code null} if field not present.
126      */
127     public Number getValue( Field.NumberField field )
128     {
129         return field.getFieldValue( fields );
130     }
131 
132     /**
133      * Returns the value belonging to given field in this record, or {@code null} if field not present.
134      */
135     public Boolean getValue( Field.BooleanField field )
136     {
137         return field.getFieldValue( fields );
138     }
139 }