KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > cmr > repository > datatype > ValueConverter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.cmr.repository.datatype;
18
19 import java.math.BigDecimal JavaDoc;
20 import java.math.BigInteger JavaDoc;
21 import java.text.DecimalFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.alfresco.service.cmr.dictionary.DictionaryException;
31 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.service.cmr.repository.Path;
34 import org.alfresco.service.namespace.QName;
35 import org.alfresco.util.CachingDateFormat;
36 import org.alfresco.util.ParameterCheck;
37
38 /**
39  * Support for generic conversion between types.
40  *
41  * Additional conversions may be added. Basic interoperabikitynos supported.
42  *
43  * Direct conversion and two stage conversions via Number are supported. We do
44  * not support conversion by any route at the moment
45  *
46  * TODO: Add conversion for binary as byte[] for UTF8 String encoding
47  *
48  * TODO: Add support for QName
49  *
50  * TODO: Add support for Path
51  *
52  * TODO: Add support for lucene
53  *
54  * TODO: Add suport to check of a type is convertable
55  *
56  * TODO: Support for dynamically manging converions
57  *
58  * @author andyh
59  *
60  */

61 public class ValueConverter
62 {
63     /**
64      * General conversion method to Object types (note it cannot support
65      * conversion to primary types due the restrictions of reflection. Use the
66      * static conversion methods to primitive types)
67      *
68      * @param propertyType - the target property type
69      * @param value - the value to be converted
70      * @return - the converted value as the correct type
71      */

72     public static Object JavaDoc convert(DataTypeDefinition propertyType, Object JavaDoc value)
73     {
74         ParameterCheck.mandatory("Property type definition", propertyType);
75         
76         // Convert property type to java class
77
Class JavaDoc javaClass = null;
78         String JavaDoc javaClassName = propertyType.getJavaClassName();
79         try
80         {
81             javaClass = Class.forName(javaClassName);
82         }
83         catch (ClassNotFoundException JavaDoc e)
84         {
85             throw new DictionaryException("Java class " + javaClassName + " of property type " + propertyType.getName() + " is invalid", e);
86         }
87         
88         return convert(javaClass, value);
89     }
90     
91     
92     /**
93      * General conversion method to Object types (note it cannot support
94      * conversion to primary types due the restrictions of reflection. Use the
95      * static conversion methods to primitive types)
96      *
97      * @param <T> The target type for the result of the conversion
98      * @param c - a class for the target type
99      * @param value - the value to be converted
100      * @return - the converted value as the correct type
101      */

102     public static <T> T convert(Class JavaDoc<T> c, Object JavaDoc value)
103     {
104         // If multi value then default to using the first
105
if (value instanceof Collection JavaDoc)
106         {
107             Object JavaDoc valueObject = null;
108             for (Object JavaDoc o : (Collection JavaDoc) value)
109             {
110                 valueObject = o;
111                 break;
112             }
113             return convert(c, valueObject);
114         }
115         
116         if(value == null)
117         {
118             return null;
119         }
120
121         // Primative types
122
if (c.isPrimitive())
123         {
124             // We can not suport primitive type conversion
125
throw new UnsupportedOperationException JavaDoc("Can not convert direct to primitive type " + c.getName());
126         }
127
128         // Check if we already have the correct type
129
if (c.isInstance(value))
130         {
131             return c.cast(value);
132         }
133
134         // Find the correct conversion - if available and do the converiosn
135
Converter converter = getConversion(value.getClass(), c);
136         return (T) converter.convert(value);
137
138     }
139
140     /**
141      * Is the value multi valued
142      *
143      * @param value
144      * @return true - if the underlyinf is a collection of values and not a singole value
145      */

146     public static boolean isMultiValued(Object JavaDoc value)
147     {
148         return ((value instanceof Collection JavaDoc) && (((Collection JavaDoc) value).size() > 1));
149     }
150
151     /**
152      * Get the number of values represented
153      * @param value
154      * @return 1 for norma values and the size of the collection for MVPs
155      */

156     public static int size(Object JavaDoc value)
157     {
158         if (value instanceof Collection JavaDoc)
159         {
160             return ((Collection JavaDoc) value).size();
161         }
162         else
163         {
164             return 1;
165         }
166     }
167
168     /**
169      * Get a typed iterator over the values in the multi valued collection
170      * Also works for single value
171      *
172      * next() may fail with conversion failures as we convert as we go.
173      *
174      * @param <T> The target type
175      * @param c - the traget class
176      * @param value - the value to convert
177      * @return - a simple iterator over the values
178      */

179     public static <T> Iterator JavaDoc<T> iterator(Class JavaDoc<T> c, Object JavaDoc value)
180     {
181         Collection JavaDoc coll = createCollection(value);
182         Iterator JavaDoc it = coll.iterator();
183
184         Iterator JavaDoc<T> retit = new ConvertingIterator<T>(it, c);
185
186         return retit;
187     }
188
189     private static Collection JavaDoc createCollection(Object JavaDoc value)
190     {
191         Collection JavaDoc coll;
192         if (ValueConverter.isMultiValued(value))
193         {
194             coll = (Collection JavaDoc) value;
195         }
196         else
197         {
198             ArrayList JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>(1);
199             list.add(value);
200             coll = list;
201         }
202         return coll;
203     }
204
205     /**
206      * Get a collection for the values witha converting iterator
207      *
208      * @param c
209      * @param value
210      * @return
211      */

212     public static <T> Collection JavaDoc<T> getCollection(Class JavaDoc<T> c, Object JavaDoc value)
213     {
214         Collection JavaDoc coll = createCollection(value);
215         return new ConvertingCollection<T>(coll, c);
216     }
217     
218     /**
219      * Helper call for converting as iteratong over a collection
220      * @author andyh
221      *
222      * @param <T>
223      */

224     private static class ConvertingIterator<T> implements Iterator JavaDoc<T>
225     {
226         Iterator JavaDoc it;
227
228         Class JavaDoc type;
229
230         ConvertingIterator(Iterator JavaDoc it, Class JavaDoc type)
231         {
232             this.it = it;
233             this.type = type;
234         }
235
236         public boolean hasNext()
237         {
238             return it.hasNext();
239         }
240
241         public T next()
242         {
243             return (T) ValueConverter.convert(type, it.next());
244         }
245
246         public void remove()
247         {
248             throw new UnsupportedOperationException JavaDoc();
249         }
250
251     }
252     
253     private static class ConvertingCollection<T> implements Collection JavaDoc<T>
254     {
255         private Collection JavaDoc<?> base;
256         private Class JavaDoc c;
257         
258         ConvertingCollection(Collection JavaDoc<?> base, Class JavaDoc c)
259         {
260             super();
261             this.base = base;
262             this.c = c;
263         }
264
265         public boolean add(T o)
266         {
267             throw new UnsupportedOperationException JavaDoc();
268         }
269
270         public boolean addAll(Collection JavaDoc c)
271         {
272             throw new UnsupportedOperationException JavaDoc();
273         }
274
275         public void clear()
276         {
277             throw new UnsupportedOperationException JavaDoc();
278         }
279
280         public boolean contains(Object JavaDoc o)
281         {
282             return base.contains(o);
283         }
284
285         public boolean containsAll(Collection JavaDoc<?> c)
286         {
287             return base.containsAll(c);
288         }
289
290         public boolean equals(Object JavaDoc o)
291         {
292            return base.equals(o);
293         }
294
295         public int hashCode()
296         {
297            return base.hashCode();
298         }
299
300         public boolean isEmpty()
301         {
302            return base.isEmpty();
303         }
304
305         public Iterator JavaDoc<T> iterator()
306         {
307             Iterator JavaDoc it = base.iterator();
308             Iterator JavaDoc<T> retit = new ConvertingIterator<T>(it, c);
309             return retit;
310         }
311
312         public boolean remove(Object JavaDoc o)
313         {
314             throw new UnsupportedOperationException JavaDoc();
315         }
316
317         public boolean removeAll(Collection JavaDoc c)
318         {
319             throw new UnsupportedOperationException JavaDoc();
320         }
321
322         public boolean retainAll(Collection JavaDoc c)
323         {
324             throw new UnsupportedOperationException JavaDoc();
325         }
326
327         public int size()
328         {
329             return base.size();
330         }
331
332         public Object JavaDoc[] toArray()
333         {
334             // TODO Auto-generated method stub
335
throw new UnsupportedOperationException JavaDoc();
336         }
337
338         public <O> O[] toArray(O[] a)
339         {
340             // TODO Auto-generated method stub
341
throw new UnsupportedOperationException JavaDoc();
342         }
343         
344         
345     }
346     
347     /**
348      * Get the boolean value for the value object
349      * May have conversion failure
350      *
351      * @param value
352      * @return
353      */

354     public static boolean booleanValue(Object JavaDoc value)
355     {
356         return convert(Boolean JavaDoc.class, value).booleanValue();
357     }
358
359     
360     /**
361      * Get the char value for the value object
362      * May have conversion failure
363      *
364      * @param value
365      * @return
366      */

367     public static char charValue(Object JavaDoc value)
368     {
369         return convert(Character JavaDoc.class, value).charValue();
370     }
371
372     
373     /**
374      * Get the byte value for the value object
375      * May have conversion failure
376      *
377      * @param value
378      * @return
379      */

380     public static byte byteValue(Object JavaDoc value)
381     {
382         if (value instanceof Number JavaDoc)
383         {
384             return ((Number JavaDoc) value).byteValue();
385         }
386         return convert(Number JavaDoc.class, value).byteValue();
387     }
388
389     /**
390      * Get the short value for the value object
391      * May have conversion failure
392      *
393      * @param value
394      * @return
395      */

396     public static short shortValue(Object JavaDoc value)
397     {
398         if (value instanceof Number JavaDoc)
399         {
400             return ((Number JavaDoc) value).shortValue();
401         }
402         return convert(Number JavaDoc.class, value).shortValue();
403     }
404
405     
406     /**
407      * Get the int value for the value object
408      * May have conversion failure
409      *
410      * @param value
411      * @return
412      */

413     public static int intValue(Object JavaDoc value)
414     {
415         if (value instanceof Number JavaDoc)
416         {
417             return ((Number JavaDoc) value).intValue();
418         }
419         return convert(Number JavaDoc.class, value).intValue();
420     }
421
422     
423     /**
424      * Get the long value for the value object
425      * May have conversion failure
426      *
427      * @param value
428      * @return
429      */

430     public static long longValue(Object JavaDoc value)
431     {
432         if (value instanceof Number JavaDoc)
433         {
434             return ((Number JavaDoc) value).longValue();
435         }
436         return convert(Number JavaDoc.class, value).longValue();
437     }
438
439     /**
440      * Get the bollean value for the value object
441      * May have conversion failure
442      *
443      * @param float
444      * @return
445      */

446     public static float floatValue(Object JavaDoc value)
447     {
448         if (value instanceof Number JavaDoc)
449         {
450             return ((Number JavaDoc) value).floatValue();
451         }
452         return convert(Number JavaDoc.class, value).floatValue();
453     }
454
455     
456     /**
457      * Get the bollean value for the value object
458      * May have conversion failure
459      *
460      * @param double
461      * @return
462      */

463     public static double doubleValue(Object JavaDoc value)
464     {
465         if (value instanceof Number JavaDoc)
466         {
467             return ((Number JavaDoc) value).doubleValue();
468         }
469         return convert(Number JavaDoc.class, value).doubleValue();
470     }
471
472     /**
473      * Find a conversion
474      *
475      * @param <F>
476      * @param <T>
477      * @param source
478      * @param dest
479      * @return
480      */

481     private static <F, T> Converter getConversion(Class JavaDoc<F> source, Class JavaDoc<T> dest)
482     {
483         Converter<?, ?> converter = null;
484         Class JavaDoc clazz = source;
485         do
486         {
487             Map JavaDoc<Class JavaDoc, Converter> map = conversions.get(clazz);
488             if (map == null)
489             {
490                 continue;
491             }
492             converter = map.get(dest);
493             if (converter == null)
494             {
495                 Converter<?, ?> first = map.get(Number JavaDoc.class);
496                 Converter<?, ?> second = null;
497                 if (first != null)
498                 {
499                     map = conversions.get(Number JavaDoc.class);
500                     if (map != null)
501                     {
502                         second = map.get(dest);
503                     }
504                 }
505                 if (second != null)
506                 {
507                     converter = new TwoStageConverter<F, T, Number JavaDoc>(first, second);
508                 }
509
510             }
511         }
512         while ((converter == null) && ((clazz = clazz.getSuperclass()) != null));
513
514         if (converter == null)
515         {
516             throw new UnsupportedOperationException JavaDoc("There are is no conversion registered from source type " + source.getName() + " to " + dest);
517
518         }
519         return converter;
520     }
521
522     /**
523      * Add a converter to the list of those available
524      *
525      * @param <F>
526      * @param <T>
527      * @param source
528      * @param destination
529      * @param converter
530      */

531     public static <F, T> void addConverter(Class JavaDoc<F> source, Class JavaDoc<T> destination, Converter<F, T> converter)
532     {
533         Map JavaDoc<Class JavaDoc, Converter> map = conversions.get(source);
534         if (map == null)
535         {
536             map = new HashMap JavaDoc<Class JavaDoc, Converter>();
537             conversions.put(source, map);
538         }
539         map.put(destination, converter);
540     }
541     
542     /**
543      * Map of conversion
544      */

545     static Map JavaDoc<Class JavaDoc, Map JavaDoc<Class JavaDoc, Converter>> conversions = new HashMap JavaDoc<Class JavaDoc, Map JavaDoc<Class JavaDoc, Converter>>();
546
547     /**
548      * Initialise the starting conversions
549      */

550     static
551     {
552         //
553
// From string
554
//
555

556         Map JavaDoc<Class JavaDoc, Converter> map = new HashMap JavaDoc<Class JavaDoc, Converter>();
557         conversions.put(String JavaDoc.class, map);
558         map.put(Boolean JavaDoc.class, new Converter<String JavaDoc, Boolean JavaDoc>()
559         {
560             public Boolean JavaDoc convert(String JavaDoc source)
561             {
562                 return Boolean.valueOf(source);
563             }
564
565         });
566
567         map.put(Character JavaDoc.class, new Converter<String JavaDoc, Character JavaDoc>()
568         {
569             public Character JavaDoc convert(String JavaDoc source)
570             {
571                 if ((source == null) || (source.length() == 0))
572                 {
573                     return null;
574                 }
575                 return Character.valueOf(source.charAt(0));
576             }
577
578         });
579
580         map.put(Number JavaDoc.class, new Converter<String JavaDoc, Number JavaDoc>()
581         {
582             public Number JavaDoc convert(String JavaDoc source)
583             {
584                 try
585                 {
586                     return DecimalFormat.getNumberInstance().parse(source);
587                 }
588                 catch (ParseException JavaDoc e)
589                 {
590                     throw new RuntimeException JavaDoc(e);
591                 }
592             }
593
594         });
595
596         map.put(Byte JavaDoc.class, new Converter<String JavaDoc, Byte JavaDoc>()
597         {
598             public Byte JavaDoc convert(String JavaDoc source)
599             {
600                 return Byte.valueOf(source);
601             }
602
603         });
604
605         map.put(Short JavaDoc.class, new Converter<String JavaDoc, Short JavaDoc>()
606         {
607             public Short JavaDoc convert(String JavaDoc source)
608             {
609                 return Short.valueOf(source);
610             }
611
612         });
613
614         map.put(Integer JavaDoc.class, new Converter<String JavaDoc, Integer JavaDoc>()
615         {
616             public Integer JavaDoc convert(String JavaDoc source)
617             {
618                 return Integer.valueOf(source);
619             }
620
621         });
622
623         map.put(Long JavaDoc.class, new Converter<String JavaDoc, Long JavaDoc>()
624         {
625             public Long JavaDoc convert(String JavaDoc source)
626             {
627                 return Long.valueOf(source);
628             }
629
630         });
631
632         map.put(Float JavaDoc.class, new Converter<String JavaDoc, Float JavaDoc>()
633         {
634             public Float JavaDoc convert(String JavaDoc source)
635             {
636                 return Float.valueOf(source);
637             }
638
639         });
640
641         map.put(Double JavaDoc.class, new Converter<String JavaDoc, Double JavaDoc>()
642         {
643             public Double JavaDoc convert(String JavaDoc source)
644             {
645                 return Double.valueOf(source);
646             }
647
648         });
649
650         map.put(BigInteger JavaDoc.class, new Converter<String JavaDoc, BigInteger JavaDoc>()
651         {
652             public BigInteger JavaDoc convert(String JavaDoc source)
653             {
654                 return new BigInteger JavaDoc(source);
655             }
656
657         });
658
659         map.put(BigDecimal JavaDoc.class, new Converter<String JavaDoc, BigDecimal JavaDoc>()
660         {
661             public BigDecimal JavaDoc convert(String JavaDoc source)
662             {
663                 return new BigDecimal JavaDoc(source);
664             }
665
666         });
667
668         map.put(Date JavaDoc.class, new Converter<String JavaDoc, Date JavaDoc>()
669         {
670             public Date JavaDoc convert(String JavaDoc source)
671             {
672                 try
673                 {
674                     return CachingDateFormat.getDateFormat().parse(source);
675                 }
676                 catch (ParseException JavaDoc e)
677                 {
678                     throw new RuntimeException JavaDoc(e);
679                 }
680             }
681
682         });
683
684         map.put(Duration.class, new Converter<String JavaDoc, Duration>()
685         {
686             public Duration convert(String JavaDoc source)
687             {
688                 return new Duration(source);
689             }
690
691         });
692         
693         map.put(QName.class, new Converter<String JavaDoc, QName>()
694                 {
695                     public QName convert(String JavaDoc source)
696                     {
697                         return QName.createQName(source);
698                     }
699
700                 });
701         
702         map.put(NodeRef.class, new Converter<String JavaDoc, NodeRef>()
703                 {
704                     public NodeRef convert(String JavaDoc source)
705                     {
706                         return new NodeRef(source);
707                     }
708
709                 });
710
711         //
712
// Number to Subtypes and Date
713
//
714

715         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
716         conversions.put(Number JavaDoc.class, map);
717
718         map.put(Byte JavaDoc.class, new Converter<Number JavaDoc, Byte JavaDoc>()
719         {
720             public Byte JavaDoc convert(Number JavaDoc source)
721             {
722                 return Byte.valueOf(source.byteValue());
723             }
724
725         });
726
727         map.put(Short JavaDoc.class, new Converter<Number JavaDoc, Short JavaDoc>()
728         {
729             public Short JavaDoc convert(Number JavaDoc source)
730             {
731                 return Short.valueOf(source.shortValue());
732             }
733
734         });
735
736         map.put(Integer JavaDoc.class, new Converter<Number JavaDoc, Integer JavaDoc>()
737         {
738             public Integer JavaDoc convert(Number JavaDoc source)
739             {
740                 return Integer.valueOf(source.intValue());
741             }
742
743         });
744
745         map.put(Long JavaDoc.class, new Converter<Number JavaDoc, Long JavaDoc>()
746         {
747             public Long JavaDoc convert(Number JavaDoc source)
748             {
749                 return Long.valueOf(source.longValue());
750             }
751
752         });
753
754         map.put(Float JavaDoc.class, new Converter<Number JavaDoc, Float JavaDoc>()
755         {
756             public Float JavaDoc convert(Number JavaDoc source)
757             {
758                 return Float.valueOf(source.floatValue());
759             }
760
761         });
762
763         map.put(Double JavaDoc.class, new Converter<Number JavaDoc, Double JavaDoc>()
764         {
765             public Double JavaDoc convert(Number JavaDoc source)
766             {
767                 return Double.valueOf(source.doubleValue());
768             }
769
770         });
771
772         map.put(Date JavaDoc.class, new Converter<Number JavaDoc, Date JavaDoc>()
773         {
774             public Date JavaDoc convert(Number JavaDoc source)
775             {
776                 return new Date JavaDoc(source.longValue());
777             }
778
779         });
780
781         map.put(String JavaDoc.class, new Converter<Number JavaDoc, String JavaDoc>()
782         {
783             public String JavaDoc convert(Number JavaDoc source)
784             {
785                 return source.toString();
786             }
787
788         });
789
790         map.put(BigInteger JavaDoc.class, new Converter<Number JavaDoc, BigInteger JavaDoc>()
791         {
792             public BigInteger JavaDoc convert(Number JavaDoc source)
793             {
794                 if (source instanceof BigDecimal JavaDoc)
795                 {
796                     return ((BigDecimal JavaDoc) source).toBigInteger();
797                 }
798                 else
799                 {
800                     return BigInteger.valueOf(source.longValue());
801                 }
802             }
803
804         });
805
806         map.put(BigDecimal JavaDoc.class, new Converter<Number JavaDoc, BigDecimal JavaDoc>()
807         {
808             public BigDecimal JavaDoc convert(Number JavaDoc source)
809             {
810                 if (source instanceof BigInteger JavaDoc)
811                 {
812                     return new BigDecimal JavaDoc((BigInteger JavaDoc) source);
813                 }
814                 else
815                 {
816                     return BigDecimal.valueOf(source.longValue());
817                 }
818             }
819
820         });
821
822         //
823
// Date ->
824
//
825

826         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
827         conversions.put(Date JavaDoc.class, map);
828
829         map.put(Number JavaDoc.class, new Converter<Date JavaDoc, Number JavaDoc>()
830         {
831             public Number JavaDoc convert(Date JavaDoc source)
832             {
833                 return Long.valueOf(source.getTime());
834             }
835
836         });
837
838         map.put(String JavaDoc.class, new Converter<Date JavaDoc, String JavaDoc>()
839         {
840             public String JavaDoc convert(Date JavaDoc source)
841             {
842                 return CachingDateFormat.getDateFormat().format(source);
843             }
844
845         });
846
847         //
848
// Boolean ->
849
//
850

851         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
852         conversions.put(Boolean JavaDoc.class, map);
853
854         map.put(String JavaDoc.class, new Converter<Boolean JavaDoc, String JavaDoc>()
855         {
856             public String JavaDoc convert(Boolean JavaDoc source)
857             {
858                 return source.toString();
859             }
860
861         });
862
863         //
864
// Character ->
865
//
866

867         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
868         conversions.put(Character JavaDoc.class, map);
869
870         map.put(String JavaDoc.class, new Converter<Character JavaDoc, String JavaDoc>()
871         {
872             public String JavaDoc convert(Character JavaDoc source)
873             {
874                 return source.toString();
875             }
876
877         });
878
879         //
880
// Duration ->
881
//
882

883         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
884         conversions.put(Duration.class, map);
885
886         map.put(String JavaDoc.class, new Converter<Duration, String JavaDoc>()
887         {
888             public String JavaDoc convert(Duration source)
889             {
890                 return source.toString();
891             }
892
893         });
894
895         //
896
// Byte
897
//
898

899         
900         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
901         conversions.put(Byte JavaDoc.class, map);
902
903         map.put(String JavaDoc.class, new Converter<Byte JavaDoc, String JavaDoc>()
904         {
905             public String JavaDoc convert(Byte JavaDoc source)
906             {
907                 return source.toString();
908             }
909
910         });
911
912         //
913
// Short
914
//
915

916         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
917         conversions.put(Short JavaDoc.class, map);
918
919         map.put(String JavaDoc.class, new Converter<Short JavaDoc, String JavaDoc>()
920         {
921             public String JavaDoc convert(Short JavaDoc source)
922             {
923                 return source.toString();
924             }
925
926         });
927
928         //
929
// Integer
930
//
931

932         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
933         conversions.put(Integer JavaDoc.class, map);
934
935         map.put(String JavaDoc.class, new Converter<Integer JavaDoc, String JavaDoc>()
936         {
937             public String JavaDoc convert(Integer JavaDoc source)
938             {
939                 return source.toString();
940             }
941
942         });
943
944         //
945
// Long
946
//
947

948         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
949         conversions.put(Long JavaDoc.class, map);
950
951         map.put(String JavaDoc.class, new Converter<Long JavaDoc, String JavaDoc>()
952         {
953             public String JavaDoc convert(Long JavaDoc source)
954             {
955                 return source.toString();
956             }
957
958         });
959
960         //
961
// Float
962
//
963

964         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
965         conversions.put(Float JavaDoc.class, map);
966
967         map.put(String JavaDoc.class, new Converter<Float JavaDoc, String JavaDoc>()
968         {
969             public String JavaDoc convert(Float JavaDoc source)
970             {
971                 return source.toString();
972             }
973
974         });
975
976         //
977
// Double
978
//
979

980         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
981         conversions.put(Double JavaDoc.class, map);
982
983         map.put(String JavaDoc.class, new Converter<Double JavaDoc, String JavaDoc>()
984         {
985             public String JavaDoc convert(Double JavaDoc source)
986             {
987                 return source.toString();
988             }
989
990         });
991
992         //
993
// BigInteger
994
//
995

996         map = new HashMap JavaDoc<Class JavaDoc, Converter>();
997         conversions.put(BigInteger JavaDoc.class, map);
998
999         map.put(String JavaDoc.class, new Converter<BigInteger JavaDoc, String JavaDoc>()
1000        {
1001            public String JavaDoc convert(BigInteger JavaDoc source)
1002            {
1003                return source.toString();
1004            }
1005
1006        });
1007
1008        //
1009
// BigDecimal
1010
//
1011

1012        map = new HashMap JavaDoc<Class JavaDoc, Converter>();
1013        conversions.put(BigDecimal JavaDoc.class, map);
1014
1015        map.put(String JavaDoc.class, new Converter<BigDecimal JavaDoc, String JavaDoc>()
1016        {
1017            public String JavaDoc convert(BigDecimal JavaDoc source)
1018            {
1019                return source.toString();
1020            }
1021
1022        });
1023        
1024        //
1025
// QName
1026
//
1027

1028        map = new HashMap JavaDoc<Class JavaDoc, Converter>();
1029        conversions.put(QName.class, map);
1030
1031        map.put(String JavaDoc.class, new Converter<QName, String JavaDoc>()
1032        {
1033            public String JavaDoc convert(QName source)
1034            {
1035                return source.toString();
1036            }
1037
1038        });
1039        
1040        //
1041
// NodeRef
1042
//
1043

1044        map = new HashMap JavaDoc<Class JavaDoc, Converter>();
1045        conversions.put(NodeRef.class, map);
1046
1047        map.put(String JavaDoc.class, new Converter<NodeRef, String JavaDoc>()
1048        {
1049            public String JavaDoc convert(NodeRef source)
1050            {
1051                return source.toString();
1052            }
1053
1054        });
1055        
1056        //
1057
// Path
1058
//
1059

1060        map = new HashMap JavaDoc<Class JavaDoc, Converter>();
1061        conversions.put(Path.class, map);
1062
1063        map.put(String JavaDoc.class, new Converter<Path, String JavaDoc>()
1064        {
1065            public String JavaDoc convert(Path source)
1066            {
1067                return source.toString();
1068            }
1069
1070        });
1071
1072    }
1073
1074    // Support for pluggable conversions
1075

1076    /**
1077     * Conversion interface
1078     * @author andyh
1079     *
1080     * @param <F> From type
1081     * @param <T> To type
1082     */

1083    public static interface Converter<F, T>
1084    {
1085        public T convert(F source);
1086    }
1087
1088    /**
1089     * Support for chaining conversions
1090     *
1091     * @author andyh
1092     *
1093     * @param <F> From Type
1094     * @param <T> To Type
1095     * @param <M> Intermediate type
1096     */

1097    public static class TwoStageConverter<F, T, M> implements Converter<F, T>
1098    {
1099        Converter first;
1100
1101        Converter second;
1102
1103        TwoStageConverter(Converter first, Converter second)
1104        {
1105            this.first = first;
1106            this.second = second;
1107        }
1108
1109        public T convert(F source)
1110        {
1111            return (T) second.convert((M) first.convert(source));
1112        }
1113    }
1114
1115}
1116
Popular Tags