View Javadoc

1   /*
2    * Copyright 2004 Ronald Blaschke.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rblasch.convert;
17  
18  import org.rblasch.convert.converters.ObjectToMetaConvertersConverter;
19  import org.rblasch.convert.converters.OnceConverterFactory;
20  import org.rblasch.convert.converters.collections.ArrayConverterFactory;
21  import org.rblasch.convert.converters.collections.ArrayToListConverterFactory;
22  import org.rblasch.convert.converters.collections.ListConverterFactory;
23  import org.rblasch.convert.converters.lang.JlConverterFactory;
24  import org.rblasch.convert.converters.primitives.ByteToBooleanConverter;
25  import org.rblasch.convert.converters.primitives.StringToBooleanConverter;
26  import org.rblasch.convert.converters.primitives.StringToIntegerConverter;
27  import org.rblasch.convert.type.Type;
28  import org.rblasch.convert.type.Types;
29  
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.Set;
34  
35  /***
36   * @author Ronald Blaschke
37   * @api
38   */
39  public class SptConverter implements TargetedConverter {
40      private final ShortestPathSearch search = new ShortestPathSearch();
41  
42      private static Set/*<MetaConverter>*/ defaultConverters() {
43          final Set/*<MetaConverter>*/ cs = new HashSet();
44          // currently disabled, until basic stuff is working, to avoid converter cluttering
45          if (false) {
46              cs.add(new ByteToBooleanConverter());
47              //cs.add(new IntegerToByteConverter());
48              cs.add(new StringToIntegerConverter());
49              cs.add(new StringToBooleanConverter());
50          }
51          return cs;
52      }
53  
54      private static Set/*<ConverterFactory>*/ defaultFactories() {
55          final Set/*<ConverterFactory>*/ cf = new HashSet();
56          cf.add(new JlConverterFactory());
57          cf.add(new ArrayToListConverterFactory());
58          cf.add(new ArrayConverterFactory());
59          cf.add(new ListConverterFactory());
60          return cf;
61      }
62  
63      public static SptConverter createEmpty() {
64          return new SptConverter();
65      }
66  
67      public static SptConverter createJl() {
68          final SptConverter c = new SptConverter();
69          c.addFactory(new JlConverterFactory());
70          return c;
71      }
72  
73      public static SptConverter createDefault() {
74          return new SptConverter(defaultConverters(), defaultFactories());
75      }
76  
77      public SptConverter() {
78      }
79  
80      public SptConverter(final Set/*<MetaConverter>*/ converters) {
81          search.addAllConverters(converters);
82      }
83  
84      public SptConverter(final Set/*<MetaConverter>*/ converters, final Set/*<ConverterFactory>*/ factories) {
85          search.addAllConverters(converters);
86          search.addAllFactories(factories);
87      }
88  
89      public void addConverter(final MetaConverter converter) {
90          search.addConverter(converter);
91      }
92  
93      public void addDynamicConverter(final Object converter) {
94          final SptConverter c = new SptConverter();
95          c.addConverter(new ObjectToMetaConvertersConverter(100));
96  
97          final Set/*<MetaConverter>*/ converters = (Set) c.convert(converter, Types.findPTypeByClass(Set.class, MetaConverter.class));
98          for (final Iterator i = converters.iterator(); i.hasNext();) {
99              final MetaConverter mc = (MetaConverter) i.next();
100             search.addConverter(mc);
101         }
102     }
103 
104     public void addFactory(final ConverterFactory factory) {
105         search.addFactory(factory);
106     }
107 
108     public void addOnceFactory(final OnceConverterFactory once) {
109         search.addOnceFactory(once);
110     }
111 
112     public Set/*<MetaConverter>*/ removeAllConverters(final Type sourceType, final Type destinationType) {
113         throw new UnsupportedOperationException("removeAllConverters not yet implemented");
114     }
115 
116     public Object convert(final Object value, final Type destinationType) throws ConversionException {
117         return convert(value, value == null ? Types.nullType() : Types.findTypeByClass(value.getClass()), destinationType);
118     }
119 
120     public Object convert(final Object value, final Type sourceType, final Type destinationType) throws ConversionException {
121         try {
122             return search.lookup(sourceType, destinationType).getConverter()
123                     .convert(value);
124         } catch (final RuntimeException ex) {
125             throw ex;  // propagate runtime exceptions
126         } catch (final Exception ex) {
127             throw new ConversionFailedException(ex);
128         }
129     }
130 
131     String dumpGraph() {
132         return search.dumpGraph();
133     }
134 }