|
1 | 1 | package dev.zarr.zarrjava.core; |
| 2 | + |
2 | 3 | import dev.zarr.zarrjava.ZarrException; |
3 | 4 | import dev.zarr.zarrjava.store.StoreHandle; |
4 | 5 |
|
5 | 6 | import javax.annotation.Nonnull; |
6 | 7 | import javax.annotation.Nullable; |
| 8 | +import java.io.IOException; |
7 | 9 | import java.util.Objects; |
8 | 10 | import java.util.stream.Stream; |
9 | 11 |
|
10 | | -public abstract class Group extends Node { |
11 | | - |
12 | | - protected Group(@Nonnull StoreHandle storeHandle) { |
13 | | - super(storeHandle); |
14 | | - } |
15 | | - |
16 | | - @Nullable |
17 | | - public abstract Node get(String key) throws ZarrException; |
18 | | - |
19 | | - public Stream<Node> list() { |
20 | | - return storeHandle.list() |
21 | | - .map(key -> { |
22 | | - try { |
23 | | - return get(key); |
24 | | - } catch (ZarrException e) { |
25 | | - throw new RuntimeException(e); |
26 | | - } |
27 | | - }) |
28 | | - .filter(Objects::nonNull); |
29 | | - } |
30 | | - |
31 | | - public Node[] listAsArray() { |
32 | | - try (Stream<Node> nodeStream = list()) { |
33 | | - return nodeStream.toArray(Node[]::new); |
| 12 | +public abstract class Group extends AbstractNode { |
| 13 | + |
| 14 | + protected Group(@Nonnull StoreHandle storeHandle) { |
| 15 | + super(storeHandle); |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Opens an existing Zarr group at a specified storage location. Automatically detects the Zarr version. |
| 21 | + * |
| 22 | + * @param storeHandle the storage location of the Zarr group |
| 23 | + * @throws IOException throws IOException if the metadata cannot be read |
| 24 | + * @throws ZarrException throws ZarrException if the Zarr group cannot be opened |
| 25 | + */ |
| 26 | + public static Group open(StoreHandle storeHandle) throws IOException, ZarrException { |
| 27 | + boolean isV3 = storeHandle.resolve(ZARR_JSON).exists(); |
| 28 | + boolean isV2 = storeHandle.resolve(ZGROUP).exists(); |
| 29 | + if (isV3 && isV2) { |
| 30 | + throw new ZarrException("Both Zarr v2 and v3 groups found at " + storeHandle); |
| 31 | + } else if (isV3) { |
| 32 | + return dev.zarr.zarrjava.v3.Group.open(storeHandle); |
| 33 | + } else if (isV2) { |
| 34 | + return dev.zarr.zarrjava.v2.Group.open(storeHandle); |
| 35 | + } else { |
| 36 | + throw new ZarrException("No Zarr group found at " + storeHandle); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + @Nullable |
| 42 | + public abstract Node get(String key) throws ZarrException; |
| 43 | + |
| 44 | + public Stream<Node> list() { |
| 45 | + return storeHandle.list() |
| 46 | + .map(key -> { |
| 47 | + try { |
| 48 | + return get(key); |
| 49 | + } catch (ZarrException e) { |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + }) |
| 53 | + .filter(Objects::nonNull); |
| 54 | + } |
| 55 | + |
| 56 | + public Node[] listAsArray() { |
| 57 | + try (Stream<Node> nodeStream = list()) { |
| 58 | + return nodeStream.toArray(Node[]::new); |
| 59 | + } |
34 | 60 | } |
35 | | - } |
36 | 61 | } |
0 commit comments