-
Notifications
You must be signed in to change notification settings - Fork 5
/
PropertyConstraint.java
53 lines (48 loc) · 2.18 KB
/
PropertyConstraint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.genericsystem.defaults.constraints;
import java.io.Serializable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.annotations.Components;
import org.genericsystem.api.core.annotations.Meta;
import org.genericsystem.api.core.annotations.Supers;
import org.genericsystem.api.core.annotations.SystemGeneric;
import org.genericsystem.api.core.annotations.constraints.InstanceValueClassConstraint;
import org.genericsystem.api.core.exceptions.ConstraintViolationException;
import org.genericsystem.defaults.DefaultConfig.MetaAttribute;
import org.genericsystem.defaults.DefaultConfig.SystemMap;
import org.genericsystem.defaults.DefaultGeneric;
import org.genericsystem.defaults.constraints.Constraint.CheckableConstraint;
import org.genericsystem.defaults.exceptions.PropertyConstraintViolationException;
/**
* Represents the constraint to allow only one value for an attribute.
*
* @author Nicolas Feybesse
*
* @param <T>
* the implementation of DefaultVertex.
*/
@SystemGeneric
@Meta(MetaAttribute.class)
@Supers(SystemMap.class)
@Components(MetaAttribute.class)
@InstanceValueClassConstraint(Boolean.class)
@org.genericsystem.api.core.annotations.constraints.PropertyConstraint
public class PropertyConstraint<T extends DefaultGeneric<T>> implements CheckableConstraint<T> {
@Override
public void check(T modified, T attribute, Serializable value) throws ConstraintViolationException {
T base = modified.getBaseComponent();
Snapshot<T> snapshot = new Snapshot<T>() {
@Override
public Stream<T> unfilteredStream() {
return base.getHolders(attribute).stream().filter(x -> modified.getComponents().equals(x.getComponents()) && modified.getMeta().equals(x.getMeta()));
}
};
if (snapshot.size() > 1)
throw new PropertyConstraintViolationException("For attribute : " + attribute + " these holders violates property constraint : \n" + snapshot.stream().map(x -> x.info() + "\n").collect(Collectors.toList()));
}
@Override
public boolean isCheckable(T modified, boolean isOnAdd, boolean isFlushTime, boolean isRevert) {
return isOnAdd || (modified.getValue() == null);
}
}