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

inet.ipaddr.format.util.AddedTree Maven / Gradle / Ivy

There is a newer version: 5.5.1
Show newest version
/*
 * Copyright 2022 Sean C Foley
 *
 * 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
 *     or at
 *     https://github.com/seancfoley/IPAddress/blob/master/LICENSE
 *
 * 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.
 */

package inet.ipaddr.format.util;

import java.util.List;

import inet.ipaddr.Address;
import inet.ipaddr.format.util.AddressTrie.SubNodesMappingBasic;
import inet.ipaddr.format.util.AssociativeAddressTrie.AssociativeTrieNode;

/** 
 * AddedTree is an alternative non-binary tree data structure originating from a binary trie
 * with the possible exception of the root, which matches the root node of the original.
 * The root may or may not be an added node from the original trie.
 * This tree is also read-only and is generated from the originating trie,
 * but does not change in concert with changes to the original trie.
 */
public class AddedTree extends AddedTreeBase> {

	public AddedTree(AssociativeAddressTrie> wrapped) {
		super(wrapped);
	}

	/** 
	 * AddedTreeNode represents a node in an AddedTree.
	 */
	public static class AddedTreeNode extends AddedTreeNodeBase> {

		public AddedTreeNode(AssociativeTrieNode> node) {
			super(node);
		}
		
		@Override
		public AddedTreeNode[] getSubNodes() {
			SubNodesMappingBasic value = node.getValue();
			if(value == null) {
				return null;
			}
			List>> subNodes = value.subNodes;  
			if(subNodes == null || subNodes.size() == 0) {
				return null;
			}
			@SuppressWarnings("unchecked")
			AddedTreeNode[] nodes = (AddedTreeNode[]) new AddedTreeNode[subNodes.size()];
			for(int i = 0; i < nodes.length; i++) {
				nodes[i] = new AddedTreeNode(subNodes.get(i));
			}
			return nodes;
		}
	}
	
	/**
	 * Returns the root of this tree, which corresponds to the root of the originating trie.
	 */
	@Override
	public AddedTreeNode getRoot()  {
		return new AddedTreeNode(wrapped.getRoot());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy