1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.util.TreeMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.Set ; 32 33 34 35 41 42 class Frame { 43 static Frame newFrame(){ 47 return new Frame( 48 new Frame(){ 49 String lookup(String s){ 50 return (System.getProperty(s) != null 51 ? System.getProperty(s) 52 : "${"+s+"}"); 53 } 54 public String toString(){ 55 return "[]"; 56 } 57 } 58 ); 59 } 60 static Frame newFrame(Frame f){ 61 return new Frame(f); 62 } 63 String lookup(String s){ 64 return (map.get(s) != null ? (String ) map.get(s) : parent.lookup(s)); 65 } 66 67 Frame put(String key, String value){ 68 map.put(key, value); 69 return this; 70 } 71 72 Frame inheritFrom(Frame f) throws IllegalArgumentException { 73 if (contains(getAncestors(),f) || contains(f.getAncestors(),this)){ 74 throw new IllegalArgumentException ("Inheriting from an ancestor is illegal - it causes loops!"); 75 } 76 parent = f; 77 return this; 78 } 79 80 private boolean contains(Set s, Frame f){ 81 for (Iterator it = s.iterator(); it.hasNext();){ 82 if (it.next() == f) { 83 return true; 84 } 85 } 86 return false; 87 } 88 89 public String toString(){ 90 StringBuffer sb = new StringBuffer ("["); 91 for (Iterator it = map.entrySet().iterator(); it.hasNext();){ 92 Map.Entry e = (Map.Entry ) it.next(); 93 sb.append(e.getKey() + "->" + e.getValue()); 94 if (it.hasNext()){ 95 sb.append(", "); 96 } 97 } 98 sb.append(" "+parent +"]"); 99 return sb.toString(); 100 } 101 102 103 public boolean equals(Object o){ 104 return this == o || (o != null && o instanceof Frame && this.equals((Frame) o)); 105 } 106 114 public int hashCode(){ 115 return map.hashCode(); 116 } 117 118 private boolean equals(Frame o){ 119 return o != null 120 && (this.map.equals(o.map) 121 && ((this.parent == null && o.parent == null) || 122 (this.parent != null && this.parent.equals(o.parent)))); 123 } 124 125 126 127 private Frame(){} 128 private Frame(Frame f){ 129 parent = f; 130 } 131 132 private Map map = new TreeMap (); 133 private Frame parent; 134 135 private Set getAncestors(){ 136 if (parent == null){ 137 return new HashSet (); 138 } else { 139 Set ancestors = parent.getAncestors(); 140 ancestors.add(this); 141 return ancestors; 142 } 143 144 } 145 146 } 147 148 | Popular Tags |