Mercurial > hg > jslab
comparison src/samer/core_/Node.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * Node.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 * | |
11 * Change history: | |
12 * | |
13 * Removed all references to parent nodes - each node now | |
14 * stores its full name. Uses more memory but simpler in the | |
15 * long run, I think. | |
16 */ | |
17 | |
18 package samer.core; | |
19 import java.util.*; | |
20 | |
21 /** | |
22 <p> A Node is essentially a path in a hierarchical | |
23 name space. Each node is an independent object. | |
24 A Node can be constructed either with an explicit | |
25 parent Node, or with an absolute path, or as a child | |
26 of the current Node maintained by the current Shell. | |
27 Names are constructed in parts, concatenated with | |
28 dots, eg, | |
29 <code> | |
30 Node A=new Node("top",null); | |
31 Node B=new Node("middle",A); | |
32 Node C=new Node("bottom",B); | |
33 </code> | |
34 Then node A is "top", node B is "top.middle" and node | |
35 C is "top.middle.bottom" | |
36 | |
37 <p>The top level Environment (see Environment) | |
38 actually uses a nameless top level node: "", so that | |
39 all fully specified node paths begin with ".", eg | |
40 ".antelope.gibbon.firebucket" | |
41 This is so that names that <I>don't</I> start with a | |
42 dot are treated as relative to the current Environment's node. | |
43 */ | |
44 | |
45 public class Node implements java.io.Serializable | |
46 { | |
47 private final String name; | |
48 | |
49 /** Construct new Node with given name as a child of the | |
50 current Environment's base node, ie | |
51 <code>Shell.env().node()</code> | |
52 */ | |
53 public Node(String name) { | |
54 if (name==null || name.equals("")) | |
55 throw new Error("Node with empty name"); | |
56 | |
57 if (isAbsolute(name)) this.name=name; | |
58 else this.name=Shell.env().node().fullNameFor(name); | |
59 } | |
60 | |
61 | |
62 private Node() { name=""; } | |
63 | |
64 /** Construct new Node with given name and parent | |
65 If parent is null, a top level node is created | |
66 */ | |
67 public Node(String name, Node parent) { this.name = parent.fullName()+"."+name; } | |
68 | |
69 /** Returns parent Node, or null if no parent */ | |
70 public Node getParent() { return new Node(name.substring(0,name.lastIndexOf('.'))); } | |
71 public static String lastPart(String str) { return str.substring(1+str.lastIndexOf('.')); } | |
72 public String fullName() { return name; } | |
73 public String shortName() { return lastPart(name); } | |
74 | |
75 /** Return full name of hypothetical Node with given name | |
76 as a child of this Node, ie, this node's full name + "."+ nm. | |
77 */ | |
78 public String fullNameFor(String nm) { return name+"."+nm; } | |
79 | |
80 /** Absolute names start with a . This is only true of the top | |
81 level Node has an empty name! | |
82 */ | |
83 public static boolean isAbsolute(String nm) { return (nm.charAt(0)=='.'); } | |
84 | |
85 /** Returns true if and only if the given name is a child of this one. | |
86 This is trivially true if the given name is relative (ie doesn't start | |
87 with a dot). If it does, then the initial substrings must match. | |
88 */ | |
89 public boolean isSubnode(String nm) { | |
90 if (!isAbsolute(nm)) return true; | |
91 return nm.startsWith(name); | |
92 } | |
93 | |
94 /** Returns the full name of this node. */ | |
95 public String toString() { return name; } | |
96 | |
97 /** Returns a NEW top-level node each time it is called. */ | |
98 public static Node root() { return new Node(); } | |
99 } |