Skip to content

Commit c03f3c6

Browse files
committed
overload open and create for Path and String arguments
1 parent cf80fcf commit c03f3c6

File tree

9 files changed

+270
-2
lines changed

9 files changed

+270
-2
lines changed

src/main/java/dev/zarr/zarrjava/core/Array.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.zarr.zarrjava.core;
22

33
import dev.zarr.zarrjava.ZarrException;
4+
import dev.zarr.zarrjava.store.FilesystemStore;
45
import dev.zarr.zarrjava.store.StoreHandle;
56
import dev.zarr.zarrjava.utils.IndexingUtils;
67
import dev.zarr.zarrjava.utils.MultiArrayUtils;
@@ -12,6 +13,8 @@
1213
import javax.annotation.Nullable;
1314
import java.io.IOException;
1415
import java.nio.ByteBuffer;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
1518
import java.util.Arrays;
1619
import java.util.stream.Stream;
1720

@@ -44,7 +47,27 @@ public static Array open(StoreHandle storeHandle) throws IOException, ZarrExcept
4447
throw new ZarrException("No Zarr array found at the specified location.");
4548
}
4649
}
50+
/**
51+
* Opens an existing Zarr array at a specified storage location. Automatically detects the Zarr version.
52+
*
53+
* @param path the storage location of the Zarr array
54+
* @throws IOException throws IOException if the metadata cannot be read
55+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
56+
*/
57+
public static Array open(Path path) throws IOException, ZarrException {
58+
return open(new StoreHandle(new FilesystemStore(path)));
59+
}
4760

61+
/**
62+
* Opens an existing Zarr array at a specified storage location. Automatically detects the Zarr version.
63+
*
64+
* @param path the storage location of the Zarr array
65+
* @throws IOException throws IOException if the metadata cannot be read
66+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
67+
*/
68+
public static Array open(String path) throws IOException, ZarrException {
69+
return open(Paths.get(path));
70+
}
4871

4972
/**
5073
* Writes a ucar.ma2.Array into the Zarr array at a specified offset. The shape of the Zarr array
@@ -357,4 +380,4 @@ public void write(@Nonnull ucar.ma2.Array content) throws ZarrException {
357380
}
358381

359382
}
360-
}
383+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package dev.zarr.zarrjava.core;
22

33
import dev.zarr.zarrjava.ZarrException;
4+
import dev.zarr.zarrjava.store.FilesystemStore;
45
import dev.zarr.zarrjava.store.StoreHandle;
56

67
import javax.annotation.Nonnull;
78
import javax.annotation.Nullable;
89
import java.io.IOException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
912
import java.util.Objects;
1013
import java.util.stream.Stream;
1114

@@ -38,6 +41,28 @@ public static Group open(StoreHandle storeHandle) throws IOException, ZarrExcept
3841
}
3942

4043

44+
/**
45+
* Opens an existing Zarr group at a specified storage location. Automatically detects the Zarr version.
46+
*
47+
* @param path the storage location of the Zarr group
48+
* @throws IOException throws IOException if the metadata cannot be read
49+
* @throws ZarrException throws ZarrException if the Zarr group cannot be opened
50+
*/
51+
public static Group open(Path path) throws IOException, ZarrException {
52+
return open(new StoreHandle(new FilesystemStore(path)));
53+
}
54+
55+
/**
56+
* Opens an existing Zarr group at a specified storage location. Automatically detects the Zarr version.
57+
*
58+
* @param path the storage location of the Zarr group
59+
* @throws IOException throws IOException if the metadata cannot be read
60+
* @throws ZarrException throws ZarrException if the Zarr group cannot be opened
61+
*/
62+
public static Group open(String path) throws IOException, ZarrException {
63+
return open(Paths.get(path));
64+
}
65+
4166
@Nullable
4267
public abstract Node get(String key) throws ZarrException;
4368

src/main/java/dev/zarr/zarrjava/core/Node.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package dev.zarr.zarrjava.core;
22

33
import dev.zarr.zarrjava.ZarrException;
4+
import dev.zarr.zarrjava.store.FilesystemStore;
45
import dev.zarr.zarrjava.store.StoreHandle;
56

67
import java.io.IOException;
78
import java.nio.file.NoSuchFileException;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
811

912
public interface Node {
1013

@@ -33,5 +36,27 @@ static Node open(StoreHandle storeHandle) throws IOException, ZarrException {
3336
throw new NoSuchFileException("No Zarr node found at " + storeHandle);
3437
}
3538
}
39+
40+
/**
41+
* Opens an existing Zarr array or group at a specified storage location. Automatically detects the Zarr version.
42+
*
43+
* @param path the storage location of the Zarr array
44+
* @throws IOException throws IOException if the metadata cannot be read
45+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
46+
*/
47+
static Node open(Path path) throws IOException, ZarrException {
48+
return open(new StoreHandle(new FilesystemStore(path)));
49+
}
50+
51+
/**
52+
* Opens an existing Zarr array or group at a specified storage location. Automatically detects the Zarr version.
53+
*
54+
* @param path the storage location of the Zarr array
55+
* @throws IOException throws IOException if the metadata cannot be read
56+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
57+
*/
58+
static Node open(String path) throws IOException, ZarrException {
59+
return open(Paths.get(path));
60+
}
3661
}
3762

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import dev.zarr.zarrjava.ZarrException;
5+
import dev.zarr.zarrjava.store.FilesystemStore;
56
import dev.zarr.zarrjava.store.StoreHandle;
67
import dev.zarr.zarrjava.utils.Utils;
78
import dev.zarr.zarrjava.core.codec.CodecPipeline;
@@ -11,6 +12,8 @@
1112
import javax.annotation.Nonnull;
1213
import java.io.IOException;
1314
import java.nio.ByteBuffer;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
1417
import java.util.Arrays;
1518
import java.util.function.Function;
1619
import java.util.stream.Collectors;
@@ -53,7 +56,57 @@ public static Array open(StoreHandle storeHandle) throws IOException, ZarrExcept
5356
);
5457
}
5558

59+
/**
60+
* Opens an existing Zarr array at a specified storage location.
61+
*
62+
* @param path the storage location of the Zarr array
63+
* @throws IOException throws IOException if the metadata cannot be read
64+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
65+
*/
66+
public static Array open(Path path) throws IOException, ZarrException {
67+
return open(new StoreHandle(new FilesystemStore(path)));
68+
}
69+
70+
/**
71+
* Opens an existing Zarr array at a specified storage location.
72+
*
73+
* @param path the storage location of the Zarr array
74+
* @throws IOException throws IOException if the metadata cannot be read
75+
* @throws ZarrException throws ZarrException if the Zarr array cannot be opened
76+
*/
77+
public static Array open(String path) throws IOException, ZarrException {
78+
return open(Paths.get(path));
79+
}
80+
81+
/**
82+
* Creates a new Zarr array with the provided metadata at a specified storage location. This
83+
* method will raise an exception if a Zarr array already exists at the specified storage
84+
* location.
85+
*
86+
* @param path the storage location of the Zarr array
87+
* @param arrayMetadata the metadata of the Zarr array
88+
* @throws IOException if the metadata cannot be serialized
89+
* @throws ZarrException if the Zarr array cannot be created
90+
*/
91+
public static Array create(Path path, ArrayMetadata arrayMetadata)
92+
throws IOException, ZarrException {
93+
return create(new StoreHandle(new FilesystemStore(path)), arrayMetadata);
94+
}
5695

96+
/**
97+
* Creates a new Zarr array with the provided metadata at a specified storage location. This
98+
* method will raise an exception if a Zarr array already exists at the specified storage
99+
* location.
100+
*
101+
* @param path the storage location of the Zarr array
102+
* @param arrayMetadata the metadata of the Zarr array
103+
* @throws IOException if the metadata cannot be serialized
104+
* @throws ZarrException if the Zarr array cannot be created
105+
*/
106+
public static Array create(String path, ArrayMetadata arrayMetadata)
107+
throws IOException, ZarrException {
108+
return create(Paths.get(path), arrayMetadata);
109+
}
57110
/**
58111
* Creates a new Zarr array with the provided metadata at a specified storage location. This
59112
* method will raise an exception if a Zarr array already exists at the specified storage

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import dev.zarr.zarrjava.ZarrException;
5+
import dev.zarr.zarrjava.store.FilesystemStore;
56
import dev.zarr.zarrjava.store.StoreHandle;
67
import dev.zarr.zarrjava.utils.Utils;
78

89
import javax.annotation.Nonnull;
910
import javax.annotation.Nullable;
1011
import java.io.IOException;
1112
import java.nio.ByteBuffer;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
1215
import java.util.function.Function;
1316
import static dev.zarr.zarrjava.v2.Node.makeObjectMapper;
1417

@@ -27,6 +30,14 @@ public static Group open(@Nonnull StoreHandle storeHandle) throws IOException {
2730
.readValue(Utils.toArray(metadataBytes), GroupMetadata.class));
2831
}
2932

33+
public static Group open(Path path) throws IOException {
34+
return open(new StoreHandle(new FilesystemStore(path)));
35+
}
36+
37+
public static Group open(String path) throws IOException {
38+
return open(Paths.get(path));
39+
}
40+
3041
public static Group create(
3142
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
3243
) throws IOException {
@@ -39,7 +50,23 @@ public static Group create(
3950
public static Group create(
4051
@Nonnull StoreHandle storeHandle
4152
) throws IOException, ZarrException {
42-
return new Group(storeHandle, new GroupMetadata());
53+
return create(storeHandle, new GroupMetadata());
54+
}
55+
56+
public static Group create(Path path, GroupMetadata groupMetadata) throws IOException {
57+
return create(new StoreHandle(new FilesystemStore(path)), groupMetadata);
58+
}
59+
60+
public static Group create(String path, GroupMetadata groupMetadata) throws IOException {
61+
return create(Paths.get(path), groupMetadata);
62+
}
63+
64+
public static Group create(Path path) throws IOException, ZarrException {
65+
return create(new StoreHandle(new FilesystemStore(path)));
66+
}
67+
68+
public static Group create(String path) throws IOException, ZarrException {
69+
return create(Paths.get(path));
4370
}
4471

4572
@Nullable

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
55
import dev.zarr.zarrjava.ZarrException;
6+
import dev.zarr.zarrjava.store.FilesystemStore;
67
import dev.zarr.zarrjava.store.StoreHandle;
78
import dev.zarr.zarrjava.v2.codec.CodecRegistry;
89

910
import java.io.IOException;
1011
import java.nio.file.NoSuchFileException;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
1114

1215
public interface Node extends dev.zarr.zarrjava.core.Node {
1316

@@ -42,4 +45,12 @@ static Node open(StoreHandle storeHandle) throws IOException, ZarrException {
4245
}
4346
throw new NoSuchFileException("Store handle '" + storeHandle + "' does not contain a " + ZGROUP + " or a " + ZARRAY + " file.");
4447
}
48+
49+
static Node open(Path path) throws IOException, ZarrException {
50+
return open(new StoreHandle(new FilesystemStore(path)));
51+
}
52+
53+
static Node open(String path) throws IOException, ZarrException {
54+
return open(Paths.get(path));
55+
}
4556
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import dev.zarr.zarrjava.ZarrException;
5+
import dev.zarr.zarrjava.store.FilesystemStore;
56
import dev.zarr.zarrjava.store.StoreHandle;
67
import dev.zarr.zarrjava.utils.Utils;
78
import dev.zarr.zarrjava.core.codec.CodecPipeline;
89
import java.io.IOException;
910
import java.nio.ByteBuffer;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
1013
import java.util.Arrays;
1114
import java.util.HashMap;
1215
import java.util.Map;
@@ -47,6 +50,58 @@ public static Array open(StoreHandle storeHandle) throws IOException, ZarrExcept
4750
);
4851
}
4952

53+
/**
54+
* Opens an existing Zarr array at a specified storage location using a Path.
55+
*
56+
* @param path the storage location of the Zarr array
57+
* @throws IOException if the metadata cannot be read
58+
* @throws ZarrException if the Zarr array cannot be opened
59+
*/
60+
public static Array open(Path path) throws IOException, ZarrException {
61+
return open(new FilesystemStore(path).resolve());
62+
}
63+
64+
/**
65+
* Opens an existing Zarr array at a specified storage location using a String path.
66+
*
67+
* @param path the storage location of the Zarr array
68+
* @throws IOException if the metadata cannot be read
69+
* @throws ZarrException if the Zarr array cannot be opened
70+
*/
71+
public static Array open(String path) throws IOException, ZarrException {
72+
return open(Paths.get(path));
73+
}
74+
75+
/**
76+
* Creates a new Zarr array with the provided metadata at a specified storage location. This
77+
* method will raise an exception if a Zarr array already exists at the specified storage
78+
* location.
79+
*
80+
* @param path the storage location of the Zarr array
81+
* @param arrayMetadata the metadata of the Zarr array
82+
* @throws IOException if the metadata cannot be serialized
83+
* @throws ZarrException if the Zarr array cannot be created
84+
*/
85+
public static Array create(Path path, ArrayMetadata arrayMetadata)
86+
throws IOException, ZarrException {
87+
return Array.create(new FilesystemStore(path).resolve(), arrayMetadata);
88+
}
89+
90+
/**
91+
* Creates a new Zarr array with the provided metadata at a specified storage location. This
92+
* method will raise an exception if a Zarr array already exists at the specified storage
93+
* location.
94+
*
95+
* @param path the storage location of the Zarr array
96+
* @param arrayMetadata the metadata of the Zarr array
97+
* @throws IOException if the metadata cannot be serialized
98+
* @throws ZarrException if the Zarr array cannot be created
99+
*/
100+
public static Array create(String path, ArrayMetadata arrayMetadata)
101+
throws IOException, ZarrException {
102+
return Array.create(Paths.get(path), arrayMetadata);
103+
}
104+
50105
/**
51106
* Creates a new Zarr array with the provided metadata at a specified storage location. This
52107
* method will raise an exception if a Zarr array already exists at the specified storage
@@ -106,6 +161,16 @@ public static Array create(StoreHandle storeHandle,
106161
arrayMetadataBuilderMapper.apply(new ArrayMetadataBuilder()).build(), existsOk);
107162
}
108163

164+
public static Array create(Path path, Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper, boolean existsOk)
165+
throws IOException, ZarrException {
166+
return Array.create(new FilesystemStore(path).resolve(), arrayMetadataBuilderMapper, existsOk);
167+
}
168+
169+
public static Array create(String path, Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper, boolean existsOk)
170+
throws IOException, ZarrException {
171+
return Array.create(Paths.get(path), arrayMetadataBuilderMapper, existsOk);
172+
}
173+
109174
@Nonnull
110175
public static ArrayMetadataBuilder metadataBuilder() {
111176
return new ArrayMetadataBuilder();

0 commit comments

Comments
 (0)