X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.acr%2Fsrc%2Forg%2Fargeo%2Fapi%2Facr%2FContent.java;h=4aac92de75b2317f78b83bea56329535c630ff81;hb=c0342975a37c70895c2e8f6b341d790700168d7f;hp=aeea27ef23ff272b2bb0fb0bf4a95c8c65336e1b;hpb=750a3e154c078d6e9fd72d8da950cb325b8ab012;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.acr/src/org/argeo/api/acr/Content.java b/org.argeo.api.acr/src/org/argeo/api/acr/Content.java index aeea27ef2..4aac92de7 100644 --- a/org.argeo.api.acr/src/org/argeo/api/acr/Content.java +++ b/org.argeo.api.acr/src/org/argeo/api/acr/Content.java @@ -2,9 +2,11 @@ package org.argeo.api.acr; import java.io.Closeable; import java.io.IOException; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import javax.xml.XMLConstants; import javax.xml.namespace.QName; @@ -50,6 +52,7 @@ public interface Content extends Iterable, Map { Optional> getMultiple(QName key, Class clss); + @SuppressWarnings("unchecked") default List getMultiple(QName key) { Class type; try { @@ -70,6 +73,13 @@ public interface Content extends Iterable, Map { /* * CONTENT OPERATIONS */ +// default CompletionStage edit(Consumer work) { +// return CompletableFuture.supplyAsync(() -> { +// work.accept(this); +// return this; +// }).minimalCompletionStage(); +// } + Content add(QName name, QName... classes); default Content add(String name, QName... classes) { @@ -80,23 +90,92 @@ public interface Content extends Iterable, Map { void remove(); + /* + * TYPING + */ + List getContentClasses(); + + /** AND */ + default boolean isContentClass(QName... contentClass) { + List contentClasses = getContentClasses(); + for (QName cClass : contentClass) { + if (!contentClasses.contains(cClass)) + return false; + } + return true; + } + + /** OR */ + default boolean hasContentClass(QName... contentClass) { + List contentClasses = getContentClasses(); + for (QName cClass : contentClass) { + if (contentClasses.contains(cClass)) + return true; + } + return false; + } + /* * DEFAULT METHODS */ - default A adapt(Class clss) throws IllegalArgumentException { - throw new IllegalArgumentException("Cannot adapt content " + this + " to " + clss.getName()); + default A adapt(Class clss) { + throw new UnsupportedOperationException("Cannot adapt content " + this + " to " + clss.getName()); + } + + default C open(Class clss) throws IOException { + throw new UnsupportedOperationException("Cannot open content " + this + " as " + clss.getName()); } - default C open(Class clss) throws IOException, IllegalArgumentException { - throw new IllegalArgumentException("Cannot open content " + this + " as " + clss.getName()); + default CompletableFuture write(Class clss) { + throw new UnsupportedOperationException("Cannot write content " + this + " as " + clss.getName()); + } + + /* + * CHILDREN + */ + + default boolean hasChild(QName name) { + for (Content child : this) { + if (child.getName().equals(name)) + return true; + } + return false; + } + + default Content anyOrAddChild(QName name, QName... classes) { + Content child = anyChild(name); + if (child != null) + return child; + return this.add(name, classes); + } + + /** Any child with this name, or null if there is none */ + default Content anyChild(QName name) { + for (Content child : this) { + if (child.getName().equals(name)) + return child; + } + return null; } /* * CONVENIENCE METHODS */ -// default String attr(String key) { -// return get(key, String.class); -// } + default String attr(String key) { + Object obj = get(key); + if (obj == null) + return null; + return obj.toString(); + + } + + default String attr(QName key) { + Object obj = get(key); + if (obj == null) + return null; + return obj.toString(); + + } // // default String attr(Object key) { // return key != null ? attr(key.toString()) : attr(null);