From e742bf9b8d1bc5ee7a97586510643db6fd3174f2 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 18 Nov 2019 14:53:44 +0100 Subject: Implement basic API (#15) --- .../java/pl/edu/mimuw/cloudatlas/model/ZMI.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java') 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 sons = new ArrayList(); @@ -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 PathName 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); -- cgit v1.2.3