HADOOP-16156. [Clean-up] Remove NULL check before instanceof and fix checkstyle in InnerNodeImpl. Contributed by Shweta Yakkali.
Signed-off-by: Wei-Chiu Chuang <weichiu@apache.org>
(cherry picked from commit dd4a7633ec
)
This commit is contained in:
parent
f148b29508
commit
99abd35a75
|
@ -41,26 +41,29 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
protected final Map<String, Node> childrenMap = new HashMap<>();
|
protected final Map<String, Node> childrenMap = new HashMap<>();
|
||||||
protected int numOfLeaves;
|
protected int numOfLeaves;
|
||||||
|
|
||||||
/** Construct an InnerNode from a path-like string */
|
/** Construct an InnerNode from a path-like string. */
|
||||||
protected InnerNodeImpl(String path) {
|
protected InnerNodeImpl(String path) {
|
||||||
super(path);
|
super(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construct an InnerNode
|
/** Construct an InnerNode
|
||||||
* from its name, its network location, its parent, and its level */
|
* from its name, its network location, its parent, and its level. */
|
||||||
protected InnerNodeImpl(String name, String location, InnerNode parent, int level) {
|
protected InnerNodeImpl(String name, String location,
|
||||||
|
InnerNode parent, int level) {
|
||||||
super(name, location, parent, level);
|
super(name, location, parent, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Node> getChildren() {return children;}
|
public List<Node> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
/** @return the number of children this node has */
|
/** @return the number of children this node has. */
|
||||||
int getNumOfChildren() {
|
int getNumOfChildren() {
|
||||||
return children.size();
|
return children.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Judge if this node represents a rack
|
/** Judge if this node represents a rack.
|
||||||
* @return true if it has no child or its children are not InnerNodes
|
* @return true if it has no child or its children are not InnerNodes
|
||||||
*/
|
*/
|
||||||
public boolean isRack() {
|
public boolean isRack() {
|
||||||
|
@ -76,7 +79,7 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Judge if this node is an ancestor of node <i>n</i>
|
/** Judge if this node is an ancestor of node <i>n</i>.
|
||||||
*
|
*
|
||||||
* @param n a node
|
* @param n a node
|
||||||
* @return true if this node is an ancestor of <i>n</i>
|
* @return true if this node is an ancestor of <i>n</i>
|
||||||
|
@ -87,7 +90,7 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
startsWith(getPath(this)+NodeBase.PATH_SEPARATOR_STR);
|
startsWith(getPath(this)+NodeBase.PATH_SEPARATOR_STR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Judge if this node is the parent of node <i>n</i>
|
/** Judge if this node is the parent of node <i>n</i>.
|
||||||
*
|
*
|
||||||
* @param n a node
|
* @param n a node
|
||||||
* @return true if this node is the parent of <i>n</i>
|
* @return true if this node is the parent of <i>n</i>
|
||||||
|
@ -107,8 +110,9 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
name = name.substring(1);
|
name = name.substring(1);
|
||||||
}
|
}
|
||||||
int index=name.indexOf(PATH_SEPARATOR);
|
int index=name.indexOf(PATH_SEPARATOR);
|
||||||
if (index !=-1)
|
if (index != -1) {
|
||||||
name = name.substring(0, index);
|
name = name.substring(0, index);
|
||||||
|
}
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +172,8 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
* @see InnerNodeImpl(String, String, InnerNode, int)
|
* @see InnerNodeImpl(String, String, InnerNode, int)
|
||||||
*/
|
*/
|
||||||
private InnerNodeImpl createParentNode(String parentName) {
|
private InnerNodeImpl createParentNode(String parentName) {
|
||||||
return new InnerNodeImpl(parentName, getPath(this), this, this.getLevel()+1);
|
return new InnerNodeImpl(parentName,
|
||||||
|
getPath(this), this, this.getLevel() + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -220,14 +225,16 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node getLoc(String loc) {
|
public Node getLoc(String loc) {
|
||||||
if (loc == null || loc.length() == 0) return this;
|
if (loc == null || loc.length() == 0) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
String[] path = loc.split(PATH_SEPARATOR_STR, 2);
|
String[] path = loc.split(PATH_SEPARATOR_STR, 2);
|
||||||
Node childnode = childrenMap.get(path[0]);
|
Node childNode = childrenMap.get(path[0]);
|
||||||
if (childnode == null) return null; // non-existing node
|
if (childNode == null || path.length == 1) {
|
||||||
if (path.length == 1) return childnode;
|
return childNode;
|
||||||
if (childnode instanceof InnerNode) {
|
} else if (childNode instanceof InnerNode) {
|
||||||
return ((InnerNode)childnode).getLoc(path[1]);
|
return ((InnerNode)childNode).getLoc(path[1]);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -237,11 +244,10 @@ public class InnerNodeImpl extends NodeBase implements InnerNode {
|
||||||
public Node getLeaf(int leafIndex, Node excludedNode) {
|
public Node getLeaf(int leafIndex, Node excludedNode) {
|
||||||
int count=0;
|
int count=0;
|
||||||
// check if the excluded node a leaf
|
// check if the excluded node a leaf
|
||||||
boolean isLeaf =
|
boolean isLeaf = !(excludedNode instanceof InnerNode);
|
||||||
excludedNode == null || !(excludedNode instanceof InnerNode);
|
|
||||||
// calculate the total number of excluded leaf nodes
|
// calculate the total number of excluded leaf nodes
|
||||||
int numOfExcludedLeaves =
|
int numOfExcludedLeaves =
|
||||||
isLeaf ? 1 : ((InnerNode)excludedNode).getNumOfLeaves();
|
isLeaf ? 1 : ((InnerNode)excludedNode).getNumOfLeaves();
|
||||||
if (isLeafParent()) { // children are leaves
|
if (isLeafParent()) { // children are leaves
|
||||||
if (isLeaf) { // excluded node is a leaf node
|
if (isLeaf) { // excluded node is a leaf node
|
||||||
if (excludedNode != null &&
|
if (excludedNode != null &&
|
||||||
|
|
Loading…
Reference in New Issue