]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.acr/src/org/argeo/api/acr/QualifiedData.java
Releasing
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / QualifiedData.java
1 package org.argeo.api.acr;
2
3 import static org.argeo.api.acr.NamespaceUtils.unqualified;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Optional;
8
9 import javax.xml.namespace.QName;
10
11 /** A {@link StructuredData} whose attributes have qualified keys. */
12 public interface QualifiedData<CHILD extends QualifiedData<CHILD>> extends StructuredData<QName, Object, CHILD> {
13 QName getName();
14
15 CHILD getParent();
16
17 /*
18 * ATTRIBUTES OPERATIONS
19 */
20
21 <A> Optional<A> get(QName key, Class<A> clss);
22
23 Class<?> getType(QName key);
24
25 boolean isMultiple(QName key);
26
27 <A> List<A> getMultiple(QName key, Class<A> clss);
28
29 /*
30 * PATH
31 */
32 char getPathSeparator();
33
34 /*
35 * ATTRIBUTES OPERATION HELPERS
36 */
37 default boolean containsKey(QNamed key) {
38 return containsKey(key.qName());
39 }
40
41 default <A> Optional<A> get(QNamed key, Class<A> clss) {
42 return get(key.qName(), clss);
43 }
44
45 default Object get(QNamed key) {
46 return get(key.qName());
47 }
48
49 default Object put(QNamed key, Object value) {
50 return put(key.qName(), value);
51 }
52
53 default Object remove(QNamed key) {
54 return remove(key.qName());
55 }
56
57 // TODO do we really need the helpers below?
58
59 default Object get(String key) {
60 return get(unqualified(key));
61 }
62
63 default Object put(String key, Object value) {
64 return put(unqualified(key), value);
65 }
66
67 default Object remove(String key) {
68 return remove(unqualified(key));
69 }
70
71 @SuppressWarnings("unchecked")
72 default <A> List<A> getMultiple(QName key) {
73 Class<A> type;
74 try {
75 type = (Class<A>) getType(key);
76 } catch (ClassCastException e) {
77 throw new IllegalArgumentException("Requested type is not the default type");
78 }
79 List<A> res = getMultiple(key, type);
80 return res;
81 }
82
83 /*
84 * CHILDREN
85 */
86
87 default boolean hasChild(QName name) {
88 for (CHILD child : this) {
89 if (child.getName().equals(name))
90 return true;
91 }
92 return false;
93 }
94
95 default boolean hasChild(QNamed name) {
96 return hasChild(name.qName());
97 }
98
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))
103 return child;
104 }
105 return null;
106 }
107
108 default List<CHILD> children(QName name) {
109 List<CHILD> res = new ArrayList<>();
110 for (CHILD child : this) {
111 if (child.getName().equals(name))
112 res.add(child);
113 }
114 return res;
115 }
116
117 default List<CHILD> children(QNamed name) {
118 return children(name.qName());
119 }
120
121 default Optional<CHILD> soleChild(QNamed name) {
122 return soleChild(name.qName());
123 }
124
125 default Optional<CHILD> soleChild(QName name) {
126 List<CHILD> res = children(name);
127 if (res.isEmpty())
128 return Optional.empty();
129 if (res.size() > 1)
130 throw new IllegalStateException(this + " has multiple children with name " + name);
131 return Optional.of(res.get(0));
132 }
133
134 default CHILD child(QName name) {
135 return soleChild(name).orElseThrow();
136 }
137
138 default CHILD child(QNamed name) {
139 return child(name.qName());
140 }
141
142 /*
143 * ATTR AS STRING
144 */
145 /**
146 * Convenience method returning an attribute as a {@link String}.
147 *
148 * @param key the attribute name
149 * @return the attribute value as a {@link String} or <code>null</code>.
150 *
151 * @see Object#toString()
152 */
153 default String attr(QName key) {
154 return get(key, String.class).orElse(null);
155 }
156
157 /**
158 * Convenience method returning an attribute as a {@link String}.
159 *
160 * @param key the attribute name
161 * @return the attribute value as a {@link String} or <code>null</code>.
162 *
163 * @see Object#toString()
164 */
165 default String attr(QNamed key) {
166 return attr(key.qName());
167 }
168
169 /**
170 * Convenience method returning an attribute as a {@link String}.
171 *
172 * @param key the attribute name
173 * @return the attribute value as a {@link String} or <code>null</code>.
174 *
175 * @see Object#toString()
176 */
177 default String attr(String key) {
178 return attr(unqualified(key));
179 }
180
181 /*
182 * SIBLINGS
183 */
184 default int getSiblingIndex() {
185 return 1;
186 }
187 }