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.type;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  
25  /***
26   * @author Ronald Blaschke
27   */
28  public class ParameterizableType extends AbstractType {
29      static ParameterizableType valueOf(final String s) throws UnknownTypeException {
30          if (s.endsWith("[]")) {
31              // special array syntax, eg int[], java.lang.Integer[]
32              final String componentName = s.substring(0, s.length() - 2);
33              final Type componentType = Types.findTypeByName(componentName);
34  
35              return new ParameterizableType(Types.findTypeByName("array"), componentType);
36          } else {
37              final int ltPos = s.indexOf('<');
38              final int gtPos = s.lastIndexOf('>');
39  
40              if (ltPos == -1 || gtPos == -1) {
41                  throw new UnknownTypeException(s + " is not a parameterizable type");
42              }
43  
44              final String parameters = s.substring(ltPos + 1, gtPos);
45  
46              final Type mainType = Types.findTypeByName(s.substring(0, ltPos));
47              final List/*<Type>*/ parameterTypes = new ArrayList();
48              for (final StringTokenizer tokenizer = new StringTokenizer(parameters, ","); tokenizer.hasMoreElements();) {
49                  final String token = tokenizer.nextToken();
50                  parameterTypes.add(Types.findTypeByName(token.trim()));
51              }
52  
53              return new ParameterizableType(mainType, parameterTypes);
54          }
55      }
56  
57      private final Type t;
58      private final List/*<Type>*/ parameters;
59  
60      ParameterizableType(final Type t, final Type p1) {
61          this(t, Collections.singletonList(p1));
62      }
63  
64      ParameterizableType(final Type t, final Type p1, final Type p2) {
65          this(t, Arrays.asList(new Type[]{p1, p2}));
66      }
67  
68      ParameterizableType(final Type t, final List/*<Type>*/ parameters) {
69          this.t = t;
70          this.parameters = new ArrayList(parameters);
71      }
72  
73      public Type getType() {
74          return t;
75      }
76  
77      public List/*<Type>*/ getParameters() {
78          return parameters;
79      }
80  
81      public Type getParameter(final int i) {
82          return (Type) parameters.get(i);
83      }
84  
85      public List/*<Type>*/ toList() {
86          final List/*<Type>*/ list = new ArrayList(1 + parameters.size());
87          list.add(t);
88          list.addAll(parameters);
89          return list;
90      }
91  
92      public String getName() {
93          final StringBuffer buffy = new StringBuffer();
94          buffy.append(t.getName());
95          buffy.append('<');
96          for (final Iterator i = parameters.iterator(); i.hasNext();) {
97              buffy.append(((Type) i.next()).getName());
98              if (i.hasNext()) {
99                  buffy.append(",");
100             }
101         }
102         buffy.append('>');
103         return buffy.toString();
104     }
105 }