001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: SCOHelper.java,v 1.1 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.sco;
012:
013: import com.triactive.jdo.SCO;
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.util.Map.Entry;
019:
020: /**
021: * Contains static methods to help mutable SCO classes.
022: * Because SCO classes must inherit from specific base classes they cannot
023: * share this code via inheritance.
024: *
025: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
026: * @version $Revision: 1.1 $
027: */
028:
029: class SCOHelper {
030: private SCOHelper() {
031: }
032:
033: private static void assertIsValidElement(SCO o, boolean allowNulls,
034: Class expectedType, Object element) {
035: if (element == null && !allowNulls)
036: throw new NullsNotAllowedException(o);
037:
038: Class actualType = element.getClass();
039:
040: if (!expectedType.isAssignableFrom(actualType))
041: throw new IncompatibleElementTypeException(o, expectedType,
042: actualType);
043: }
044:
045: private static void assertIsValidKey(SCO o, Class expectedType,
046: Object key) {
047: if (key == null)
048: throw new NullsNotAllowedException(o);
049:
050: Class actualType = key.getClass();
051:
052: if (!expectedType.isAssignableFrom(actualType))
053: throw new IncompatibleKeyTypeException(o, expectedType,
054: actualType);
055: }
056:
057: private static void assertIsValidValue(SCO o, boolean allowNulls,
058: Class expectedType, Object value) {
059: if (value == null && !allowNulls)
060: throw new NullsNotAllowedException(o);
061:
062: Class actualType = value.getClass();
063:
064: if (!expectedType.isAssignableFrom(actualType))
065: throw new IncompatibleValueTypeException(o, expectedType,
066: actualType);
067: }
068:
069: /**
070: * Asserts that an element is valid for storage in the specified SCO
071: * collection.
072: *
073: * @exception SCOException
074: * If the element is not valid.
075: */
076:
077: public static void assertIsValidElement(SCOCollection c,
078: Object element) {
079: if (c.getOwner() != null)
080: assertIsValidElement(c, c.allowsNulls(),
081: c.getElementType(), element);
082: }
083:
084: /**
085: * Asserts that a key is valid for storage in the specified SCO map.
086: *
087: * @exception SCOException
088: * If the key is not valid.
089: */
090:
091: public static void assertIsValidKey(SCOMap m, Object key) {
092: if (m.getOwner() != null)
093: assertIsValidKey(m, m.getKeyType(), key);
094: }
095:
096: /**
097: * Asserts that a value is valid for storage in the specified SCO map.
098: *
099: * @exception SCOException
100: * If the value is not valid.
101: */
102:
103: public static void assertIsValidValue(SCOMap m, Object value) {
104: if (m.getOwner() != null)
105: assertIsValidValue(m, m.allowsNullValues(), m
106: .getValueType(), value);
107: }
108:
109: /**
110: * Tests if an element is valid for storage in the specified SCO collection.
111: *
112: * @return <code>true</code> if the element valid,
113: * <code>false</code> otherwise.
114: */
115:
116: public static boolean isValidElement(SCOCollection c, Object element) {
117: if (c.getOwner() != null) {
118: try {
119: assertIsValidElement(c, c.allowsNulls(), c
120: .getElementType(), element);
121: } catch (Exception e) {
122: return false;
123: }
124: }
125:
126: return true;
127: }
128:
129: /**
130: * Tests if a key is valid for storage in the specified SCO map.
131: *
132: * @return <code>true</code> if the key valid,
133: * <code>false</code> otherwise.
134: */
135:
136: public static boolean isValidKey(SCOMap m, Object key) {
137: if (m.getOwner() != null) {
138: try {
139: assertIsValidKey(m, m.getKeyType(), key);
140: } catch (Exception e) {
141: return false;
142: }
143: }
144:
145: return true;
146: }
147:
148: /**
149: * Tests if a value is valid for storage in the specified SCO map.
150: *
151: * @return <code>true</code> if the value valid,
152: * <code>false</code> otherwise.
153: */
154:
155: public static boolean isValidValue(SCOMap m, Object value) {
156: if (m.getOwner() != null) {
157: try {
158: assertIsValidValue(m, m.allowsNullValues(), m
159: .getValueType(), value);
160: } catch (Exception e) {
161: return false;
162: }
163: }
164:
165: return true;
166: }
167:
168: /**
169: * Asserts that a group of elements are all valid for storage in the
170: * specified SCO collection.
171: *
172: * @exception IllegalArgumentsException
173: * If one or more of the elements are not valid.
174: */
175:
176: public static void assertAllValidElements(SCOCollection c,
177: Collection elements) {
178: if (c.getOwner() != null) {
179: boolean allowNulls = c.allowsNulls();
180: Class expectedType = c.getElementType();
181:
182: ArrayList failures = new ArrayList();
183: Iterator i = elements.iterator();
184:
185: while (i.hasNext()) {
186: try {
187: assertIsValidElement(c, allowNulls, expectedType, i
188: .next());
189: } catch (Throwable t) {
190: failures.add(t);
191: }
192: }
193:
194: if (!failures.isEmpty())
195: throw new IllegalArgumentsException(
196: c,
197: (Throwable[]) failures
198: .toArray(new Throwable[failures.size()]));
199: }
200: }
201:
202: /**
203: * Asserts that all the entries of a map are valid for storage in the
204: * specified SCO map.
205: *
206: * @exception IllegalArgumentsException
207: * If one or more of the entries are not valid.
208: */
209:
210: public static void assertAllValidEntries(SCOMap m, Map entries) {
211: if (m.getOwner() != null) {
212: boolean allowNullValues = m.allowsNullValues();
213: Class keyType = m.getKeyType();
214: Class valueType = m.getValueType();
215:
216: ArrayList failures = new ArrayList();
217: Iterator i = entries.entrySet().iterator();
218:
219: while (i.hasNext()) {
220: try {
221: Entry e = (Entry) i.next();
222: assertIsValidKey(m, keyType, e.getKey());
223: assertIsValidValue(m, allowNullValues, valueType, e
224: .getValue());
225: } catch (Throwable t) {
226: failures.add(t);
227: }
228: }
229:
230: if (!failures.isEmpty())
231: throw new IllegalArgumentsException(
232: m,
233: (Throwable[]) failures
234: .toArray(new Throwable[failures.size()]));
235: }
236: }
237:
238: /**
239: * Returns a string representation of an SCO object suitable for use in an
240: * exception or a log message.
241: *
242: * @return A string representation of the SCO for logging purposes.
243: */
244:
245: public static String toLogString(SCO sco) {
246: StringBuffer sb = new StringBuffer("SCO ");
247:
248: String scoClassName = sco.getClass().getName();
249: sb.append(
250: scoClassName
251: .substring(scoClassName.lastIndexOf('.') + 1))
252: .append('@').append(System.identityHashCode(sco));
253:
254: Object owner = sco.getOwner();
255:
256: if (owner != null) {
257: String ownerClassName = owner.getClass().getName();
258:
259: sb.append(" for ").append(
260: ownerClassName.substring(ownerClassName
261: .lastIndexOf('.') + 1)).append('@').append(
262: System.identityHashCode(owner)).append('.').append(
263: sco.getFieldName());
264: }
265:
266: return sb.toString();
267: }
268: }
|