001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common;
009:
010: //base classes
011: import java.math.BigDecimal;
012: import java.sql.PreparedStatement;
013: import java.sql.SQLException;
014: import java.sql.Timestamp;
015: import java.sql.Types;
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.HashMap;
019: import java.util.Iterator;
020:
021: //project specific classes
022: import org.jfolder.common.UnexpectedSystemException;
023:
024: //other classes
025:
026: public class StandardDataTypes {
027:
028: //
029: private final static String DISPLAY_NAME__ANYTHING = "Anything";
030: private final static String DISPLAY_NAME__NOTHING = "Nothing";
031: //
032: private final static String DISPLAY_NAME__BOOLEAN = "Boolean";
033: private final static String DISPLAY_NAME__STRING = "String";
034: private final static String DISPLAY_NAME__DECIMAL = "Decimal";
035: //
036: private final static String DISPLAY_NAME__TIMESTAMP = "Timestamp";
037: private final static String DISPLAY_NAME__BINARY = "Binary";
038: //
039: private final static String DISPLAY_NAME__LIST = "List";
040: private final static String DISPLAY_NAME__MAP = "Map";
041:
042: //
043: private StandardDataTypes() {
044: }
045:
046: //
047: public final static Class getAnyClass() {
048: return Object.class;
049: }
050:
051: public final static Class getNothingClass() {
052: return Void.TYPE;
053: }
054:
055: //
056: public final static Class getBooleanClass() {
057: return Boolean.class;
058: }
059:
060: public final static Class getStringClass() {
061: return String.class;
062: }
063:
064: public final static Class getDecimalClass() {
065: return BigDecimal.class;
066: }
067:
068: //
069: public final static Class getBinaryClass() {
070: return ImmutableByteArray.class;
071: }
072:
073: public final static Class getTimestampClass() {
074: return ImmutableTimestamp.class;
075: }
076:
077: public final static Class getHtmlClass() {
078: return ImmutablePreFormattedHtml.class;
079: }
080:
081: //
082: public final static Class getListClass() {
083: return ArrayList.class;
084: }
085:
086: public final static Class getMapClass() {
087: return HashMap.class;
088: }
089:
090: //
091: //
092: public final static boolean isBooleanClass(Class inClass) {
093: return getBooleanClass().equals(inClass);
094: }
095:
096: public final static boolean isDecimalClass(Class inClass) {
097: return getDecimalClass().equals(inClass);
098: }
099:
100: public final static boolean isStringClass(Class inClass) {
101: return getStringClass().equals(inClass);
102: }
103:
104: //
105: public final static boolean isBinaryClass(Class inClass) {
106: return getBinaryClass().equals(inClass);
107: }
108:
109: public final static boolean isTimestampClass(Class inClass) {
110: return getTimestampClass().equals(inClass);
111: }
112:
113: //
114: public final static boolean isListClass(Class inClass) {
115: return getListClass().equals(inClass);
116: }
117:
118: public final static boolean isMapClass(Class inClass) {
119: return getMapClass().equals(inClass);
120: }
121:
122: //
123: public final static HashMap getClassToDisplayName() {
124:
125: HashMap outValue = new HashMap();
126:
127: //
128: outValue.put(getAnyClass(), DISPLAY_NAME__ANYTHING);
129: outValue.put(getNothingClass(), DISPLAY_NAME__NOTHING);
130: //
131: outValue.put(getBooleanClass(), DISPLAY_NAME__BOOLEAN);
132: outValue.put(getStringClass(), DISPLAY_NAME__STRING);
133: outValue.put(getDecimalClass(), DISPLAY_NAME__DECIMAL);
134: //
135: outValue.put(getBinaryClass(), DISPLAY_NAME__BINARY);
136: outValue.put(getTimestampClass(), DISPLAY_NAME__TIMESTAMP);
137: //
138: outValue.put(getListClass(), DISPLAY_NAME__LIST);
139: outValue.put(getMapClass(), DISPLAY_NAME__MAP);
140:
141: return outValue;
142: }
143:
144: public final static ArrayList getImmutableVariableTypes() {
145:
146: ArrayList outValue = new ArrayList();
147:
148: //
149: outValue.add(DISPLAY_NAME__STRING);
150: outValue.add(DISPLAY_NAME__DECIMAL);
151: outValue.add(DISPLAY_NAME__BOOLEAN);
152: //
153: outValue.add(DISPLAY_NAME__BINARY);
154: outValue.add(DISPLAY_NAME__TIMESTAMP);
155:
156: return outValue;
157: }
158:
159: public final static ArrayList getAllConcreteVariableTypes() {
160:
161: ArrayList outValue = new ArrayList();
162:
163: //
164: outValue.addAll(getImmutableVariableTypes());
165: //
166: outValue.add(DISPLAY_NAME__LIST);
167: outValue.add(DISPLAY_NAME__MAP);
168:
169: return outValue;
170: }
171:
172: public final static HashMap getDisplayNameToClass() {
173:
174: HashMap outValue = new HashMap();
175:
176: HashMap ctnm = getClassToDisplayName();
177: Iterator ctnmIter = ctnm.keySet().iterator();
178: while (ctnmIter.hasNext()) {
179: Class nextCtnmClass = ((Class) ctnmIter.next());
180: String nextCtnmDisplayName = ((String) ctnm
181: .get(nextCtnmClass));
182: outValue.put(nextCtnmDisplayName, nextCtnmClass);
183: }
184:
185: return outValue;
186: }
187:
188: //
189: public final static String getDisplayName(Class inClass) {
190:
191: String outValue = null;
192:
193: HashMap ctnm = getClassToDisplayName();
194:
195: if (ctnm.containsKey(inClass)) {
196: outValue = ((String) ctnm.get(inClass));
197: }
198: //if (inClass.equals(getAnyClass())) {
199: // outValue = "Anything";
200: //}
201: //else if (inClass.equals(getNothingClass())) {
202: // outValue = "Nothing";
203: //}
204: //else if (inClass.equals(getBooleanClass())) {
205: // outValue = "Boolean";
206: //}
207: //else if (inClass.equals(getStringClass())) {
208: // outValue = "String";
209: //}
210: //else if (inClass.equals(getDecimalClass())) {
211: // outValue = "Decimal";
212: //}
213: //else if (inClass.equals(getBinaryClass())) {
214: // outValue = "Binary";
215: //}
216: //else if (inClass.equals(getDateClass())) {
217: // outValue = "Date";
218: //}
219: else {
220: outValue = inClass.getName();
221: }
222:
223: return outValue;
224: }
225:
226: //
227: public final static Class getActualClass(String inDisplayName) {
228:
229: Class outValue = null;
230:
231: HashMap dntc = getDisplayNameToClass();
232: if (dntc.containsKey(inDisplayName)) {
233: outValue = ((Class) dntc.get(inDisplayName));
234: }
235:
236: return outValue;
237: }
238:
239: ////
240: public final static Object getNothingInstance() {
241: return null;
242: }
243:
244: //
245: public final static Object getBinaryInstance(byte inValue[]) {
246: return ImmutableByteArray.newInstance(inValue);
247: }
248:
249: public final static Object getTimestampInstance(
250: Timestamp inTimestamp) {
251: return ImmutableTimestamp.newInstance(inTimestamp);
252: }
253:
254: public final static Object getHtmlInstance(String inValue) {
255: return ImmutablePreFormattedHtml.newInstance(inValue);
256: }
257:
258: public final static Object getBooleanInstance(Boolean inValue) {
259: return inValue;
260: }
261:
262: public final static Object getBooleanInstance(String inValue) {
263: return (new Boolean(inValue));
264: }
265:
266: //
267: public final static Object getStringInstance(String inValue) {
268: return inValue;
269: }
270:
271: //
272: public final static Object getDecimalInstance(BigDecimal inValue) {
273: return inValue;
274: }
275:
276: public final static Object getDecimalInstance(String inValue) {
277: return (new BigDecimal(inValue));
278: }
279:
280: //
281: public final static Object getEmptyListInstance() {
282: return (new ArrayList());
283: }
284:
285: public final static Object getEmptyMapInstance() {
286: return (new HashMap());
287: }
288:
289: //
290: public final static Object getListInstance(Collection inColl) {
291: return (new ArrayList(inColl));
292: }
293:
294: //
295: ////
296: //
297: public final static String getSubString(Object inString,
298: Object inStartIndex, Object inEndIndex) {
299: //
300: String outValue = null;
301:
302: //
303: outValue = inString.toString();
304: //
305: BigDecimal startBd = ((BigDecimal) inStartIndex);
306: BigDecimal endBd = ((BigDecimal) inEndIndex);
307: //
308: outValue = outValue.substring(startBd.intValue(), endBd
309: .intValue());
310:
311: return outValue;
312: }
313:
314: //
315: public final static ImmutableByteArray getBinaryAsBinary(
316: Object inObj) {
317:
318: ImmutableByteArray outValue = null;
319:
320: outValue = ((ImmutableByteArray) inObj);
321:
322: return outValue;
323: }
324:
325: //
326: public final static String getStringAsString(Object inObj) {
327:
328: String outValue = null;
329:
330: outValue = ((String) inObj);
331:
332: return outValue;
333: }
334:
335: public final static ArrayList getListAsList(Object inObj) {
336:
337: ArrayList outValue = null;
338:
339: outValue = ((ArrayList) inObj);
340:
341: return outValue;
342: }
343:
344: public final static Object getStringLength(Object inObj) {
345:
346: Object outValue = null;
347:
348: String s = ((String) inObj);
349: outValue = StandardDataTypes.getDecimalInstance(new BigDecimal(
350: s.length()));
351:
352: return outValue;
353: }
354:
355: public final static Object getListLength(Object inObj) {
356:
357: Object outValue = null;
358:
359: ArrayList al = ((ArrayList) inObj);
360: outValue = StandardDataTypes.getDecimalInstance(new BigDecimal(
361: al.size()));
362:
363: return outValue;
364: }
365:
366: public final static Object getListValue(Object inObj, Object inIndex) {
367:
368: Object outValue = null;
369:
370: //
371: ArrayList al = ((ArrayList) inObj);
372: //
373: BigDecimal bd = ((BigDecimal) inIndex);
374: //
375: outValue = al.get(bd.intValue());
376:
377: return outValue;
378: }
379:
380: public final static void addListValue(Object inObj, Object inValue) {
381: //
382: addListValue(inObj, null, inValue);
383: }
384:
385: public final static void addListValue(Object inObj, Object inIndex,
386: Object inValue) {
387:
388: //
389: ArrayList al = ((ArrayList) inObj);
390: //
391: if (inIndex != null) {
392: //
393: BigDecimal bd = ((BigDecimal) inIndex);
394: //
395: al.add(bd.intValue(), inValue);
396: } else {
397: al.add(inValue);
398: }
399: }
400:
401: public final static void addListToList(Object inObj,
402: Object inIndex, Object inValue) {
403:
404: //
405: ArrayList parentList = ((ArrayList) inObj);
406: //
407: BigDecimal bd = ((BigDecimal) inIndex);
408: //
409: ArrayList subList = ((ArrayList) inValue);
410: //
411: parentList.addAll(bd.intValue(), subList);
412: }
413:
414: public final static void removeListValue(Object inObj,
415: Object inIndex) {
416:
417: //
418: ArrayList al = ((ArrayList) inObj);
419: //
420: BigDecimal bd = ((BigDecimal) inIndex);
421: //
422: al.remove(bd.intValue());
423: }
424:
425: public final static Object getSubList(Object inObj,
426: Object inStartIndex, Object inEndIndex) {
427:
428: Object outValue = null;
429:
430: //
431: ArrayList al = ((ArrayList) inObj);
432: //
433: BigDecimal startIndex = ((BigDecimal) inStartIndex);
434: BigDecimal endIndex = ((BigDecimal) inEndIndex);
435: //
436: outValue = new ArrayList(al.subList(startIndex.intValue(),
437: endIndex.intValue()));
438:
439: return outValue;
440: }
441:
442: //
443: ////
444: public final static Object getKeysFromMap(Object inObj) {
445:
446: Object outValue = null;
447:
448: HashMap hm = ((HashMap) inObj);
449: outValue = StandardDataTypes.getListInstance(hm.keySet());
450:
451: return outValue;
452: }
453:
454: public final static Object getIsValuePresentInMap(Object inObj,
455: Object inKey) {
456:
457: Object outValue = null;
458:
459: HashMap hm = ((HashMap) inObj);
460: outValue = new Boolean(hm.containsKey(inKey));
461:
462: return outValue;
463: }
464:
465: public final static void removeValueFromMap(Object inObj,
466: Object inKey) {
467:
468: HashMap hm = ((HashMap) inObj);
469: hm.remove(inKey);
470: }
471:
472: public final static Object getValueFromMap(Object inObj,
473: Object inKey) {
474:
475: Object outValue = null;
476:
477: HashMap hm = ((HashMap) inObj);
478: if (!hm.containsKey(inKey)) {
479: throw new UnexpectedSystemException("Map '" + hm
480: + "' does not contain key '" + inKey + "'");
481: }
482: outValue = hm.get(inKey);
483:
484: return outValue;
485: }
486:
487: public final static void placeValueInMap(Object inObj,
488: Object inKey, Object inValue) {
489:
490: HashMap hm = ((HashMap) inObj);
491: hm.put(inKey, inValue);
492: }
493:
494: //
495: ////
496: public final static void prepareBooleanColumn(
497: PreparedStatement inPs, Object inValue, int inIndex)
498: throws SQLException {
499: //
500: prepareObjectColumn(inPs, inValue, inIndex, Types.BOOLEAN);
501: }
502:
503: public final static void prepareDecimalColumn(
504: PreparedStatement inPs, Object inValue, int inIndex)
505: throws SQLException {
506: //
507: prepareObjectColumn(inPs, inValue, inIndex, Types.DECIMAL);
508: }
509:
510: public final static void prepareStringColumn(
511: PreparedStatement inPs, Object inValue, int inIndex)
512: throws SQLException {
513: //
514: prepareObjectColumn(inPs, inValue, inIndex, Types.VARCHAR);
515: }
516:
517: public final static void prepareBinaryColumn(
518: PreparedStatement inPs, Object inValue, int inIndex)
519: throws SQLException {
520: //
521: prepareObjectColumn(inPs, inValue, inIndex, Types.BINARY);
522: }
523:
524: public final static void prepareTimestampColumn(
525: PreparedStatement inPs, Object inValue, int inIndex)
526: throws SQLException {
527: //
528: prepareObjectColumn(inPs, inValue, inIndex, Types.TIMESTAMP);
529: }
530:
531: private final static void prepareObjectColumn(
532: PreparedStatement inPs, Object inValue, int inIndex,
533: int inType) throws SQLException {
534: //
535: if (inValue != null) {
536: inPs.setObject(inIndex + 1, inValue, inType);
537: } else {
538: inPs.setNull(inIndex + 1, inType);
539: }
540: }
541: //
542: }
|