1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.net; |
---|
19 | |
---|
20 | /** A base class that implements interface Node |
---|
21 | * |
---|
22 | */ |
---|
23 | |
---|
24 | public class NodeBase implements Node { |
---|
25 | public final static char PATH_SEPARATOR = '/'; |
---|
26 | public final static String PATH_SEPARATOR_STR = "/"; |
---|
27 | public final static String ROOT = ""; // string representation of root |
---|
28 | |
---|
29 | protected String name; //host:port# |
---|
30 | protected String location; //string representation of this node's location |
---|
31 | protected int level; //which level of the tree the node resides |
---|
32 | protected Node parent; //its parent |
---|
33 | |
---|
34 | /** Default constructor */ |
---|
35 | public NodeBase() { |
---|
36 | } |
---|
37 | |
---|
38 | /** Construct a node from its path |
---|
39 | * @param path |
---|
40 | * a concatenation of this node's location, the path seperator, and its name |
---|
41 | */ |
---|
42 | public NodeBase(String path) { |
---|
43 | path = normalize(path); |
---|
44 | int index = path.lastIndexOf(PATH_SEPARATOR); |
---|
45 | if (index== -1) { |
---|
46 | set(ROOT, path); |
---|
47 | } else { |
---|
48 | set(path.substring(index+1), path.substring(0, index)); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** Construct a node from its name and its location |
---|
53 | * @param name this node's name |
---|
54 | * @param location this node's location |
---|
55 | */ |
---|
56 | public NodeBase(String name, String location) { |
---|
57 | set(name, normalize(location)); |
---|
58 | } |
---|
59 | |
---|
60 | /** Construct a node from its name and its location |
---|
61 | * @param name this node's name |
---|
62 | * @param location this node's location |
---|
63 | * @param parent this node's parent node |
---|
64 | * @param level this node's level in the tree |
---|
65 | */ |
---|
66 | public NodeBase(String name, String location, Node parent, int level) { |
---|
67 | set(name, normalize(location)); |
---|
68 | this.parent = parent; |
---|
69 | this.level = level; |
---|
70 | } |
---|
71 | |
---|
72 | /* set this node's name and location */ |
---|
73 | private void set(String name, String location) { |
---|
74 | if (name != null && name.contains(PATH_SEPARATOR_STR)) |
---|
75 | throw new IllegalArgumentException( |
---|
76 | "Network location name contains /: "+name); |
---|
77 | this.name = (name==null)?"":name; |
---|
78 | this.location = location; |
---|
79 | } |
---|
80 | |
---|
81 | /** Return this node's name */ |
---|
82 | public String getName() { return name; } |
---|
83 | |
---|
84 | /** Return this node's network location */ |
---|
85 | public String getNetworkLocation() { return location; } |
---|
86 | |
---|
87 | /** Set this node's network location */ |
---|
88 | public void setNetworkLocation(String location) { this.location = location; } |
---|
89 | |
---|
90 | /** Return this node's path */ |
---|
91 | public static String getPath(Node node) { |
---|
92 | return node.getNetworkLocation()+PATH_SEPARATOR_STR+node.getName(); |
---|
93 | } |
---|
94 | |
---|
95 | /** Return this node's string representation */ |
---|
96 | public String toString() { |
---|
97 | return getPath(this); |
---|
98 | } |
---|
99 | |
---|
100 | /** Normalize a path */ |
---|
101 | static public String normalize(String path) { |
---|
102 | if (path == null || path.length() == 0) return ROOT; |
---|
103 | |
---|
104 | if (path.charAt(0) != PATH_SEPARATOR) { |
---|
105 | throw new IllegalArgumentException( |
---|
106 | "Network Location path does not start with " |
---|
107 | +PATH_SEPARATOR_STR+ ": "+path); |
---|
108 | } |
---|
109 | |
---|
110 | int len = path.length(); |
---|
111 | if (path.charAt(len-1) == PATH_SEPARATOR) { |
---|
112 | return path.substring(0, len-1); |
---|
113 | } |
---|
114 | return path; |
---|
115 | } |
---|
116 | |
---|
117 | /** Return this node's parent */ |
---|
118 | public Node getParent() { return parent; } |
---|
119 | |
---|
120 | /** Set this node's parent */ |
---|
121 | public void setParent(Node parent) { |
---|
122 | this.parent = parent; |
---|
123 | } |
---|
124 | |
---|
125 | /** Return this node's level in the tree. |
---|
126 | * E.g. the root of a tree returns 0 and its children return 1 |
---|
127 | */ |
---|
128 | public int getLevel() { return level; } |
---|
129 | |
---|
130 | /** Set this node's level in the tree */ |
---|
131 | public void setLevel(int level) { |
---|
132 | this.level = level; |
---|
133 | } |
---|
134 | } |
---|