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