--- /dev/null
+/**
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+
+import com.google.common.base.MoreObjects;
+import java.util.Objects;
+import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+
+/**
+ * Utility methods for ConstraintDefinition implementations.
+ */
+final class ConstraintDefinitions {
+ private ConstraintDefinitions() {
+ throw new UnsupportedOperationException();
+ }
+
+ static int hashCode(final ConstraintDefinition def) {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Objects.hashCode(def.getWhenCondition());
+ result = prime * result + Objects.hashCode(def.getMustConstraints());
+ result = prime * result + Objects.hashCode(def.getMinElements());
+ result = prime * result + Objects.hashCode(def.getMaxElements());
+ result = prime * result + Objects.hashCode(def.isMandatory());
+ return result;
+ }
+
+ static boolean equals(final ConstraintDefinition def, final Object obj) {
+ if (def == obj) {
+ return true;
+ }
+ if (!(obj instanceof ConstraintDefinition)) {
+ return false;
+ }
+ final ConstraintDefinition other = (EffectiveConstraintDefinitionImpl) obj;
+ if (def.isMandatory() != other.isMandatory()) {
+ return false;
+ }
+ if (!Objects.equals(def.getWhenCondition(), other.getWhenCondition())) {
+ return false;
+ }
+ if (!Objects.equals(def.getMustConstraints(), other.getMustConstraints())) {
+ return false;
+ }
+ if (!Objects.equals(def.getMinElements(), other.getMinElements())) {
+ return false;
+ }
+ if (!Objects.equals(def.getMaxElements(), other.getMaxElements())) {
+ return false;
+ }
+ return true;
+ }
+
+ static String toString(final ConstraintDefinition def) {
+ return MoreObjects.toStringHelper(def).omitNullValues()
+ .add("whenCondition", def.getWhenCondition())
+ .add("mustConstraints", def.getMustConstraints())
+ .add("mandatory", def.isMandatory())
+ .add("minElements", def.getMinElements())
+ .add("maxElements", def.getMaxElements()).toString();
+ }
+}
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
-import java.util.Collection;
-import java.util.Objects;
import java.util.Set;
import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
import org.opendaylight.yangtools.yang.model.api.MustDefinition;
import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
-public final class EffectiveConstraintDefinitionImpl implements ConstraintDefinition {
+final class EffectiveConstraintDefinitionImpl implements ConstraintDefinition {
private static final Integer UNBOUNDED_INT = Integer.MAX_VALUE;
private static final String UNBOUNDED_STR = "unbounded";
private final RevisionAwareXPath whenCondition;
private final Set<MustDefinition> mustConstraints;
- private final Boolean mandatory;
private final Integer minElements;
private final Integer maxElements;
+ private final boolean mandatory;
+
+ private EffectiveConstraintDefinitionImpl(final boolean mandatory, final Integer minElements,
+ final Integer maxElements, final RevisionAwareXPath whenCondition,
+ final Set<MustDefinition> mustConstraints) {
+ this.mandatory = mandatory;
+ this.minElements = Preconditions.checkNotNull(minElements);
+ this.maxElements = Preconditions.checkNotNull(maxElements);
+ this.whenCondition = whenCondition;
+ this.mustConstraints = Preconditions.checkNotNull(mustConstraints);
+ }
- public EffectiveConstraintDefinitionImpl(final EffectiveStatementBase<?, ?> parent) {
-
- WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
- this.whenCondition = (firstWhenStmt == null) ? null : firstWhenStmt.argument();
-
- MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
+ static ConstraintDefinition forParent(final EffectiveStatementBase<?, ?> parent) {
+ final MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
.firstEffective(MinElementsEffectiveStatementImpl.class);
- this.minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
+ final Integer minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
- MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
+ final MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
.firstEffective(MaxElementsEffectiveStatementImpl.class);
- String maxElementsArg = (firstMaxElementsStmt == null) ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
+ final String maxElementsArg = (firstMaxElementsStmt == null) ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
+ final Integer maxElements;
if (UNBOUNDED_STR.equals(maxElementsArg)) {
- this.maxElements = UNBOUNDED_INT;
+ maxElements = UNBOUNDED_INT;
} else {
- this.maxElements = Integer.valueOf(maxElementsArg);
+ maxElements = Integer.valueOf(maxElementsArg);
}
- MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
+ final MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
.firstEffective(MandatoryEffectiveStatementImpl.class);
- this.mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
+ final boolean mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
+
+ final Set<MustDefinition> mustSubstatements = ImmutableSet.copyOf(parent.allSubstatementsOfType(
+ MustDefinition.class));
+ final WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
- Collection<MustDefinition> mustSubstatements = parent.allSubstatementsOfType(MustDefinition.class);
- this.mustConstraints = ImmutableSet.copyOf(mustSubstatements);
+ // Check for singleton instances
+ if (minElements == 0 && maxElements == UNBOUNDED_INT && mustSubstatements.isEmpty() && firstWhenStmt == null) {
+ return EmptyConstraintDefinition.create(mandatory);
+ }
+
+ return new EffectiveConstraintDefinitionImpl(mandatory, minElements, maxElements,
+ (firstWhenStmt == null) ? null : firstWhenStmt.argument(), mustSubstatements);
}
@Override
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Objects.hashCode(whenCondition);
- result = prime * result + Objects.hashCode(mustConstraints);
- result = prime * result + Objects.hashCode(minElements);
- result = prime * result + Objects.hashCode(maxElements);
- result = prime * result + mandatory.hashCode();
- return result;
+ return ConstraintDefinitions.hashCode(this);
}
@Override
public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- EffectiveConstraintDefinitionImpl other = (EffectiveConstraintDefinitionImpl) obj;
- if (!mandatory.equals(other.mandatory)) {
- return false;
- }
- if (!Objects.equals(whenCondition, other.whenCondition)) {
- return false;
- }
- if (!Objects.equals(mustConstraints, other.mustConstraints)) {
- return false;
- }
- if (!Objects.equals(minElements, other.minElements)) {
- return false;
- }
- if (!Objects.equals(maxElements, other.maxElements)) {
- return false;
- }
- return true;
+ return ConstraintDefinitions.equals(this, obj);
}
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(EffectiveConstraintDefinitionImpl.class.getSimpleName());
- sb.append("[");
- sb.append("whenCondition=").append(whenCondition);
- sb.append(", mustConstraints=").append(mustConstraints);
- sb.append(", mandatory=").append(mandatory);
- sb.append(", minElements=").append(minElements);
- sb.append(", maxElements=").append(maxElements);
- sb.append("]");
- return sb.toString();
+ return ConstraintDefinitions.toString(this);
}
}
--- /dev/null
+/**
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
+import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.MustDefinition;
+import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+
+/**
+ * Utility holder for constraint definitions which do not really constrain anything.
+ */
+abstract class EmptyConstraintDefinition implements ConstraintDefinition {
+ private static final EmptyConstraintDefinition MANDATORY = new EmptyConstraintDefinition() {
+ @Override
+ public boolean isMandatory() {
+ return true;
+ }
+ };
+ private static final EmptyConstraintDefinition OPTIONAL = new EmptyConstraintDefinition() {
+ @Override
+ public boolean isMandatory() {
+ return false;
+ }
+ };
+
+ private EmptyConstraintDefinition() {
+ // Hidden on purpose
+ }
+
+ static EmptyConstraintDefinition create(final boolean mandatory) {
+ return mandatory ? MANDATORY : OPTIONAL;
+ }
+
+ @Override
+ public final RevisionAwareXPath getWhenCondition() {
+ return null;
+ }
+
+ @Override
+ public final Set<MustDefinition> getMustConstraints() {
+ return ImmutableSet.of();
+ }
+
+ @Override
+ public final Integer getMinElements() {
+ return 0;
+ }
+
+ @Override
+ public final Integer getMaxElements() {
+ return Integer.MAX_VALUE;
+ }
+
+ @Override
+ public final int hashCode() {
+ return ConstraintDefinitions.hashCode(this);
+ }
+
+ @Override
+ public final boolean equals(final Object obj) {
+ return ConstraintDefinitions.equals(this, obj);
+ }
+
+ @Override
+ public final String toString() {
+ return ConstraintDefinitions.toString(this);
+ }
+}