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    /**
024     * @author Franck WOLFF
025     */
026    public class PackageTranslator {
027    
028        private String java = null;
029        private String as3 = null;
030        private int weight = -1;
031    
032        public PackageTranslator() {
033        }
034    
035        public PackageTranslator(String java, String as3) {
036            setJava(java);
037            setAs3(as3);
038        }
039    
040        public String getJava() {
041            return java;
042        }
043        public void setJava(String java) {
044            this.java = java;
045            this.weight = (java != null ? java.split("\\Q.\\E", -1).length : -1);
046        }
047    
048        public String getAs3() {
049            return as3;
050        }
051        public void setAs3(String as3) {
052            this.as3 = as3;
053        }
054    
055        public int getWeight() {
056            return weight;
057        }
058    
059        public boolean isValid() {
060            return java != null && java.length() > 0 && as3 != null && as3.length() > 0;
061        }
062    
063        public int match(String pkg) {
064            if (!pkg.startsWith(java))
065                return 0;
066            if (pkg.equals(java))
067                return Integer.MAX_VALUE;
068            return weight;
069        }
070    
071        public String translate(String pkg) {
072            return as3 + pkg.substring(java.length());
073        }
074    
075        @Override
076        public int hashCode() {
077            return (java != null ? java.hashCode() : 0);
078        }
079    
080        @Override
081        public boolean equals(Object o) {
082            if (o == this)
083                return true;
084            if (!(o instanceof PackageTranslator))
085                return false;
086            return (
087                java == null ?
088                ((PackageTranslator)o).java == null :
089                java.equals(((PackageTranslator)o).java)
090            );
091        }
092    
093        @Override
094        public String toString() {
095            return getClass().getName() + "{java=\"" + java + "\", as=\"" + as3 + "\"}";
096        }
097    }