001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.xbean.spring.generator; 018 019import java.util.Set; 020import java.util.HashSet; 021import java.util.Collections; 022 023/** 024 * @author Dain Sundstrom 025 * @version $Id$ 026 * @since 1.0 027 */ 028public class Type { 029 private final String name; 030 private final Type nestedType; 031 private final boolean primitive; 032 033 public static Type newSimpleType(String name) { 034 if (name == null) throw new NullPointerException("type"); 035 if (name.indexOf("[") >= 0 || name.indexOf("]") >= 0) { 036 throw new IllegalArgumentException("Name can not contain '[' or ']' " + name); 037 } 038 return new Type(name, null); 039 } 040 041 public static Type newArrayType(String type, int dimensions) { 042 if (type == null) throw new NullPointerException("type"); 043 if (dimensions < 1) throw new IllegalArgumentException("dimensions must be atleast one"); 044 StringBuffer buf = new StringBuffer(type.length() + (dimensions * 2)); 045 buf.append(type); 046 for (int i = 0; i < dimensions; i ++) { 047 buf.append("[]"); 048 } 049 return new Type(buf.toString(), newSimpleType(type)); 050 } 051 052 public static Type newCollectionType(String collectionType, Type elementType) { 053 if (collectionType == null) throw new NullPointerException("collectionType"); 054 if (elementType == null) throw new NullPointerException("elementType"); 055 return new Type(collectionType, elementType); 056 } 057 058 private Type(String name, Type nestedType) { 059 this.name = name; 060 this.nestedType = nestedType; 061 primitive = (nestedType == null) && primitives.contains(name); 062 } 063 064 public String getName() { 065 return name; 066 } 067 068 public Type getNestedType() { 069 return nestedType; 070 } 071 072 public boolean isCollection() { 073 return nestedType != null; 074 } 075 076 public boolean isPrimitive() { 077 return primitive; 078 } 079 080 public int hashCode() { 081 return super.hashCode(); 082 } 083 084 public boolean equals(Object obj) { 085 return super.equals(obj); 086 } 087 088 private static final Set primitives; 089 090 static { 091 Set set = new HashSet(); 092 set.add("boolean"); 093 set.add("byte"); 094 set.add("char"); 095 set.add("short"); 096 set.add("int"); 097 set.add("long"); 098 set.add("float"); 099 set.add("double"); 100 primitives = Collections.unmodifiableSet(set); 101 } 102}