001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.runtime.support.stubgen.model;
018    
019    /**
020     * Representation of a type definition.
021     *
022     * @version $Id: TypeDef.java 18 2009-07-16 09:39:40Z user57 $
023     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
024     */
025    public class TypeDef
026        extends NamedElement
027    {
028        public static final String BYTE = "byte";
029    
030        public static final String SHORT = "short";
031    
032        public static final String INT = "int";
033    
034        public static final String LONG = "long";
035    
036        public static final String FLOAT = "float";
037    
038        public static final String DOUBLE = "double";
039    
040        public static final String CHAR = "char";
041    
042        public static final String BOOLEAN = "boolean";
043    
044        public static final String OBJECT = Object.class.getName();
045    
046        public static final String STRING = String.class.getName();
047    
048        public static final String BIG_INT = java.math.BigInteger.class.getName();
049    
050        public static final String BIG_DECIMAL = java.math.BigDecimal.class.getName();
051    
052        public static final String VOID = "void";
053    
054        public static final String NULL = "null";
055    
056        private int dimensions;
057    
058        public TypeDef() {}
059    
060        public TypeDef(final String name) {
061            setName(name);
062        }
063    
064        public TypeDef(final String name, final int n) {
065            setName(name);
066            setDimensions(n);
067    
068        }
069        public int getDimensions() {
070            return dimensions;
071        }
072    
073        public void setDimensions(final int n) {
074            this.dimensions = n;
075        }
076    
077        public void setName(final String name) {
078            // NOTE: Try to keep some sanity, this isn't fool-proof, but should help some to avoid crappy errors.
079            if ("Object".equals(name)) {
080                super.setName(OBJECT);
081            }
082            else if ("String".equals(name)) {
083                super.setName(STRING);
084            }
085            else {
086                super.setName(name);
087            }
088        }
089        
090        public String toString() {
091            StringBuffer buff = new StringBuffer();
092    
093            String name = getName();
094    
095            if (name == null) {
096                name = OBJECT;
097            }
098    
099            buff.append(name);
100    
101            if (dimensions > 0) {
102                buff.append("[");
103                buff.append(dimensions);
104            }
105    
106            return buff.toString();
107        }
108    
109        public String getDefaultValue() {
110            String name = getName();
111    
112            //
113            // NOTE: Default values taken from the "Default Values" section of:
114            //       http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
115            //
116    
117            if (name == null) {
118                return NULL;
119            }
120            else if (dimensions > 0) {
121                return NULL;
122            }
123            else if (name.equals(BYTE)) {
124                return "0";
125            }
126            else if (name.equals(SHORT)) {
127                return "0";
128            }
129            else if (name.equals(INT)) {
130                return "0";
131            }
132            else if (name.equals(LONG)) {
133                return "0L";
134            }
135            else if (name.equals(FLOAT)) {
136                return "0.0f";
137            }
138            else if (name.equals(DOUBLE)) {
139                return "0.0d";
140            }
141            else if (name.equals(CHAR)) {
142                return "'\\u0000'";
143            }
144            else if (name.equals(BOOLEAN)) {
145                return "false";
146            }
147            else {
148                return NULL;
149            }
150        }
151    
152        public boolean isBoolean() {
153            return BOOLEAN.equals(getName());
154        }
155    }