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

nats-io / nats.java / #2182

11 Sep 2025 04:47PM UTC coverage: 95.075% (+0.009%) from 95.066%
#2182

push

github

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

Add getter for ObjectMeta Metadata to ObjectInfo

2 of 4 new or added lines in 1 file covered. (50.0%)

2 existing lines in 1 file now uncovered.

11969 of 12589 relevant lines covered (95.08%)

0.95 hits per line

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

98.37
/src/main/java/io/nats/client/api/ObjectInfo.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.Message;
16
import io.nats.client.impl.Headers;
17
import io.nats.client.support.*;
18
import org.jspecify.annotations.NonNull;
19
import org.jspecify.annotations.Nullable;
20

21
import java.time.ZonedDateTime;
22
import java.util.Map;
23

24
import static io.nats.client.support.ApiConstants.*;
25
import static io.nats.client.support.JsonUtils.beginJson;
26
import static io.nats.client.support.JsonUtils.endJson;
27

28
/**
29
 * The ObjectInfo is Object Meta Information plus instance information
30
 */
31
public class ObjectInfo implements JsonSerializable {
32
    private final String bucket;
33
    private final String nuid;
34
    private final long size;
35
    private final long chunks;
36
    private final String digest;
37
    private final boolean deleted;
38
    private final ObjectMeta objectMeta;
39
    private final ZonedDateTime modified;
40

41
    private ObjectInfo(Builder b) {
1✔
42
        bucket = b.bucket;
1✔
43
        nuid = b.nuid;
1✔
44
        size = b.size;
1✔
45
        modified = b.modified;
1✔
46
        chunks = b.chunks;
1✔
47
        digest = b.digest;
1✔
48
        deleted = b.deleted;
1✔
49
        objectMeta = b.metaBuilder.build();
1✔
50
    }
1✔
51

52
    public ObjectInfo(MessageInfo mi) {
53
        this(mi.getData(), mi.getTime());
1✔
54
    }
1✔
55

56
    public ObjectInfo(Message m) {
57
        this(m.getData(), m.metaData().timestamp());
1✔
58
    }
1✔
59

60
    ObjectInfo(byte[] jsonBytes, ZonedDateTime messageTime) {
1✔
61
        JsonValue jv = JsonParser.parseUnchecked(jsonBytes);
1✔
62
        objectMeta = new ObjectMeta(jv);
1✔
63
        bucket = JsonValueUtils.readString(jv, BUCKET);
1✔
64
        nuid = JsonValueUtils.readString(jv, NUID);
1✔
65
        size = JsonValueUtils.readLong(jv, SIZE, 0);
1✔
66
        modified = DateTimeUtils.toGmt(messageTime);
1✔
67
        chunks = JsonValueUtils.readLong(jv, CHUNKS, 0);
1✔
68
        digest = JsonValueUtils.readString(jv, DIGEST);
1✔
69
        deleted = JsonValueUtils.readBoolean(jv, DELETED);
1✔
70
    }
1✔
71

72
    @Override
73
    @NonNull
74
    public String toJson() {
75
        // never write MTIME (modified)
76
        StringBuilder sb = beginJson();
1✔
77
        objectMeta.embedJson(sb); // the go code embeds the objectMeta's fields instead of as a child object.
1✔
78
        JsonUtils.addField(sb, BUCKET, bucket);
1✔
79
        JsonUtils.addField(sb, NUID, nuid);
1✔
80
        JsonUtils.addField(sb, SIZE, size);
1✔
81
        JsonUtils.addField(sb, CHUNKS, chunks);
1✔
82
        JsonUtils.addField(sb, DIGEST, digest);
1✔
83
        JsonUtils.addField(sb, DELETED, deleted);
1✔
84
        return endJson(sb).toString();
1✔
85
    }
86

87
    @NonNull
88
    public String getBucket() {
89
        return bucket;
1✔
90
    }
91

92
    @Nullable
93
    public String getNuid() {
94
        return nuid;
1✔
95
    }
96

97
    public long getSize() {
98
        return size;
1✔
99
    }
100

101
    @Nullable
102
    public ZonedDateTime getModified() {
103
        return modified;
1✔
104
    }
105

106
    public long getChunks() {
107
        return chunks;
1✔
108
    }
109

110
    @Nullable
111
    public String getDigest() {
112
        return digest;
1✔
113
    }
114

115
    public boolean isDeleted() {
116
        return deleted;
1✔
117
    }
118

119
    /**
120
     * The full object meta object
121
     * @return the ObjectMeta
122
     */
123
    @NonNull
124
    public ObjectMeta getObjectMeta() {
125
        return objectMeta;
1✔
126
    }
127

128
    /**
129
     * The object name
130
     * @return the object name
131
     */
132
    @Nullable
133
    public String getObjectName() {
134
        return objectMeta.getObjectName();
1✔
135
    }
136

137
    /**
138
     * The object meta description
139
     * @return the description text or null
140
     */
141
    @Nullable
142
    public String getDescription() {
143
        return objectMeta.getDescription();
1✔
144
    }
145

146
    /**
147
     * The object meta Headers. May be empty but will not be null. In all cases it will be unmodifiable
148
     * @return the headers object
149
     */
150
    @NonNull
151
    public Headers getHeaders() {
152
        return objectMeta.getHeaders();
1✔
153
    }
154

155
    /**
156
     * The object meta metadata. May be empty but will not be null. In all cases it will be unmodifiable
157
     * @return the map
158
     */
159
    @NonNull
160
    public Map<String, String> getMetaData() {
161
        return objectMeta.getMetadata();
1✔
162
    }
163

164
    /**
165
     * Whether the object is actually a link
166
     * @return true if the object is a link instead of a direct object
167
     */
168
    public boolean isLink() {
169
        return objectMeta.getObjectMetaOptions() != null && objectMeta.getObjectMetaOptions().getLink() != null;
1✔
170
    }
171

172
    /**
173
     * If this is a link to an object, get the ObjectLink instance, otherwise this will be null
174
     * @return the ObjectLink or null
175
     */
176
    @Nullable
177
    public ObjectLink getLink() {
178
        return objectMeta.getObjectMetaOptions() == null ? null : objectMeta.getObjectMetaOptions().getLink();
1✔
179
    }
180

181
    public static Builder builder(String bucket, String objectName) {
182
        return new Builder(bucket, objectName);
1✔
183
    }
184

185
    public static Builder builder(String bucket, ObjectMeta meta) {
186
        return new Builder(bucket, meta);
1✔
187
    }
188

189
    public static Builder builder(ObjectInfo info) {
190
        return new Builder(info);
1✔
191
    }
192

193
    public static class Builder {
194
        String bucket;
195
        String nuid;
196
        long size;
197
        ZonedDateTime modified;
198
        long chunks;
199
        String digest;
200
        boolean deleted;
201
        ObjectMeta.Builder metaBuilder;
202

203
        public Builder(String bucket, String objectName) {
1✔
204
            metaBuilder = ObjectMeta.builder(objectName);
1✔
205
            bucket(bucket);
1✔
206
        }
1✔
207

208
        public Builder(String bucket, ObjectMeta meta) {
1✔
209
            metaBuilder = ObjectMeta.builder(meta);
1✔
210
            bucket(bucket);
1✔
211
        }
1✔
212

213
        public Builder(ObjectInfo info) {
1✔
214
            bucket = info.bucket;
1✔
215
            nuid = info.nuid;
1✔
216
            size = info.size;
1✔
217
            modified = info.modified;
1✔
218
            chunks = info.chunks;
1✔
219
            digest = info.digest;
1✔
220
            deleted = info.deleted;
1✔
221
            metaBuilder = ObjectMeta.builder(info.objectMeta);
1✔
222
        }
1✔
223

224
        public Builder objectName(String name) {
225
            metaBuilder.objectName(name);
1✔
226
            return this;
1✔
227
        }
228

229
        public Builder bucket(String bucket) {
230
            this.bucket = Validator.validateBucketName(bucket, true);
1✔
231
            return this;
1✔
232
        }
233

234
        public Builder nuid(String nuid) {
235
            this.nuid = nuid;
1✔
236
            return this;
1✔
237
        }
238

239
        public Builder size(long size) {
240
            this.size = size;
1✔
241
            return this;
1✔
242
        }
243

244
        public Builder modified(ZonedDateTime modified) {
245
            this.modified = modified;
1✔
246
            return this;
1✔
247
        }
248

249
        public Builder chunks(long chunks) {
250
            this.chunks = chunks;
1✔
251
            return this;
1✔
252
        }
253

254
        public Builder digest(String digest) {
255
            this.digest = digest;
1✔
256
            return this;
1✔
257
        }
258

259
        public Builder deleted(boolean deleted) {
260
            this.deleted = deleted;
1✔
261
            return this;
1✔
262
        }
263

264
        public Builder description(String description) {
265
            metaBuilder.description(description);
1✔
266
            return this;
1✔
267
        }
268

269
        public Builder headers(Headers headers) {
270
            metaBuilder.headers(headers);
1✔
271
            return this;
1✔
272
        }
273

274
        public Builder metadata(Map<String, String> metadata) {
NEW
275
            metaBuilder.metadata(metadata);
×
NEW
276
            return this;
×
277
        }
278

279
        public Builder options(ObjectMetaOptions objectMetaOptions) {
280
            metaBuilder.options(objectMetaOptions);
1✔
281
            return this;
1✔
282
        }
283

284
        public Builder chunkSize(int chunkSize) {
285
            metaBuilder.chunkSize(chunkSize);
1✔
286
            return this;
1✔
287
        }
288

289
        public Builder link(ObjectLink link) {
290
            metaBuilder.link(link);
1✔
291
            return this;
1✔
292
        }
293

294
        public Builder bucketLink(String bucket) {
295
            metaBuilder.link(ObjectLink.bucket(bucket));
1✔
296
            return this;
1✔
297
        }
298

299
        public Builder objectLink(String bucket, String objectName) {
300
            metaBuilder.link(ObjectLink.object(bucket, objectName));
1✔
301
            return this;
1✔
302
        }
303

304
        public ObjectInfo build() {
305
            return new ObjectInfo(this);
1✔
306
        }
307
    }
308

309
    @Override
310
    public boolean equals(Object o) {
311
        if (this == o) return true;
1✔
312
        if (o == null || getClass() != o.getClass()) return false;
1✔
313

314
        ObjectInfo info = (ObjectInfo) o;
1✔
315

316
        if (size != info.size) return false;
1✔
317
        if (chunks != info.chunks) return false;
1✔
318
        if (deleted != info.deleted) return false;
1✔
319
        if (!bucket.equals(info.bucket)) return false; // bucket never null
1✔
320
        if (nuid != null ? !nuid.equals(info.nuid) : info.nuid != null) return false;
1✔
321
        if (modified != null ? !modified.equals(info.modified) : info.modified != null) return false;
1✔
322
        if (digest != null ? !digest.equals(info.digest) : info.digest != null) return false;
1✔
323
        return objectMeta.equals(info.objectMeta);
1✔
324
    }
325

326
    @Override
327
    public int hashCode() {
328
        int result = bucket.hashCode(); // bucket never null
1✔
329
        result = 31 * result + (nuid != null ? nuid.hashCode() : 0);
1✔
330
        result = 31 * result + Long.hashCode(size);
1✔
331
        result = 31 * result + (modified != null ? modified.hashCode() : 0);
1✔
332
        result = 31 * result + Long.hashCode(chunks);
1✔
333
        result = 31 * result + (digest != null ? digest.hashCode() : 0);
1✔
334
        result = 31 * result + (deleted ? 1 : 0);
1✔
335
        result = 31 * result + objectMeta.hashCode();
1✔
336
        return result;
1✔
337
    }
338

339
    @Override
340
    public String toString() {
341
        return "ObjectInfo{" +
1✔
342
            "bucket='" + bucket + '\'' +
343
            ", nuid='" + nuid + '\'' +
344
            ", size=" + size +
345
            ", modified=" + modified +
346
            ", chunks=" + chunks +
347
            ", digest='" + digest + '\'' +
348
            ", deleted=" + deleted +
349
            ", objectMeta=" + objectMeta +
350
            '}';
351
    }
352
}
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

© 2025 Coveralls, Inc