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