001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 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 */
022package org.granite.generator.as3;
023
024import java.util.Collections;
025import java.util.List;
026import java.util.Set;
027
028
029/**
030 * @author Franck WOLFF
031 */
032public class As3Type implements ClientType {
033
034    ///////////////////////////////////////////////////////////////////////////
035    // Fields.
036
037    public static final As3Type INT = new As3Type(null, "int", Integer.valueOf(0));
038    public static final As3Type UINT = new As3Type(null, "uint", Integer.valueOf(0));
039    public static final As3Type BOOLEAN = new As3Type(null, "Boolean", Boolean.valueOf(false));
040    public static final As3Type NUMBER = new As3Type(null, "Number", "Number.NaN");
041    public static final As3Type LONG = new As3Type("org.granite.math", "Long");
042    public static final As3Type BIG_INTEGER = new As3Type("org.granite.math", "BigInteger");
043    public static final As3Type BIG_DECIMAL = new As3Type("org.granite.math", "BigDecimal");
044    public static final As3Type MATH_CONTEXT = new As3Type("org.granite.math", "MathContext");
045    public static final As3Type ROUNDING_MODE = new As3Type("org.granite.math", "RoundingMode");
046    public static final As3Type OBJECT = new As3Type(null, "Object");
047    public static final As3Type STRING = new As3Type(null, "String");
048    public static final As3Type ARRAY = new As3Type(null, "Array");
049    public static final As3Type DATE = new As3Type(null, "Date");
050    public static final As3Type XML = new As3Type(null, "XML");
051    public static final As3Type BYTE_ARRAY = new As3Type("flash.utils", "ByteArray");
052    public static final As3Type DICTIONARY = new As3Type("flash.utils", "Dictionary");
053
054    public static final As3Type LIST_COLLECTION_VIEW = new As3Type("mx.collections", "ListCollectionView");
055    public static final As3Type ARRAY_COLLECTION = new As3Type("mx.collections", "ArrayCollection");
056    public static final As3Type ILIST = new As3Type("mx.collections", "IList");
057    public static final As3Type IMAP = new As3Type("org.granite.collections", "IMap");
058    public static final As3Type ENUM = new As3Type("org.granite.util", "Enum");
059
060    public static final As3Type PAGE = new As3Type("org.granite.tide.data.model", "Page");
061    public static final As3Type PAGE_INFO = new As3Type("org.granite.tide.data.model", "PageInfo");
062    public static final As3Type SORT_INFO = new As3Type("org.granite.tide.data.model", "SortInfo");
063
064    private final String packageName;
065    private final String name;
066    private final String qualifiedName;
067    private final Object nullValue;
068
069    ///////////////////////////////////////////////////////////////////////////
070    // Constructors.
071
072    public As3Type(String packageName, String simpleName) {
073        this(packageName, simpleName, null);
074    }
075    public As3Type(String packageName, String name, Object nullValue) {
076        this.packageName = (packageName != null ? packageName : "");
077        this.name = name;
078        this.qualifiedName = (hasPackage() ? (packageName + '.' + name) : name);
079        this.nullValue = nullValue;
080    }
081
082    ///////////////////////////////////////////////////////////////////////////
083    // Properties.
084
085    @Override
086        public boolean hasPackage() {
087        return packageName.length() > 0;
088    }
089
090    @Override
091        public String getPackageName() {
092        return packageName;
093    }
094
095    @Override
096        public String getName() {
097        return name;
098    }
099
100    @Override
101        public String getQualifiedName() {
102        return qualifiedName;
103    }
104
105    @Override
106        public Object getNullValue() {
107        return nullValue;
108    }
109
110    public boolean isNumber() {
111        return NUMBER.equals(this);
112    }
113    
114    ///////////////////////////////////////////////////////////////////////////
115    // Methods
116    
117    @Override
118        public Set<String> getImports() {
119        if (hasPackage())
120                return Collections.singleton(qualifiedName);
121        
122        return Collections.emptySet();
123    }
124    
125    @Override
126    public void addImports(Set<String> imports) {         
127    }
128    
129    @Override
130        public As3Type toArrayType() {
131        return ARRAY;
132    }
133    
134    @Override
135        public As3Type translatePackage(PackageTranslator translator) {
136        return new As3Type(translator.translate(packageName), getName());
137    }
138    
139    @Override
140    public As3Type translatePackages(List<PackageTranslator> translators) {
141        boolean translate = false;
142        
143        PackageTranslator translator = PackageTranslator.forPackage(translators, packageName);
144        String translatedPackageName = packageName;
145        if (translator != null) {
146                translate = true;
147                translatedPackageName = translator.translate(packageName);
148        }
149        
150        if (!translate)
151                return this;
152        
153        return new As3Type(translatedPackageName, getName());
154    }
155
156    ///////////////////////////////////////////////////////////////////////////
157    // Utilities.
158
159    @Override
160    public int hashCode() {
161        return qualifiedName.hashCode();
162    }
163
164    @Override
165    public boolean equals(Object obj) {
166        if (obj == this)
167            return true;
168        if (!(obj instanceof As3Type))
169            return false;
170        return qualifiedName.equals(((As3Type)obj).qualifiedName);
171    }
172
173    @Override
174    public String toString() {
175        return qualifiedName;
176    }
177}