add size()
This commit is contained in:
@ -14,6 +14,10 @@ public class AST {
|
|||||||
return this.root;
|
return this.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long size() {
|
||||||
|
return this.root.size();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof AST) {
|
if (obj instanceof AST) {
|
||||||
|
|||||||
@ -7,9 +7,9 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class Node {
|
public class Node {
|
||||||
|
|
||||||
private final String name;
|
private String name;
|
||||||
private String value;
|
private String value;
|
||||||
private final List<Node> children = new ArrayList<>();
|
private List<Node> children = new ArrayList<>();
|
||||||
|
|
||||||
public Node(String name) {
|
public Node(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -32,23 +32,17 @@ public class Node {
|
|||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public void setValue(String value) {
|
public void setValue(String value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public int hashCode() {
|
||||||
if (obj instanceof Node) {
|
return Objects.hash(this.name, this.value, this.children);
|
||||||
for (int i = 0; i < this.children.size(); i++) {
|
|
||||||
if (!this.children.get(i).equals(((Node) obj).children.get(i))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// toString() und print() von hier: https://stackoverflow.com/a/8948691
|
// toString() und print() von hier: https://stackoverflow.com/a/8948691
|
||||||
@ -79,11 +73,31 @@ public class Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public boolean equals(Object obj) {
|
||||||
return Objects.hash(this.name, this.value, this.children); // TODO: children?
|
if (obj instanceof Node) {
|
||||||
|
return this.name.equals(((Node) obj).name)
|
||||||
|
&& this.value.equals(((Node) obj).value)
|
||||||
|
&& this.children.equals(((Node) obj).children);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setChildren(List<Node> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long size() {
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
for (Node child : this.children) {
|
||||||
|
s += child.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
return s + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user