View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.xbean.spring.generator;
18  
19  import java.util.Set;
20  import java.util.HashSet;
21  import java.util.Collections;
22  
23  /**
24   * @author Dain Sundstrom
25   * @version $Id$
26   * @since 1.0
27   */
28  public class Type {
29      private final String name;
30      private final Type nestedType;
31      private final boolean primitive;
32  
33      public static Type newSimpleType(String name) {
34          if (name == null) throw new NullPointerException("type");
35          if (name.indexOf("[") >= 0 || name.indexOf("]") >= 0) {
36              throw new IllegalArgumentException("Name can not contain '[' or ']' " + name);
37          }
38          return new Type(name, null);
39      }
40  
41      public static Type newArrayType(String type, int dimensions) {
42          if (type == null) throw new NullPointerException("type");
43          if (dimensions < 1) throw new IllegalArgumentException("dimensions must be atleast one");
44          StringBuffer buf = new StringBuffer(type.length() + (dimensions * 2));
45          buf.append(type);
46          for (int i = 0; i < dimensions; i ++) {
47              buf.append("[]");
48          }
49          return new Type(buf.toString(), newSimpleType(type));
50      }
51  
52      public static Type newCollectionType(String collectionType, Type elementType) {
53          if (collectionType == null) throw new NullPointerException("collectionType");
54          if (elementType == null) throw new NullPointerException("elementType");
55          return new Type(collectionType, elementType);
56      }
57  
58      private Type(String name, Type nestedType) {
59          this.name = name;
60          this.nestedType = nestedType;
61          primitive = (nestedType == null) && primitives.contains(name);
62      }
63  
64      public String getName() {
65          return name;
66      }
67  
68      public Type getNestedType() {
69          return nestedType;
70      }
71  
72      public boolean isCollection() {
73          return nestedType != null;
74      }
75  
76      public boolean isPrimitive() {
77          return primitive;
78      }
79  
80      public int hashCode() {
81          return super.hashCode();
82      }
83  
84      public boolean equals(Object obj) {
85          return super.equals(obj);
86      }
87  
88      private static final Set primitives;
89  
90      static {
91          Set set = new HashSet();
92          set.add("boolean");
93          set.add("byte");
94          set.add("char");
95          set.add("short");
96          set.add("int");
97          set.add("long");
98          set.add("float");
99          set.add("double");
100         primitives = Collections.unmodifiableSet(set);
101     }
102 }