KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > Frame


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.util.TreeMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Set JavaDoc;
32
33
34
35 /**
36  * Frame.java
37  *
38  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
39  * @version $Revision: 1.3 $
40  */

41
42 class Frame {
43       // The top frames have a special parent - this one responds to
44
// "lookup(s)" by merely looking in the environment and/or
45
// returning the key as a variable.
46
static Frame newFrame(){
47         return new Frame(
48                 new Frame(){
49                     String JavaDoc lookup(String JavaDoc s){
50                     return (System.getProperty(s) != null
51                             ? System.getProperty(s)
52                             : "${"+s+"}");
53                     }
54                     public String JavaDoc toString(){
55                         return "[]";
56                     }
57                 }
58                 );
59     }
60     static Frame newFrame(Frame f){
61         return new Frame(f);
62     }
63     String JavaDoc lookup(String JavaDoc s){
64         return (map.get(s) != null ? (String JavaDoc) map.get(s) : parent.lookup(s));
65     }
66     
67     Frame put(String JavaDoc key, String JavaDoc value){
68         map.put(key, value);
69         return this;
70     }
71
72     Frame inheritFrom(Frame f) throws IllegalArgumentException JavaDoc {
73         if (contains(getAncestors(),f) || contains(f.getAncestors(),this)){
74             throw new IllegalArgumentException JavaDoc("Inheriting from an ancestor is illegal - it causes loops!");
75         }
76         parent = f;
77         return this;
78     }
79
80     private boolean contains(Set JavaDoc s, Frame f){
81         for (Iterator JavaDoc it = s.iterator(); it.hasNext();){
82             if (it.next() == f) {
83                 return true;
84             }
85         }
86         return false;
87     }
88
89     public String JavaDoc toString(){
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[");
91         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext();){
92             Map.Entry JavaDoc e = (Map.Entry JavaDoc) 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 JavaDoc o){
104         return this == o || (o != null && o instanceof Frame && this.equals((Frame) o));
105     }
106       /*
107        * The contract for equals and hashcode is:
108        * FORALL a, b . a.equals(b) => a.hashCode() == b.hashCode()
109        *
110        * This will be true in this case because only when two frames
111        * have equal maps (amongst other things) can they be
112        * equal(), therefore the map hashCodes will be ==.
113       */

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 JavaDoc map = new TreeMap JavaDoc();
133     private Frame parent;
134     
135     private Set JavaDoc getAncestors(){
136         if (parent == null){
137             return new HashSet JavaDoc();
138         } else {
139             Set JavaDoc ancestors = parent.getAncestors();
140             ancestors.add(this);
141             return ancestors;
142         }
143             
144     }
145     
146 }
147
148
Popular Tags