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