• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nats.java / #2119

20 Aug 2025 04:06PM UTC coverage: 95.334% (-0.03%) from 95.36%
#2119

push

github

web-flow
Merge pull request #1399 from nats-io/object-meta-meta

ObjectMeta metadata

21 of 24 new or added lines in 2 files covered. (87.5%)

2 existing lines in 2 files now uncovered.

11933 of 12517 relevant lines covered (95.33%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.4
/src/main/java/io/nats/client/api/ObjectMeta.java
1
// Copyright 2022 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at:
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
package io.nats.client.api;
14

15
import io.nats.client.impl.Headers;
16
import io.nats.client.support.JsonSerializable;
17
import io.nats.client.support.JsonUtils;
18
import io.nats.client.support.JsonValue;
19
import io.nats.client.support.Validator;
20
import org.jspecify.annotations.NonNull;
21
import org.jspecify.annotations.Nullable;
22

23
import java.util.Collections;
24
import java.util.HashMap;
25
import java.util.Map;
26
import java.util.Objects;
27

28
import static io.nats.client.support.ApiConstants.*;
29
import static io.nats.client.support.JsonUtils.beginJson;
30
import static io.nats.client.support.JsonUtils.endJson;
31
import static io.nats.client.support.JsonValueUtils.*;
32

33
/**
34
 * The ObjectMeta is Object Meta is high level information about an object
35
 */
36
public class ObjectMeta implements JsonSerializable {
37

38
    private final String objectName;
39
    private final String description;
40
    private final Headers headers;
41
    private final Map<String, String> metadata;
42
    private final ObjectMetaOptions objectMetaOptions;
43

44
    private ObjectMeta(Builder b) {
1✔
45
        objectName = b.objectName;
1✔
46
        description = b.description;
1✔
47
        headers = new Headers(b.headers, true);
1✔
48
        metadata = Collections.unmodifiableMap(b.metadata);
1✔
49
        objectMetaOptions = b.metaOptionsBuilder.build();
1✔
50
    }
1✔
51

52
    ObjectMeta(JsonValue vObjectMeta) {
1✔
53
        objectName = readString(vObjectMeta, NAME);
1✔
54
        description = readString(vObjectMeta, DESCRIPTION);
1✔
55
        Headers h = new Headers();
1✔
56
        JsonValue hJv = readObject(vObjectMeta, HEADERS);
1✔
57
        for (String key : hJv.map.keySet()) {
1✔
58
            h.put(key, readStringList(hJv, key));
1✔
59
        }
1✔
60
        headers = new Headers(h, true);
1✔
61
        Map<String, String> meta = readStringStringMap(vObjectMeta, METADATA);
1✔
62
        metadata = meta == null ? Collections.unmodifiableMap(new HashMap<>()) : Collections.unmodifiableMap(meta);
1✔
63
        objectMetaOptions = new ObjectMetaOptions(readObject(vObjectMeta, OPTIONS));
1✔
64
    }
1✔
65

66
    @Override
67
    @NonNull
68
    public String toJson() {
69
        StringBuilder sb = beginJson();
1✔
70
        embedJson(sb);
1✔
71
        return endJson(sb).toString();
1✔
72
    }
73

74
    void embedJson(StringBuilder sb) {
75
        JsonUtils.addField(sb, NAME, objectName);
1✔
76
        JsonUtils.addField(sb, DESCRIPTION, description);
1✔
77
        JsonUtils.addField(sb, HEADERS, headers);
1✔
78
        JsonUtils.addField(sb, METADATA, metadata);
1✔
79

80
        // avoid adding an empty child to the json because JsonUtils.addField
81
        // only checks versus the object being null, which it is never
82
        if (objectMetaOptions.hasData()) {
1✔
83
            JsonUtils.addField(sb, OPTIONS, objectMetaOptions);
1✔
84
        }
85
    }
1✔
86

87
    /**
88
     * The object name
89
     * @return the object name
90
     */
91
    @NonNull
92
    public String getObjectName() {
93
        return objectName;
1✔
94
    }
95

96
    /**
97
     * The description
98
     * @return the description text or null
99
     */
100
    @Nullable
101
    public String getDescription() {
102
        return description;
1✔
103
    }
104

105
    /**
106
     * Headers may be empty but will not be null. In all cases it will be unmodifiable
107
     * @return the headers object
108
     */
109
    @NonNull
110
    public Headers getHeaders() {
111
        return headers;
1✔
112
    }
113

114
    /**
115
     * Metadata may be empty but will not be null. In all cases it will be unmodifiable
116
     * @return the map
117
     */
118
    @NonNull
119
    public Map<String, String> getMetadata() {
120
        return metadata;
1✔
121
    }
122

123
    /**
124
     * The ObjectMetaOptions are additional options describing the object
125
     * @return the object meta data
126
     */
127
    @Nullable
128
    public ObjectMetaOptions getObjectMetaOptions() {
129
        return objectMetaOptions;
1✔
130
    }
131

132
    public static Builder builder(String objectName) {
133
        return new Builder(objectName);
1✔
134
    }
135

136
    public static Builder builder(ObjectMeta om) {
137
        return new Builder(om);
1✔
138
    }
139

140
    public static ObjectMeta objectName(String objectName) {
141
        return new Builder(objectName).build();
1✔
142
    }
143

144
    public static class Builder {
145
        String objectName;
146
        String description;
147
        Headers headers;
148
        Map<String, String> metadata;
149
        ObjectMetaOptions.Builder metaOptionsBuilder;
150

151
        public Builder(String objectName) {
1✔
152
            headers = new Headers();
1✔
153
            metadata = new HashMap<>();
1✔
154
            metaOptionsBuilder = ObjectMetaOptions.builder();
1✔
155
            objectName(objectName);
1✔
156
        }
1✔
157

158
        public Builder(ObjectMeta om) {
1✔
159
            objectName = om.objectName;
1✔
160
            description = om.description;
1✔
161
            headers = new Headers(om.headers);
1✔
162
            metadata = new HashMap<>(om.metadata);
1✔
163
            metaOptionsBuilder = ObjectMetaOptions.builder(om.objectMetaOptions);
1✔
164
        }
1✔
165

166
        public Builder objectName(String name) {
167
            this.objectName = Validator.validateNotNull(name, "Object Name");
1✔
168
            return this;
1✔
169
        }
170

171
        public Builder description(String description) {
172
            this.description = description;
1✔
173
            return this;
1✔
174
        }
175

176
        public Builder headers(Headers headers) {
177
            if (headers == null) {
1✔
178
                this.headers.clear();
1✔
179
            }
180
            else {
181
                this.headers = new Headers(headers);
1✔
182
            }
183
            return this;
1✔
184
        }
185

186
        public Builder metadata(Map<String, String> metadata) {
NEW
187
            if (metadata == null) {
×
NEW
188
                this.metadata.clear();
×
189
            }
190
            else {
NEW
191
                this.metadata = metadata;
×
192
            }
UNCOV
193
            return this;
×
194
        }
195

196
        public Builder options(ObjectMetaOptions objectMetaOptions) {
197
            metaOptionsBuilder = ObjectMetaOptions.builder(objectMetaOptions);
1✔
198
            return this;
1✔
199
        }
200

201
        public Builder chunkSize(int chunkSize) {
202
            metaOptionsBuilder.chunkSize(chunkSize);
1✔
203
            return this;
1✔
204
        }
205

206
        public Builder link(ObjectLink link) {
207
            metaOptionsBuilder.link(link);
1✔
208
            return this;
1✔
209
        }
210

211
        public ObjectMeta build() {
212
            return new ObjectMeta(this);
1✔
213
        }
214
    }
215

216
    @Override
217
    public boolean equals(Object o) {
218
        if (this == o) return true;
1✔
219
        if (o == null || getClass() != o.getClass()) return false;
1✔
220

221
        ObjectMeta that = (ObjectMeta) o;
1✔
222

223
        if (!objectName.equals(that.objectName)) return false;
1✔
224
        if (description != null ? !description.equals(that.description) : that.description != null) return false;
1✔
225
        if (!Objects.equals(headers, that.headers)) return false;
1✔
226
        if (!Objects.equals(metadata, that.metadata)) return false;
1✔
227
        return objectMetaOptions.equals(that.objectMetaOptions);
1✔
228
    }
229

230
    @Override
231
    public int hashCode() {
232
        int result = objectName.hashCode();
1✔
233
        result = 31 * result + (description != null ? description.hashCode() : 0);
1✔
234
        result = 31 * result + headers.hashCode();
1✔
235
        result = 31 * result + metadata.hashCode();
1✔
236
        result = 31 * result + objectMetaOptions.hashCode();
1✔
237
        return result;
1✔
238
    }
239

240
    @Override
241
    public String toString() {
242
        return "ObjectMeta{" +
1✔
243
            "objectName='" + objectName + '\'' +
244
            ", description='" + description + '\'' +
245
            ", headers?" + headers.size() +
1✔
246
            ", metadata?" + metadata.size() +
1✔
247
            ", objectMetaOptions=" + objectMetaOptions +
248
            '}';
249
    }
250
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc