Package: CardinalityConstraintsValidator
CardinalityConstraintsValidator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CardinalityConstraintsValidator() |
|
|
|
|
|
||||||||||||||||||||
extractValueCount(Object) |
|
|
|
|
|
||||||||||||||||||||
validate(Field, Object) |
|
|
|
|
|
||||||||||||||||||||
validate(FieldSpecification, Object) |
|
|
|
|
|
||||||||||||||||||||
validateNonEmpty(Attribute, int) |
|
|
|
|
|
||||||||||||||||||||
validateNonEmpty(Field, int, ParticipationConstraints) |
|
|
|
|
|
||||||||||||||||||||
validateParticipationConstraint(Field, int, ParticipationConstraint) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.validator;
16:
17: import cz.cvut.kbss.jopa.exceptions.CardinalityConstraintViolatedException;
18: import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraint;
19: import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
20: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.utils.ErrorUtils;
23:
24: import java.lang.reflect.Field;
25: import java.util.Collection;
26: import java.util.Objects;
27:
28: /**
29: * Validator of participation constraints.
30: */
31: class CardinalityConstraintsValidator extends IntegrityConstraintsValidator {
32:
33: CardinalityConstraintsValidator() {
34: }
35:
36: /**
37: * Validates cardinality constraints defined in the {@link ParticipationConstraints} annotation.
38: *
39: * @param field Field on which the constraints are defined
40: * @param value The value to validate
41: */
42: public void validate(Field field, Object value) {
43: Objects.requireNonNull(field, ErrorUtils.constructNPXMessage("field"));
44: final ParticipationConstraints constraints = field.getAnnotation(ParticipationConstraints.class);
45:• if (constraints == null) {
46: return;
47: }
48: final int valueCount = extractValueCount(value);
49:• for (ParticipationConstraint pc : constraints.value()) {
50: validateParticipationConstraint(field, valueCount, pc);
51: }
52:• if (constraints.value().length == 0) {
53: validateNonEmpty(field, valueCount, constraints);
54: }
55: }
56:
57: @Override
58: public void validate(FieldSpecification<?, ?> attribute, Object attributeValue) {
59:• if (!(attribute instanceof Attribute)) {
60: // Only proper attributes can have cardinality constraints
61: return;
62: }
63: final Attribute<?, ?> att = (Attribute<?, ?>) attribute;
64: final int valueCount = extractValueCount(attributeValue);
65:• for (ParticipationConstraint pc : att.getConstraints()) {
66: validateParticipationConstraint(att.getJavaField(), valueCount, pc);
67: }
68:• if (att.getConstraints().length == 0) {
69: validateNonEmpty(att, valueCount);
70: }
71: }
72:
73: private int extractValueCount(Object value) {
74:• if (value == null) {
75: return 0;
76: }
77:• return value instanceof Collection ? ((Collection<?>) value).size() : 1;
78: }
79:
80: private void validateParticipationConstraint(Field field, int valueCount, ParticipationConstraint pc) {
81:• if (valueCount < pc.min()) {
82: throw new CardinalityConstraintViolatedException("At least " + pc.min() +
83: " values of attribute " + field.getDeclaringClass().getSimpleName() + "." + field.getName() +
84: " expected, but got only " + valueCount);
85: }
86:• if (pc.max() >= 0 && pc.max() < valueCount) {
87: throw new CardinalityConstraintViolatedException("At most " + pc.max() +
88: " values of attribute " + field.getDeclaringClass().getSimpleName() + "." + field.getName() +
89: " expected, but got " + valueCount);
90: }
91: }
92:
93: private void validateNonEmpty(Field field, int valueCount, ParticipationConstraints constraints) {
94:• if (valueCount == 0 && constraints.nonEmpty()) {
95: throw new CardinalityConstraintViolatedException(
96: "Attribute " + field.getDeclaringClass() + "." + field.getName() +
97: " was marked as nonEmpty, but contains no value.");
98: }
99: }
100:
101: private void validateNonEmpty(Attribute<?, ?> attribute, int valueCount) {
102:• if (valueCount == 0 && attribute.isNonEmpty()) {
103: throw new CardinalityConstraintViolatedException(
104: "Attribute " + attribute.getDeclaringType().getJavaType().getSimpleName() + "." +
105: attribute.getName() + " was marked as nonEmpty, but contains no value.");
106: }
107: }
108: }