/* * 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 static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import java.net.URI; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Objects; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware; import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; public final class AugmentEffectiveStatementImpl extends AbstractEffectiveDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable { private final SchemaPath targetPath; private final URI namespace; private final Date revision; private final int order; private final List unknownNodes; private final RevisionAwareXPath whenCondition; private final AugmentationSchema copyOf; public AugmentEffectiveStatementImpl( final StmtContext> ctx) { super(ctx); this.targetPath = ctx.getStatementArgument().asSchemaPath(); QNameModule rootModuleQName = Utils.getRootModuleQName(ctx); this.namespace = rootModuleQName.getNamespace(); this.revision = rootModuleQName.getRevision(); this.order = ctx.getOrder(); this.copyOf = ctx.getOriginalCtx() == null ? null : (AugmentationSchema) ctx.getOriginalCtx().buildEffective(); WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class); this.whenCondition = (whenStmt == null) ? null : whenStmt.argument(); // initSubstatementCollections Collection> effectiveSubstatements = effectiveSubstatements(); ImmutableList.Builder listBuilder = new ImmutableList.Builder<>(); for (EffectiveStatement effectiveStatement : effectiveSubstatements) { if (effectiveStatement instanceof UnknownSchemaNode) { listBuilder.add((UnknownSchemaNode) effectiveStatement); } } this.unknownNodes = listBuilder.build(); } @Override public Optional getOriginalDefinition() { return Optional.fromNullable(this.copyOf); } @Override public SchemaPath getTargetPath() { return targetPath; } @Override public RevisionAwareXPath getWhenCondition() { return whenCondition; } @Override public List getUnknownSchemaNodes() { return unknownNodes; } @Override public URI getNamespace() { return namespace; } @Override public Date getRevision() { return revision; } @Override public int hashCode() { final int prime = 17; int result = 1; result = prime * result + Objects.hashCode(targetPath); result = prime * result + Objects.hashCode(whenCondition); result = prime * result + getChildNodes().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; } AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj; if (!Objects.equals(targetPath, other.targetPath)) { return false; } if (!Objects.equals(whenCondition, other.whenCondition)) { return false; } if (!getChildNodes().equals(other.getChildNodes())) { return false; } return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName()); sb.append("["); sb.append("targetPath=").append(targetPath); sb.append(", when=").append(whenCondition); sb.append("]"); return sb.toString(); } @Override public int compareTo(final AugmentEffectiveStatementImpl o) { checkNotNull(o); Iterator thisIt = this.targetPath.getPathFromRoot().iterator(); Iterator otherIt = o.getTargetPath().getPathFromRoot().iterator(); while (thisIt.hasNext()) { if (otherIt.hasNext()) { int comp = thisIt.next().compareTo(otherIt.next()); if (comp != 0) { return comp; } } else { return 1; } } if (otherIt.hasNext()) { return -1; } return this.order - o.order; } }