1 10 11 package com.triactive.jdo.sco; 12 13 import com.triactive.jdo.SCO; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Map.Entry; 19 20 21 29 30 class SCOHelper 31 { 32 private SCOHelper() {} 33 34 35 private static void assertIsValidElement(SCO o, boolean allowNulls, Class expectedType, Object element) 36 { 37 if (element == null && !allowNulls) 38 throw new NullsNotAllowedException(o); 39 40 Class 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 expectedType, Object key) 48 { 49 if (key == null) 50 throw new NullsNotAllowedException(o); 51 52 Class 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 expectedType, Object value) 60 { 61 if (value == null && !allowNulls) 62 throw new NullsNotAllowedException(o); 63 64 Class actualType = value.getClass(); 65 66 if (!expectedType.isAssignableFrom(actualType)) 67 throw new IncompatibleValueTypeException(o, expectedType, actualType); 68 } 69 70 71 78 79 public static void assertIsValidElement(SCOCollection c, Object element) 80 { 81 if (c.getOwner() != null) 82 assertIsValidElement(c, c.allowsNulls(), c.getElementType(), element); 83 } 84 85 86 92 93 public static void assertIsValidKey(SCOMap m, Object key) 94 { 95 if (m.getOwner() != null) 96 assertIsValidKey(m, m.getKeyType(), key); 97 } 98 99 100 106 107 public static void assertIsValidValue(SCOMap m, Object value) 108 { 109 if (m.getOwner() != null) 110 assertIsValidValue(m, m.allowsNullValues(), m.getValueType(), value); 111 } 112 113 114 120 121 public static boolean isValidElement(SCOCollection c, Object element) 122 { 123 if (c.getOwner() != null) 124 { 125 try { assertIsValidElement(c, c.allowsNulls(), c.getElementType(), element); } 126 catch (Exception e) { return false; } 127 } 128 129 return true; 130 } 131 132 133 139 140 public static boolean isValidKey(SCOMap m, Object key) 141 { 142 if (m.getOwner() != null) 143 { 144 try { assertIsValidKey(m, m.getKeyType(), key); } 145 catch (Exception e) { return false; } 146 } 147 148 return true; 149 } 150 151 152 158 159 public static boolean isValidValue(SCOMap m, Object value) 160 { 161 if (m.getOwner() != null) 162 { 163 try { assertIsValidValue(m, m.allowsNullValues(), m.getValueType(), value); } 164 catch (Exception e) { return false; } 165 } 166 167 return true; 168 } 169 170 171 178 179 public static void assertAllValidElements(SCOCollection c, Collection elements) 180 { 181 if (c.getOwner() != null) 182 { 183 boolean allowNulls = c.allowsNulls(); 184 Class expectedType = c.getElementType(); 185 186 ArrayList failures = new ArrayList (); 187 Iterator i = elements.iterator(); 188 189 while (i.hasNext()) 190 { 191 try 192 { 193 assertIsValidElement(c, allowNulls, expectedType, i.next()); 194 } 195 catch (Throwable t) 196 { 197 failures.add(t); 198 } 199 } 200 201 if (!failures.isEmpty()) 202 throw new IllegalArgumentsException(c, (Throwable [])failures.toArray(new Throwable [failures.size()])); 203 } 204 } 205 206 207 214 215 public static void assertAllValidEntries(SCOMap m, Map entries) 216 { 217 if (m.getOwner() != null) 218 { 219 boolean allowNullValues = m.allowsNullValues(); 220 Class keyType = m.getKeyType(); 221 Class valueType = m.getValueType(); 222 223 ArrayList failures = new ArrayList (); 224 Iterator 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 t) 235 { 236 failures.add(t); 237 } 238 } 239 240 if (!failures.isEmpty()) 241 throw new IllegalArgumentsException(m, (Throwable [])failures.toArray(new Throwable [failures.size()])); 242 } 243 } 244 245 246 252 253 public static String toLogString(SCO sco) 254 { 255 StringBuffer sb = new StringBuffer ("SCO "); 256 257 String scoClassName = sco.getClass().getName(); 258 sb.append(scoClassName.substring(scoClassName.lastIndexOf('.') + 1)) 259 .append('@').append(System.identityHashCode(sco)); 260 261 Object owner = sco.getOwner(); 262 263 if (owner != null) 264 { 265 String 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 |