All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.common.collections.tree.UnmodifiableTree Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
/*
 * 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> filter) {
		throw new UnsupportedOperationException();
	}

	@Nonnull
	@Override
	public List> 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> 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> getAllNodes() {
			return Trees.unmodifiableTreeNodes(tree.getAllNodes());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy