1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
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 }