
org.solovyev.common.collections.tree.UnmodifiableTree Maven / Gradle / Ivy
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ---------------------------------------------------------------------
* Contact details
*
* Email: [email protected]
* Site: http://se.solovyev.org
*/
package org.solovyev.common.collections.tree;
import org.solovyev.common.JPredicate;
import javax.annotation.Nonnull;
import java.util.List;
class UnmodifiableTree implements MutableTree {
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nonnull
private final MutableTree t;
/*
**********************************************************************
*
* CONSTRUCTOR
*
**********************************************************************
*/
private UnmodifiableTree(@Nonnull MutableTree t) {
this.t = t;
}
public static UnmodifiableTree wrap(@Nonnull MutableTree t) {
return new UnmodifiableTree(t);
}
public static UnmodifiableTree wrap(@Nonnull Tree t) {
if (t instanceof MutableTree) {
return wrap((MutableTree) t);
} else {
return new UnmodifiableTree(TreeAdapter.adapt(t));
}
}
/*
**********************************************************************
*
* METHODS
*
**********************************************************************
*/
@Nonnull
@Override
public MutableTreeNode getRoot() {
return t.getRoot();
}
@Override
public int getSize() {
return t.getSize();
}
@Nonnull
@Override
public TreeIterator iterator() {
return t.iterator();
}
@Nonnull
@Override
public TreeIterator getIterator() {
return t.getIterator();
}
@Override
public void removeNodeIf(@Nonnull JPredicate super TreeNode> filter) {
throw new UnsupportedOperationException();
}
@Nonnull
@Override
public List extends MutableTreeNode> getAllNodes() {
return t.getAllNodes();
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static class TreeAdapter implements MutableTree {
@Nonnull
private final Tree tree;
private TreeAdapter(@Nonnull Tree tree) {
this.tree = tree;
}
@Nonnull
private static TreeAdapter adapt(@Nonnull Tree tree) {
return new TreeAdapter(tree);
}
@Override
@Nonnull
public MutableTreeNode getRoot() {
return UnmodifiableTreeNode.wrap(tree.getRoot());
}
@Override
public void removeNodeIf(@Nonnull JPredicate super TreeNode> filter) {
throw new AssertionError("Should never happen!");
}
@Override
public int getSize() {
return tree.getSize();
}
@Nonnull
@Override
public TreeIterator iterator() {
return tree.iterator();
}
@Override
@Nonnull
public TreeIterator getIterator() {
return tree.getIterator();
}
@Override
@Nonnull
public List extends MutableTreeNode> getAllNodes() {
return Trees.unmodifiableTreeNodes(tree.getAllNodes());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy