Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/dns/NameNode.java
38924 views
/*1* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.dns;262728import java.util.Hashtable;293031/**32* A NameNode represents a node in the DNS namespace. Each node33* has a label, which is its name relative to its parent (so the34* node at Sun.COM has label "Sun"). Each node has a hashtable of35* children indexed by their labels converted to lower-case.36*37* <p> A node may be addressed from another by giving a DnsName38* consisting of the sequence of labels from one node to the other.39*40* <p> Each node also has an <tt>isZoneCut</tt> flag, used to indicate41* if the node is a zone cut. A zone cut is a node with an NS record42* that is contained in one zone, but that actually belongs to a child zone.43*44* <p> All access is unsynchronized.45*46* @author Scott Seligman47*/484950class NameNode {5152private String label; // name of this node relative to its53// parent, or null for root of a tree54private Hashtable<String,NameNode> children = null; // child nodes55private boolean isZoneCut = false; // true if this node is a zone cut56private int depth = 0; // depth in tree (0 for root)5758NameNode(String label) {59this.label = label;60}6162/*63* Returns a newly-allocated NameNode. Used to allocate new nodes64* in a tree. Should be overridden in a subclass to return an object65* of the subclass's type.66*/67protected NameNode newNameNode(String label) {68return new NameNode(label);69}7071/*72* Returns the name of this node relative to its parent, or null for73* the root of a tree.74*/75String getLabel() {76return label;77}7879/*80* Returns the depth of this node in the tree. The depth of the root81* is 0.82*/83int depth() {84return depth;85}8687boolean isZoneCut() {88return isZoneCut;89}9091void setZoneCut(boolean isZoneCut) {92this.isZoneCut = isZoneCut;93}9495/*96* Returns the children of this node, or null if there are none.97* The caller must not modify the Hashtable returned.98*/99Hashtable<String,NameNode> getChildren() {100return children;101}102103/*104* Returns the child node given the hash key (the down-cased label)105* for its name relative to this node, or null if there is no such106* child.107*/108NameNode get(String key) {109return (children != null)110? children.get(key)111: null;112}113114/*115* Returns the node at the end of a path, or null if the116* node does not exist.117* The path is specified by the labels of <tt>name</tt>, beginning118* at index idx.119*/120NameNode get(DnsName name, int idx) {121NameNode node = this;122for (int i = idx; i < name.size() && node != null; i++) {123node = node.get(name.getKey(i));124}125return node;126}127128/*129* Returns the node at the end of a path, creating it and any130* intermediate nodes as needed.131* The path is specified by the labels of <tt>name</tt>, beginning132* at index idx.133*/134NameNode add(DnsName name, int idx) {135NameNode node = this;136for (int i = idx; i < name.size(); i++) {137String label = name.get(i);138String key = name.getKey(i);139140NameNode child = null;141if (node.children == null) {142node.children = new Hashtable<>();143} else {144child = node.children.get(key);145}146if (child == null) {147child = newNameNode(label);148child.depth = node.depth + 1;149node.children.put(key, child);150}151node = child;152}153return node;154}155}156157158