KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > sco > SCOHelper


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SCOHelper.java,v 1.1 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.sco;
12
13 import com.triactive.jdo.SCO;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Map.Entry;
19
20
21 /**
22  * Contains static methods to help mutable SCO classes.
23  * Because SCO classes must inherit from specific base classes they cannot
24  * share this code via inheritance.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.1 $
28  */

29
30 class SCOHelper
31 {
32     private SCOHelper() {}
33
34
35     private static void assertIsValidElement(SCO o, boolean allowNulls, Class JavaDoc expectedType, Object JavaDoc element)
36     {
37         if (element == null && !allowNulls)
38             throw new NullsNotAllowedException(o);
39
40         Class JavaDoc actualType = element.getClass();
41
42         if (!expectedType.isAssignableFrom(actualType))
43             throw new IncompatibleElementTypeException(o, expectedType, actualType);
44     }
45
46
47     private static void assertIsValidKey(SCO o, Class JavaDoc expectedType, Object JavaDoc key)
48     {
49         if (key == null)
50             throw new NullsNotAllowedException(o);
51
52         Class JavaDoc actualType = key.getClass();
53
54         if (!expectedType.isAssignableFrom(actualType))
55             throw new IncompatibleKeyTypeException(o, expectedType, actualType);
56     }
57
58
59     private static void assertIsValidValue(SCO o, boolean allowNulls, Class JavaDoc expectedType, Object JavaDoc value)
60     {
61         if (value == null && !allowNulls)
62             throw new NullsNotAllowedException(o);
63
64         Class JavaDoc actualType = value.getClass();
65
66         if (!expectedType.isAssignableFrom(actualType))
67             throw new IncompatibleValueTypeException(o, expectedType, actualType);
68     }
69
70
71     /**
72      * Asserts that an element is valid for storage in the specified SCO
73      * collection.
74      *
75      * @exception SCOException
76      * If the element is not valid.
77      */

78
79     public static void assertIsValidElement(SCOCollection c, Object JavaDoc element)
80     {
81         if (c.getOwner() != null)
82             assertIsValidElement(c, c.allowsNulls(), c.getElementType(), element);
83     }
84
85
86     /**
87      * Asserts that a key is valid for storage in the specified SCO map.
88      *
89      * @exception SCOException
90      * If the key is not valid.
91      */

92
93     public static void assertIsValidKey(SCOMap m, Object JavaDoc key)
94     {
95         if (m.getOwner() != null)
96             assertIsValidKey(m, m.getKeyType(), key);
97     }
98
99
100     /**
101      * Asserts that a value is valid for storage in the specified SCO map.
102      *
103      * @exception SCOException
104      * If the value is not valid.
105      */

106
107     public static void assertIsValidValue(SCOMap m, Object JavaDoc value)
108     {
109         if (m.getOwner() != null)
110             assertIsValidValue(m, m.allowsNullValues(), m.getValueType(), value);
111     }
112
113
114     /**
115      * Tests if an element is valid for storage in the specified SCO collection.
116      *
117      * @return <code>true</code> if the element valid,
118      * <code>false</code> otherwise.
119      */

120
121     public static boolean isValidElement(SCOCollection c, Object JavaDoc element)
122     {
123         if (c.getOwner() != null)
124         {
125             try { assertIsValidElement(c, c.allowsNulls(), c.getElementType(), element); }
126             catch (Exception JavaDoc e) { return false; }
127         }
128
129         return true;
130     }
131
132
133     /**
134      * Tests if a key is valid for storage in the specified SCO map.
135      *
136      * @return <code>true</code> if the key valid,
137      * <code>false</code> otherwise.
138      */

139
140     public static boolean isValidKey(SCOMap m, Object JavaDoc key)
141     {
142         if (m.getOwner() != null)
143         {
144             try { assertIsValidKey(m, m.getKeyType(), key); }
145             catch (Exception JavaDoc e) { return false; }
146         }
147
148         return true;
149     }
150
151
152     /**
153      * Tests if a value is valid for storage in the specified SCO map.
154      *
155      * @return <code>true</code> if the value valid,
156      * <code>false</code> otherwise.
157      */

158
159     public static boolean isValidValue(SCOMap m, Object JavaDoc value)
160     {
161         if (m.getOwner() != null)
162         {
163             try { assertIsValidValue(m, m.allowsNullValues(), m.getValueType(), value); }
164             catch (Exception JavaDoc e) { return false; }
165         }
166
167         return true;
168     }
169
170
171     /**
172      * Asserts that a group of elements are all valid for storage in the
173      * specified SCO collection.
174      *
175      * @exception IllegalArgumentsException
176      * If one or more of the elements are not valid.
177      */

178
179     public static void assertAllValidElements(SCOCollection c, Collection JavaDoc elements)
180     {
181         if (c.getOwner() != null)
182         {
183             boolean allowNulls = c.allowsNulls();
184             Class JavaDoc expectedType = c.getElementType();
185
186             ArrayList JavaDoc failures = new ArrayList JavaDoc();
187             Iterator JavaDoc i = elements.iterator();
188
189             while (i.hasNext())
190             {
191                 try
192                 {
193                     assertIsValidElement(c, allowNulls, expectedType, i.next());
194                 }
195                 catch (Throwable JavaDoc t)
196                 {
197                     failures.add(t);
198                 }
199             }
200
201             if (!failures.isEmpty())
202                 throw new IllegalArgumentsException(c, (Throwable JavaDoc[])failures.toArray(new Throwable JavaDoc[failures.size()]));
203         }
204     }
205
206
207     /**
208      * Asserts that all the entries of a map are valid for storage in the
209      * specified SCO map.
210      *
211      * @exception IllegalArgumentsException
212      * If one or more of the entries are not valid.
213      */

214
215     public static void assertAllValidEntries(SCOMap m, Map JavaDoc entries)
216     {
217         if (m.getOwner() != null)
218         {
219             boolean allowNullValues = m.allowsNullValues();
220             Class JavaDoc keyType = m.getKeyType();
221             Class JavaDoc valueType = m.getValueType();
222
223             ArrayList JavaDoc failures = new ArrayList JavaDoc();
224             Iterator JavaDoc i = entries.entrySet().iterator();
225
226             while (i.hasNext())
227             {
228                 try
229                 {
230                     Entry e = (Entry)i.next();
231                     assertIsValidKey(m, keyType, e.getKey());
232                     assertIsValidValue(m, allowNullValues, valueType, e.getValue());
233                 }
234                 catch (Throwable JavaDoc t)
235                 {
236                     failures.add(t);
237                 }
238             }
239
240             if (!failures.isEmpty())
241                 throw new IllegalArgumentsException(m, (Throwable JavaDoc[])failures.toArray(new Throwable JavaDoc[failures.size()]));
242         }
243     }
244
245
246     /**
247      * Returns a string representation of an SCO object suitable for use in an
248      * exception or a log message.
249      *
250      * @return A string representation of the SCO for logging purposes.
251      */

252
253     public static String JavaDoc toLogString(SCO sco)
254     {
255         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SCO ");
256
257         String JavaDoc scoClassName = sco.getClass().getName();
258         sb.append(scoClassName.substring(scoClassName.lastIndexOf('.') + 1))
259           .append('@').append(System.identityHashCode(sco));
260
261         Object JavaDoc owner = sco.getOwner();
262
263         if (owner != null)
264         {
265             String JavaDoc ownerClassName = owner.getClass().getName();
266
267             sb.append(" for ")
268               .append(ownerClassName.substring(ownerClassName.lastIndexOf('.') + 1))
269               .append('@').append(System.identityHashCode(owner))
270               .append('.').append(sco.getFieldName());
271         }
272
273         return sb.toString();
274     }
275 }
276
Popular Tags