Skip to content

Commit 36cb46a

Browse files
committed
remove code duplication and add v2.Group.setAttributes
1 parent 4763778 commit 36cb46a

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/main/java/dev/zarr/zarrjava/v2/Group.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,7 @@ public static Group open(String path) throws IOException {
5252
public static Group create(
5353
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
5454
) throws IOException {
55-
ObjectWriter objectWriter = makeObjectWriter();
56-
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(groupMetadata));
57-
storeHandle.resolve(ZGROUP).set(metadataBytes);
58-
if (groupMetadata.attributes != null) {
59-
StoreHandle attrsHandle = storeHandle.resolve(ZATTRS);
60-
ByteBuffer attrsBytes = ByteBuffer.wrap(
61-
objectWriter.writeValueAsBytes(groupMetadata.attributes));
62-
attrsHandle.set(attrsBytes);
63-
}
64-
return new Group(storeHandle, groupMetadata);
55+
return new Group(storeHandle, groupMetadata).writeMetadata();
6556
}
6657

6758
public static Group create(@Nonnull StoreHandle storeHandle) throws IOException, ZarrException {
@@ -108,10 +99,38 @@ public Array createArray(String key, ArrayMetadata arrayMetadata)
10899
}
109100

110101
public Array createArray(String key, Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper)
111-
throws IOException, ZarrException {
102+
throws IOException, ZarrException {
112103
return Array.create(storeHandle.resolve(key), arrayMetadataBuilderMapper, false);
113104
}
114105

106+
private Group writeMetadata() throws IOException {
107+
return writeMetadata(this.metadata);
108+
}
109+
110+
private Group writeMetadata(GroupMetadata newGroupMetadata) throws IOException {
111+
ObjectWriter objectWriter = makeObjectWriter();
112+
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(newGroupMetadata));
113+
storeHandle.resolve(ZGROUP).set(metadataBytes);
114+
if (newGroupMetadata.attributes != null) {
115+
StoreHandle attrsHandle = storeHandle.resolve(ZATTRS);
116+
ByteBuffer attrsBytes = ByteBuffer.wrap(
117+
objectWriter.writeValueAsBytes(newGroupMetadata.attributes));
118+
attrsHandle.set(attrsBytes);
119+
}
120+
return new Group(storeHandle, newGroupMetadata);
121+
}
122+
123+
public Group setAttributes(Attributes newAttributes) throws ZarrException, IOException {
124+
GroupMetadata newGroupMetadata = new GroupMetadata(newAttributes);
125+
return writeMetadata(newGroupMetadata);
126+
}
127+
128+
public Group updateAttributes(Function<Attributes, Attributes> attributeMapper)
129+
throws ZarrException, IOException {
130+
return setAttributes(attributeMapper.apply(metadata.attributes));
131+
}
132+
133+
115134
@Override
116135
public String toString() {
117136
return String.format("<v2.Group {%s}>", storeHandle);

src/main/java/dev/zarr/zarrjava/v3/Group.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ public static Group open(String path) throws IOException {
4646
public static Group create(
4747
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
4848
) throws IOException {
49-
ObjectWriter objectWriter = makeObjectWriter();
50-
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(groupMetadata));
51-
storeHandle.resolve(ZARR_JSON)
52-
.set(metadataBytes);
53-
return new Group(storeHandle, groupMetadata);
49+
return new Group(storeHandle, groupMetadata).writeMetadata();
5450
}
5551

5652
public static Group create(
@@ -115,6 +111,10 @@ public Array createArray(String key,
115111
return Array.create(storeHandle.resolve(key), arrayMetadataBuilderMapper, false);
116112
}
117113

114+
private Group writeMetadata() throws IOException {
115+
return writeMetadata(this.metadata);
116+
}
117+
118118
private Group writeMetadata(GroupMetadata newGroupMetadata) throws IOException {
119119
ObjectWriter objectWriter = makeObjectWriter();
120120
ByteBuffer metadataBytes = ByteBuffer.wrap(objectWriter.writeValueAsBytes(newGroupMetadata));

0 commit comments

Comments
 (0)