/* * 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.data.impl.schema.tree; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.Collections2; import java.util.Collection; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode { protected AbstractRecursiveCandidateNode(final NormalizedNodeContainer> data) { super(data); } @SuppressWarnings("unchecked") static DataTreeCandidateNode deleteNode(final NormalizedNode data) { if (data instanceof NormalizedNodeContainer) { return new RecursiveDeleteCandidateNode((NormalizedNodeContainer>) data); } return new DeleteLeafCandidateNode(data); } @SuppressWarnings("unchecked") static DataTreeCandidateNode replaceNode(final NormalizedNode oldData, final NormalizedNode newData) { if (isContainer(oldData)) { return new RecursiveReplaceCandidateNode((NormalizedNodeContainer>) oldData, (NormalizedNodeContainer>) newData); } return new ReplaceLeafCandidateNode(oldData, newData); } @SuppressWarnings("unchecked") static DataTreeCandidateNode unmodifiedNode(final NormalizedNode data) { if (data instanceof NormalizedNodeContainer) { return new RecursiveUnmodifiedCandidateNode((NormalizedNodeContainer>) data); } return new UnmodifiedLeafCandidateNode(data); } @SuppressWarnings("unchecked") static DataTreeCandidateNode writeNode(final NormalizedNode data) { if (data instanceof NormalizedNodeContainer) { return new RecursiveWriteCandidateNode((NormalizedNodeContainer>) data); } return new WriteLeafCandidateNode(data); } protected static boolean isContainer(final NormalizedNode data) { return data instanceof NormalizedNodeContainer; } @Override public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) { final Optional> potential = getData().getChild(identifier); if (potential.isPresent()) { return createChild(potential.get()); } return null; } @Override public final Collection getChildNodes() { return Collections2.transform(getData().getValue(), new Function, DataTreeCandidateNode>() { @Override public DataTreeCandidateNode apply(final NormalizedNode input) { return createChild(input); } }); } @SuppressWarnings("unchecked") private DataTreeCandidateNode createChild(final NormalizedNode childData) { if (isContainer(childData)) { return createContainer((NormalizedNodeContainer>) childData); } else { return createLeaf(childData); } } protected abstract DataTreeCandidateNode createContainer(NormalizedNodeContainer> childData); protected abstract DataTreeCandidateNode createLeaf(NormalizedNode childData); }