m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java
diff options
context:
space:
mode:
authorMartin <marcin.j.chrzanowski@gmail.com>2019-11-18 14:53:44 +0100
committerGitHub <noreply@github.com>2019-11-18 14:53:44 +0100
commite742bf9b8d1bc5ee7a97586510643db6fd3174f2 (patch)
treef1bf701c819a842e1f82e328cf3118556411502f /src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java
parent015e46aa190a36c593eeff8b09cea43d9902de0d (diff)
Implement basic API (#15)
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java
index 5a560ae..a311c61 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java
@@ -41,6 +41,11 @@ import com.esotericsoftware.kryo.io.Output;
* references to its father and sons in the tree.
*/
public class ZMI implements Cloneable {
+ public class NoSuchZoneException extends Exception {
+ public NoSuchZoneException(PathName path) {
+ super("No such zone: " + path);
+ }
+ }
private final AttributesMap attributes = new AttributesMap();
private final List<ZMI> sons = new ArrayList<ZMI>();
@@ -85,6 +90,26 @@ public class ZMI implements Cloneable {
this.father = father;
}
+ public ZMI findDescendant(PathName path) throws NoSuchZoneException {
+ ZMI descendant = this;
+ for (String component : path.getComponents()) {
+ boolean foundNextSon = false;
+ for (ZMI son : descendant.getSons()) {
+ if (son.getAttributes().get("name").equals(new ValueString(component))) {
+ descendant = son;
+ foundNextSon = true;
+ break;
+ }
+ }
+
+ if (!foundNextSon) {
+ throw new NoSuchZoneException(path);
+ }
+ }
+
+ return descendant;
+ }
+
/**
* Gets the list of sons of this ZMI. Modifying a value in the returned list will cause an exception.
*
@@ -170,6 +195,16 @@ public class ZMI implements Cloneable {
return attributes.toString();
}
+ /**
+ * Gets the PathName representing this zone.
+ *
+ * @return a <code>PathName</code> object representing this zone
+ */
+ public PathName getPathName() {
+ String name = ((ValueString)getAttributes().get("name")).getValue();
+ return getFather() == null? PathName.ROOT : getFather().getPathName().levelDown(name);
+ }
+
public static ZMI deserialize(InputStream in) {
Kryo kryo = new Kryo();
Input kryoInput = new Input(in);