]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.acr/src/org/argeo/api/acr/QualifiedData.java
1 package org
.argeo
.api
.acr
;
3 import static org
.argeo
.api
.acr
.NamespaceUtils
.unqualified
;
5 import java
.util
.ArrayList
;
7 import java
.util
.Optional
;
9 import javax
.xml
.namespace
.QName
;
11 /** A {@link StructuredData} whose attributes have qualified keys. */
12 public interface QualifiedData
<CHILD
extends QualifiedData
<CHILD
>> extends StructuredData
<QName
, Object
, CHILD
> {
18 * ATTRIBUTES OPERATIONS
21 <A
> Optional
<A
> get(QName key
, Class
<A
> clss
);
23 Class
<?
> getType(QName key
);
25 boolean isMultiple(QName key
);
27 <A
> List
<A
> getMultiple(QName key
, Class
<A
> clss
);
32 char getPathSeparator();
35 * ATTRIBUTES OPERATION HELPERS
37 default boolean containsKey(QNamed key
) {
38 return containsKey(key
.qName());
41 default <A
> Optional
<A
> get(QNamed key
, Class
<A
> clss
) {
42 return get(key
.qName(), clss
);
45 default Object
get(QNamed key
) {
46 return get(key
.qName());
49 default Object
put(QNamed key
, Object value
) {
50 return put(key
.qName(), value
);
53 default Object
remove(QNamed key
) {
54 return remove(key
.qName());
57 // TODO do we really need the helpers below?
59 default Object
get(String key
) {
60 return get(unqualified(key
));
63 default Object
put(String key
, Object value
) {
64 return put(unqualified(key
), value
);
67 default Object
remove(String key
) {
68 return remove(unqualified(key
));
71 @SuppressWarnings("unchecked")
72 default <A
> List
<A
> getMultiple(QName key
) {
75 type
= (Class
<A
>) getType(key
);
76 } catch (ClassCastException e
) {
77 throw new IllegalArgumentException("Requested type is not the default type");
79 List
<A
> res
= getMultiple(key
, type
);
87 default boolean hasChild(QName name
) {
88 for (CHILD child
: this) {
89 if (child
.getName().equals(name
))
95 default boolean hasChild(QNamed name
) {
96 return hasChild(name
.qName());
99 /** Any child with this name, or null if there is none */
100 default CHILD
anyChild(QName name
) {
101 for (CHILD child
: this) {
102 if (child
.getName().equals(name
))
108 default List
<CHILD
> children(QName name
) {
109 List
<CHILD
> res
= new ArrayList
<>();
110 for (CHILD child
: this) {
111 if (child
.getName().equals(name
))
117 default List
<CHILD
> children(QNamed name
) {
118 return children(name
.qName());
121 default Optional
<CHILD
> soleChild(QNamed name
) {
122 return soleChild(name
.qName());
125 default Optional
<CHILD
> soleChild(QName name
) {
126 List
<CHILD
> res
= children(name
);
128 return Optional
.empty();
130 throw new IllegalStateException(this + " has multiple children with name " + name
);
131 return Optional
.of(res
.get(0));
134 default CHILD
child(QName name
) {
135 return soleChild(name
).orElseThrow();
138 default CHILD
child(QNamed name
) {
139 return child(name
.qName());
146 * Convenience method returning an attribute as a {@link String}.
148 * @param key the attribute name
149 * @return the attribute value as a {@link String} or <code>null</code>.
151 * @see Object#toString()
153 default String
attr(QName key
) {
154 return get(key
, String
.class).orElse(null);
158 * Convenience method returning an attribute as a {@link String}.
160 * @param key the attribute name
161 * @return the attribute value as a {@link String} or <code>null</code>.
163 * @see Object#toString()
165 default String
attr(QNamed key
) {
166 return attr(key
.qName());
170 * Convenience method returning an attribute as a {@link String}.
172 * @param key the attribute name
173 * @return the attribute value as a {@link String} or <code>null</code>.
175 * @see Object#toString()
177 default String
attr(String key
) {
178 return attr(unqualified(key
));
184 default int getSiblingIndex() {