001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.generator.as3;
022    
023    import java.util.Collections;
024    import java.util.Set;
025    
026    
027    /**
028     * @author Franck WOLFF
029     */
030    public class As3Type implements ClientType {
031    
032        ///////////////////////////////////////////////////////////////////////////
033        // Fields.
034    
035        public static final As3Type INT = new As3Type(null, "int", Integer.valueOf(0));
036        public static final As3Type UINT = new As3Type(null, "uint", Integer.valueOf(0));
037        public static final As3Type BOOLEAN = new As3Type(null, "Boolean", Boolean.valueOf(false));
038        public static final As3Type NUMBER = new As3Type(null, "Number", "Number.NaN");
039        public static final As3Type LONG = new As3Type("org.granite.math", "Long");
040        public static final As3Type BIG_INTEGER = new As3Type("org.granite.math", "BigInteger");
041        public static final As3Type BIG_DECIMAL = new As3Type("org.granite.math", "BigDecimal");
042        public static final As3Type MATH_CONTEXT = new As3Type("org.granite.math", "MathContext");
043        public static final As3Type ROUNDING_MODE = new As3Type("org.granite.math", "RoundingMode");
044        public static final As3Type OBJECT = new As3Type(null, "Object");
045        public static final As3Type STRING = new As3Type(null, "String");
046        public static final As3Type ARRAY = new As3Type(null, "Array");
047        public static final As3Type DATE = new As3Type(null, "Date");
048        public static final As3Type XML = new As3Type(null, "XML");
049        public static final As3Type BYTE_ARRAY = new As3Type("flash.utils", "ByteArray");
050        public static final As3Type DICTIONARY = new As3Type("flash.utils", "Dictionary");
051    
052        public static final As3Type LIST_COLLECTION_VIEW = new As3Type("mx.collections", "ListCollectionView");
053        public static final As3Type ARRAY_COLLECTION = new As3Type("mx.collections", "ArrayCollection");
054        public static final As3Type ILIST = new As3Type("mx.collections", "IList");
055        public static final As3Type IMAP = new As3Type("org.granite.collections", "IMap");
056        public static final As3Type ENUM = new As3Type("org.granite.util", "Enum");
057    
058        public static final As3Type PAGE = new As3Type("org.granite.tide.data.model", "Page");
059        public static final As3Type PAGE_INFO = new As3Type("org.granite.tide.data.model", "PageInfo");
060        public static final As3Type SORT_INFO = new As3Type("org.granite.tide.data.model", "SortInfo");
061    
062        private final String packageName;
063        private final String name;
064        private final String qualifiedName;
065        private final Object nullValue;
066    
067        ///////////////////////////////////////////////////////////////////////////
068        // Constructors.
069    
070        public As3Type(String packageName, String simpleName) {
071            this(packageName, simpleName, null);
072        }
073        public As3Type(String packageName, String name, Object nullValue) {
074            this.packageName = (packageName != null ? packageName : "");
075            this.name = name;
076            this.qualifiedName = (hasPackage() ? (packageName + '.' + name) : name);
077            this.nullValue = nullValue;
078        }
079    
080        ///////////////////////////////////////////////////////////////////////////
081        // Properties.
082    
083        @Override
084            public boolean hasPackage() {
085            return packageName.length() > 0;
086        }
087    
088        @Override
089            public String getPackageName() {
090            return packageName;
091        }
092    
093        @Override
094            public String getName() {
095            return name;
096        }
097    
098        @Override
099            public String getQualifiedName() {
100            return qualifiedName;
101        }
102    
103        @Override
104            public Object getNullValue() {
105            return nullValue;
106        }
107    
108        public boolean isNumber() {
109            return NUMBER.equals(this);
110        }
111        
112        ///////////////////////////////////////////////////////////////////////////
113        // Methods
114        
115        @Override
116            public Set<String> getImports() {
117            if (hasPackage())
118                    return Collections.singleton(qualifiedName);
119            
120            return Collections.emptySet();
121        }
122        
123        @Override
124            public As3Type toArrayType() {
125            return ARRAY;
126        }
127        
128        @Override
129            public As3Type translatePackage(PackageTranslator translator) {
130            return new As3Type(translator.translate(packageName), getName());
131        }
132    
133        ///////////////////////////////////////////////////////////////////////////
134        // Utilities.
135    
136        @Override
137        public int hashCode() {
138            return qualifiedName.hashCode();
139        }
140    
141        @Override
142        public boolean equals(Object obj) {
143            if (obj == this)
144                return true;
145            if (!(obj instanceof As3Type))
146                return false;
147            return qualifiedName.equals(((As3Type)obj).qualifiedName);
148        }
149    
150        @Override
151        public String toString() {
152            return qualifiedName;
153        }
154    }