KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > extend > OutboundContext


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.extend;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.directwebremoting.util.LocalUtil;
22
23 /**
24  * We need to keep track of stuff while we are converting on the way out to
25  * prevent recurrsion.
26  * This class helps track the conversion process.
27  * @author Joe Walker [joe at getahead dot ltd dot uk]
28  */

29 public final class OutboundContext
30 {
31     /**
32      * JDK14: Can use IdentityHashMap directly
33      * Since map needs to have referencial equality rather than object equality
34      * this constructor tries to use java.util.IdentityHashMap (>=1.4), and
35      * failing that falls back on wrapper objects in a HashMap.
36      */

37     public OutboundContext()
38     {
39         // We can only assign to map once, so we use this as a staging post.
40
Map JavaDoc assign;
41
42         try
43         {
44             assign = (Map JavaDoc) LocalUtil.classForName("java.util.IdentityHashMap").newInstance();
45             referenceWrappers = false;
46         }
47         catch (Exception JavaDoc ex)
48         {
49             assign = new HashMap JavaDoc();
50             referenceWrappers = true;
51         }
52
53         map = assign;
54     }
55
56     /**
57      * Have we already converted an object?
58      * @param object The object to check
59      * @return How it was converted last time or null if we've not seen it before
60      */

61     public OutboundVariable get(Object JavaDoc object)
62     {
63         Object JavaDoc key = object;
64         if (referenceWrappers)
65         {
66             key = new ReferenceWrapper(object);
67         }
68
69         return (OutboundVariable) map.get(key);
70     }
71
72     /**
73      * @param object We have converted a new object, remember it
74      * @param ss How the object was converted
75      */

76     public void put(Object JavaDoc object, OutboundVariable ss)
77     {
78         Object JavaDoc key = object;
79         if (referenceWrappers)
80         {
81             key = new ReferenceWrapper(object);
82         }
83
84         map.put(key, ss);
85     }
86
87     /**
88      * Create a new variable name to keep everything we declare separate
89      * @return A new unique variable name
90      */

91     public String JavaDoc getNextVariableName()
92     {
93         String JavaDoc varName = OUTBOUND_VARIABLE_PREFIX + nextVarIndex;
94         nextVarIndex++;
95
96         return varName;
97     }
98
99     /* (non-Javadoc)
100      * @see java.lang.Object#toString()
101      */

102     public String JavaDoc toString()
103     {
104         return map.toString();
105     }
106
107     /**
108      * The prefix for outbound variable names the we generate
109      */

110     private static final String JavaDoc OUTBOUND_VARIABLE_PREFIX = "s";
111
112     /**
113      * The map of objects to how we converted them last time
114      */

115     private final Map JavaDoc map;
116
117     /**
118      * Tells if we are to wrap objects in the map to get referencial equality.
119      */

120     private boolean referenceWrappers;
121
122     /**
123      * What index do we tack on the next variable name that we generate
124      */

125     private int nextVarIndex = 0;
126
127     /**
128      * Wrapper class that makes sure that equals() and hashCode() uses
129      * referencial equality on the wrapped object. This is used when
130      * we can't have a IdentityHashMap in map.
131      */

132     private static class ReferenceWrapper
133     {
134         /**
135          * @param object The object to wrap
136          */

137         protected ReferenceWrapper(Object JavaDoc object)
138         {
139             this.object = object;
140         }
141
142         /* (non-Javadoc)
143          * @see java.lang.Object#hashCode()
144          */

145         public int hashCode()
146         {
147             return System.identityHashCode(object);
148         }
149
150         /* (non-Javadoc)
151          * @see java.lang.Object#equals(java.lang.Object)
152          */

153         public boolean equals(Object JavaDoc obj)
154         {
155             if (!(obj instanceof ReferenceWrapper))
156             {
157                 return false;
158             }
159
160             ReferenceWrapper that = (ReferenceWrapper) obj;
161             return this.object == that.object;
162         }
163
164         /**
165          * My wrapped object.
166          */

167         private Object JavaDoc object;
168     }
169 }
170
Popular Tags