-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathArrayMetadataBuilder.java
More file actions
162 lines (137 loc) · 5.4 KB
/
Copy pathArrayMetadataBuilder.java
File metadata and controls
162 lines (137 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package dev.zarr.zarrjava.v3;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.v3.chunkgrid.ChunkGrid;
import dev.zarr.zarrjava.v3.chunkgrid.RegularChunkGrid;
import dev.zarr.zarrjava.v3.chunkkeyencoding.ChunkKeyEncoding;
import dev.zarr.zarrjava.v3.chunkkeyencoding.DefaultChunkKeyEncoding;
import dev.zarr.zarrjava.core.chunkkeyencoding.Separator;
import dev.zarr.zarrjava.v3.chunkkeyencoding.V2ChunkKeyEncoding;
import dev.zarr.zarrjava.v3.codec.Codec;
import dev.zarr.zarrjava.v3.codec.CodecBuilder;
import dev.zarr.zarrjava.v3.codec.core.BytesCodec;
import dev.zarr.zarrjava.core.codec.core.BytesCodec.Endian;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ArrayMetadataBuilder {
long[] shape = null;
DataType dataType = null;
ChunkGrid chunkGrid = null;
ChunkKeyEncoding chunkKeyEncoding =
new DefaultChunkKeyEncoding(new DefaultChunkKeyEncoding.Configuration(Separator.SLASH));
Object fillValue = 0;
Codec[] codecs = new Codec[]{new BytesCodec(Endian.LITTLE)};
Map<String, Object> attributes = new HashMap<>();
Map<String, Object>[] storageTransformers = new HashMap[]{};
String[] dimensionNames = null;
protected ArrayMetadataBuilder() {
}
protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetadata) {
ArrayMetadataBuilder builder = new ArrayMetadataBuilder();
builder.shape = arrayMetadata.shape;
builder.dataType = arrayMetadata.dataType;
builder.chunkGrid = arrayMetadata.chunkGrid;
builder.chunkKeyEncoding = arrayMetadata.chunkKeyEncoding;
builder.fillValue = arrayMetadata.parsedFillValue;
builder.codecs = arrayMetadata.codecs;
builder.attributes = arrayMetadata.attributes;
builder.dimensionNames = arrayMetadata.dimensionNames;
builder.storageTransformers = arrayMetadata.storageTransformers;
return builder;
}
public ArrayMetadataBuilder withShape(long... shape) {
this.shape = shape;
return this;
}
public ArrayMetadataBuilder withDataType(DataType dataType) {
this.dataType = dataType;
return this;
}
public ArrayMetadataBuilder withDataType(String dataType) {
this.dataType = DataType.valueOf(dataType);
return this;
}
public ArrayMetadataBuilder withChunkShape(int... chunkShape) {
this.chunkGrid = new RegularChunkGrid(new RegularChunkGrid.Configuration(chunkShape));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding(Separator separator) {
this.chunkKeyEncoding = new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(separator));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding() {
this.chunkKeyEncoding = new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(Separator.SLASH));
return this;
}
public ArrayMetadataBuilder withDefaultChunkKeyEncoding(String separator) {
this.chunkKeyEncoding =
new DefaultChunkKeyEncoding(
new DefaultChunkKeyEncoding.Configuration(Separator.valueOf(separator)));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding(Separator separator) {
this.chunkKeyEncoding = new V2ChunkKeyEncoding(new V2ChunkKeyEncoding.Configuration(separator));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding() {
this.chunkKeyEncoding = new V2ChunkKeyEncoding(
new V2ChunkKeyEncoding.Configuration(Separator.DOT));
return this;
}
public ArrayMetadataBuilder withV2ChunkKeyEncoding(String separator) {
this.chunkKeyEncoding =
new V2ChunkKeyEncoding(new V2ChunkKeyEncoding.Configuration(Separator.valueOf(separator)));
return this;
}
public ArrayMetadataBuilder withFillValue(Object fillValue) {
this.fillValue = fillValue;
return this;
}
public ArrayMetadataBuilder withCodecs(Codec... codecs) {
this.codecs = codecs;
return this;
}
public ArrayMetadataBuilder withCodecs(Function<CodecBuilder, CodecBuilder> codecBuilder) {
if (dataType == null) {
throw new IllegalStateException("Please call `withDataType` first.");
}
CodecBuilder nestedCodecBuilder = new CodecBuilder(dataType);
this.codecs = codecBuilder.apply(nestedCodecBuilder)
.build();
return this;
}
public ArrayMetadataBuilder withDimensionNames(String... dimensionNames) {
this.dimensionNames = dimensionNames;
return this;
}
public ArrayMetadataBuilder putAttribute(String key, Object value) {
this.attributes.put(key, value);
return this;
}
public ArrayMetadataBuilder withAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
return this;
}
public ArrayMetadataBuilder withStorageTransformers(Map<String, Object>[] storageTransformers) {
this.storageTransformers = storageTransformers;
return this;
}
public ArrayMetadata build() throws ZarrException {
if (shape == null) {
throw new ZarrException("Shape needs to be provided. Please call `.withShape`.");
}
if (dataType == null) {
throw new ZarrException("Data type needs to be provided. Please call `.withDataType`.");
}
if (chunkGrid == null) {
throw new ZarrException("Chunk grid needs to be provided. Please call `.withChunkShape`.");
}
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,
dimensionNames,
attributes,
storageTransformers
);
}
}