/* * Copyright (c) 2013 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.builder.impl; import java.util.Collections; import java.util.HashSet; 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; import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl; import com.google.common.collect.ImmutableSet; public final class ConstraintsBuilder { private static final ConstraintDefinitionImpl EMPTY_CONSTRAINT = new ConstraintDefinitionImpl(); private static final ConstraintDefinitionImpl EMPTY_MANDATORY_CONSTRAINT; static { ConstraintDefinitionImpl c = new ConstraintDefinitionImpl(); c.setMandatory(true); EMPTY_MANDATORY_CONSTRAINT = c; } private final String moduleName; private final int line; private final Set mustDefinitions; private ConstraintDefinitionImpl instance; private RevisionAwareXPath whenStmt; private String whenCondition; private boolean mandatory; private Integer min; private Integer max; public ConstraintsBuilder(final String moduleName, final int line) { this.moduleName = moduleName; this.line = line; mustDefinitions = new HashSet(); } ConstraintsBuilder(final ConstraintsBuilder b) { this.moduleName = b.getModuleName(); this.line = b.getLine(); mustDefinitions = new HashSet(b.getMustDefinitions()); whenCondition = b.getWhenCondition(); mandatory = b.isMandatory(); min = b.getMinElements(); max = b.getMaxElements(); } ConstraintsBuilder(final String moduleName, final int line, final ConstraintDefinition base) { this.moduleName = moduleName; this.line = line; whenStmt = base.getWhenCondition(); mustDefinitions = new HashSet(base.getMustConstraints()); mandatory = base.isMandatory(); min = base.getMinElements(); max = base.getMaxElements(); } public ConstraintDefinition build() { if (instance != null) { return instance; } if (whenStmt == null) { if (whenCondition == null) { whenStmt = null; } else { whenStmt = new RevisionAwareXPathImpl(whenCondition, false); } } ConstraintDefinitionImpl newInstance = new ConstraintDefinitionImpl(); newInstance.setWhenCondition(whenStmt); newInstance.setMandatory(mandatory); newInstance.setMinElements(min); newInstance.setMaxElements(max); if (!mustDefinitions.isEmpty()) { newInstance.setMustConstraints(mustDefinitions); } if (EMPTY_CONSTRAINT.equals(newInstance)) { newInstance = EMPTY_CONSTRAINT; } else if (EMPTY_MANDATORY_CONSTRAINT.equals(newInstance)) { newInstance = EMPTY_MANDATORY_CONSTRAINT; } instance = newInstance; return instance; } public String getModuleName() { return moduleName; } public int getLine() { return line; } public Integer getMinElements() { return min; } public void setMinElements(final Integer minElements) { this.min = minElements; } public Integer getMaxElements() { return max; } public void setMaxElements(final Integer maxElements) { this.max = maxElements; } public Set getMustDefinitions() { return mustDefinitions; } public void addMustDefinition(final MustDefinition must) { mustDefinitions.add(must); } public String getWhenCondition() { return whenCondition; } public void addWhenCondition(final String whenCondition) { this.whenCondition = whenCondition; } public boolean isMandatory() { return mandatory; } public void setMandatory(final boolean mandatory) { this.mandatory = mandatory; } private static final class ConstraintDefinitionImpl implements ConstraintDefinition { private RevisionAwareXPath whenCondition; private Set mustConstraints = Collections.emptySet(); private Boolean mandatory = false; private Integer minElements; private Integer maxElements; @Override public RevisionAwareXPath getWhenCondition() { return whenCondition; } private void setWhenCondition(final RevisionAwareXPath whenCondition) { this.whenCondition = whenCondition; } @Override public Set getMustConstraints() { return mustConstraints; } private void setMustConstraints(final Set mustConstraints) { if (mustConstraints != null) { this.mustConstraints = ImmutableSet.copyOf(mustConstraints); } } @Override public boolean isMandatory() { return mandatory; } private void setMandatory(final boolean mandatory) { this.mandatory = mandatory; } @Override public Integer getMinElements() { return minElements; } private void setMinElements(final Integer minElements) { this.minElements = minElements; } @Override public Integer getMaxElements() { return maxElements; } private void setMaxElements(final Integer maxElements) { this.maxElements = maxElements; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode()); result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode()); result = prime * result + ((minElements == null) ? 0 : minElements.hashCode()); result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode()); result = prime * result + mandatory.hashCode(); return result; } @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj; if (whenCondition == null) { if (other.whenCondition != null) { return false; } } else if (!whenCondition.equals(other.whenCondition)) { return false; } if (mustConstraints == null) { if (other.mustConstraints != null) { return false; } } else if (!mustConstraints.equals(other.mustConstraints)) { return false; } if (mandatory != other.mandatory) { return false; } if (minElements == null) { if (other.minElements != null) { return false; } } else if (!minElements.equals(other.minElements)) { return false; } if (maxElements == null) { if (other.maxElements != null) { return false; } } else if (!maxElements.equals(other.maxElements)) { return false; } return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName()); sb.append("["); sb.append("whenCondition=" + whenCondition); sb.append(", mustConstraints=" + mustConstraints); sb.append(", mandatory=" + mandatory); sb.append(", minElements=" + minElements); sb.append(", maxElements=" + maxElements); sb.append("]"); return sb.toString(); } } }