]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.acr/src/org/argeo/api/acr/search/ContentFilter.java
1 package org
.argeo
.api
.acr
.search
;
3 import java
.util
.HashSet
;
5 import java
.util
.function
.Consumer
;
7 import javax
.xml
.namespace
.QName
;
9 import org
.argeo
.api
.acr
.QNamed
;
11 /** A constraint filtering based ona given composition (and/or). */
12 public abstract class ContentFilter
<COMPOSITION
extends Composition
> implements Constraint
{
13 private Set
<Constraint
> constraintss
= new HashSet
<>();
15 private COMPOSITION composition
;
17 boolean negateNextOperator
= false;
19 @SuppressWarnings("unchecked")
20 ContentFilter(Class
<COMPOSITION
> clss
) {
22 this.composition
= null;
23 else if (Intersection
.class.isAssignableFrom(clss
))
24 this.composition
= (COMPOSITION
) new Intersection(this);
25 else if (Union
.class.isAssignableFrom(clss
))
26 this.composition
= (COMPOSITION
) new Union(this);
28 throw new IllegalArgumentException("Unkown composition " + clss
);
35 public COMPOSITION
all(Consumer
<AndFilter
> and
) {
36 AndFilter subFilter
= new AndFilter();
37 and
.accept(subFilter
);
38 addConstraint(subFilter
);
42 public COMPOSITION
any(Consumer
<OrFilter
> or
) {
43 OrFilter subFilter
= new OrFilter();
45 addConstraint(subFilter
);
49 public ContentFilter
<COMPOSITION
> not() {
50 negateNextOperator
= !negateNextOperator
;
57 public COMPOSITION
isContentClass(QName
... contentClass
) {
58 addConstraint(new IsContentClass(contentClass
));
62 public COMPOSITION
isContentClass(QNamed
... contentClass
) {
63 addConstraint(new IsContentClass(contentClass
));
68 * COMPARISON OPERATORS
71 public COMPOSITION
eq(QName attr
, Object value
) {
72 addConstraint(new Eq(attr
, value
));
76 public COMPOSITION
eq(QNamed attr
, Object value
) {
77 addConstraint(new Eq(attr
.qName(), value
));
84 protected void addConstraint(Constraint operator
) {
86 Constraint operatorToAdd
;
87 if (negateNextOperator
) {
88 operatorToAdd
= new Not(operator
);
89 negateNextOperator
= false;
91 operatorToAdd
= operator
;
93 constraintss
.add(operatorToAdd
);
96 /** Checks that the root operator is not set. */
97 private void checkAddConstraint() {
98 if (composition
== null && !constraintss
.isEmpty())
99 throw new IllegalStateException("An operator is already registered (" + constraintss
.iterator().next()
100 + ") and no composition is defined");
106 public Set
<Constraint
> getConstraints() {
110 public boolean isUnion() {
111 return composition
instanceof Union
;
118 public static class Not
implements Constraint
{
119 final Constraint negated
;
121 public Not(Constraint negated
) {
122 this.negated
= negated
;
125 public Constraint
getNegated() {
131 public static class Eq
implements Constraint
{
135 public Eq(QName prop
, Object value
) {
141 public QName
getProp() {
145 public Object
getValue() {
151 public static class IsContentClass
implements Constraint
{
152 final QName
[] contentClasses
;
154 public IsContentClass(QName
[] contentClasses
) {
155 this.contentClasses
= contentClasses
;
158 public IsContentClass(QNamed
[] contentClasses
) {
159 this.contentClasses
= new QName
[contentClasses
.length
];
160 for (int i
= 0; i
< contentClasses
.length
; i
++)
161 this.contentClasses
[i
] = contentClasses
[i
].qName();
164 public QName
[] getContentClasses() {
165 return contentClasses
;
170 // public static void main(String[] args) {
171 // AndFilter filter = new AndFilter();
172 // filter.eq(new QName("test"), "test").and().not().eq(new QName("type"), "integer");
174 // OrFilter unionFilter = new OrFilter();
175 // unionFilter.all((f) -> {
176 // f.eq(DName.displayname, "").and().eq(DName.creationdate, "");
177 // }).or().not().any((f) -> {
178 // f.eq(DName.creationdate, "").or().eq(DName.displayname, "");