-
Notifications
You must be signed in to change notification settings - Fork 14
V2 attributes #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brokkoli71
wants to merge
6
commits into
main
Choose a base branch
from
v2-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
V2 attributes #28
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc35390
add support for attributes in Array and ArrayMetadata classes
brokkoli71 832ef4e
change v3 attributes class to Attributes
brokkoli71 0ab59f6
implement setAttributes and updateAttributes for v2
brokkoli71 526a16c
add test for resize array
brokkoli71 baeffa9
fix array.setAttributes
brokkoli71 07b02c5
rename attributes.add to set
brokkoli71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| package dev.zarr.zarrjava.core; | ||
|
|
||
| import dev.zarr.zarrjava.ZarrException; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| public class Attributes extends HashMap<String, Object> { | ||
|
|
||
| public Attributes() { | ||
| super(); | ||
| } | ||
|
|
||
| public Attributes (Function<Attributes, Attributes> attributeMapper) { | ||
| super(); | ||
| attributeMapper.apply(this); | ||
| } | ||
|
|
||
|
|
||
| public Attributes(Map<String, Object> attributes) { | ||
| super(attributes); | ||
| } | ||
|
|
||
| public Attributes add(String s, Object o){ | ||
normanrz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.put(s, o); | ||
| return this; | ||
| } | ||
|
|
||
| public Attributes delete(String s){ | ||
| this.remove(s); | ||
| return this; | ||
| } | ||
|
|
||
| public boolean getBoolean(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof Boolean) { | ||
| return (Boolean) value; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a Boolean"); | ||
| } | ||
|
|
||
| public int getInt(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof Number) { | ||
| return ((Number) value).intValue(); | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not an Integer"); | ||
| } | ||
|
|
||
| public double getDouble(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof Number) { | ||
| return ((Number) value).doubleValue(); | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a Double"); | ||
| } | ||
|
|
||
| public float getFloat(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof Number) { | ||
| return ((Number) value).floatValue(); | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a Float"); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public String getString(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof String) { | ||
| return (String) value; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a String"); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public List<Object> getList(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof List) { | ||
| return (List<Object>) value; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a List"); | ||
| } | ||
|
|
||
| public Attributes getAttributes(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof Attributes) { | ||
| return (Attributes) value; | ||
| } | ||
| if (value instanceof Map) { | ||
| return new Attributes((Map<String, Object>) value); | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not an Attributes object"); | ||
| } | ||
|
|
||
| public <T> T[] getArray(String key, Class<T> clazz) throws ZarrException { | ||
| Object value = this.get(key); | ||
| if (value instanceof Object[] && (((Object[]) value).length == 0 || clazz.isInstance(((Object[]) value)[0]) )) { | ||
| return (T[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| @SuppressWarnings("unchecked") | ||
| T[] array = (T[]) java.lang.reflect.Array.newInstance(clazz, list.size()); | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (clazz.isInstance(elem)) { | ||
| array[i] = clazz.cast(elem); | ||
| } else { | ||
| // Try to find a constructor that takes the element's class | ||
| java.lang.reflect.Constructor<?> matched = null; | ||
| for (java.lang.reflect.Constructor<?> c : clazz.getConstructors()) { | ||
| Class<?>[] params = c.getParameterTypes(); | ||
| if (params.length == 1 && params[0].isAssignableFrom(elem.getClass())) { | ||
| matched = c; | ||
| break; | ||
| } | ||
| } | ||
| if (matched != null) { | ||
| try { | ||
| array[i] = (T) matched.newInstance(elem); | ||
| } catch (Exception e) { | ||
| throw new ZarrException("Failed to convert element at index " + i + " to type " + clazz.getName(), e); | ||
| } | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not of type " + clazz.getName() + " and no suitable constructor found for conversion of type " + elem.getClass().getName()); | ||
| } | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a List or array of type " + clazz.getName()); | ||
| } | ||
|
|
||
| public int[] getIntArray(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof int[]) { | ||
| return (int[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| int[] array = new int[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (elem instanceof Number) { | ||
| array[i] = ((Number) elem).intValue(); | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not a Number"); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not an int array or List"); | ||
| } | ||
|
|
||
| public long[] getLongArray(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof long[]) { | ||
| return (long[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| long[] array = new long[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (elem instanceof Number) { | ||
| array[i] = ((Number) elem).longValue(); | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not a Number"); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a long array or List"); | ||
| } | ||
|
|
||
| public double[] getDoubleArray(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof double[]) { | ||
| return (double[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| double[] array = new double[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (elem instanceof Number) { | ||
| array[i] = ((Number) elem).doubleValue(); | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not a Number"); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a double array or List"); | ||
| } | ||
|
|
||
| public float[] getFloatArray(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof float[]) { | ||
| return (float[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| float[] array = new float[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (elem instanceof Number) { | ||
| array[i] = ((Number) elem).floatValue(); | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not a Number"); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a float array or List"); | ||
| } | ||
|
|
||
| public boolean[] getBooleanArray(String key) { | ||
| Object value = this.get(key); | ||
| if (value instanceof boolean[]) { | ||
| return (boolean[]) value; | ||
| } | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| boolean[] array = new boolean[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object elem = list.get(i); | ||
| if (elem instanceof Boolean) { | ||
| array[i] = (Boolean) elem; | ||
| } else { | ||
| throw new IllegalArgumentException("Element at index " + i + " is not a Boolean"); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| throw new IllegalArgumentException("Value for key " + key + " is not a boolean array or List"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.