001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    /**
021     * @author Carlos Sierra Andrés
022     * @author Hugo Huijser
023     */
024    public class ImportPackage implements Comparable<ImportPackage> {
025    
026            public ImportPackage(String importString, boolean isStatic, String line) {
027                    this(importString, isStatic, line, false);
028            }
029    
030            public ImportPackage(
031                    String importString, boolean isStatic, String line, boolean bndImport) {
032    
033                    _importString = importString;
034                    _isStatic = isStatic;
035                    _line = line;
036                    _bndImport = bndImport;
037            }
038    
039            @Override
040            public int compareTo(ImportPackage importPackage) {
041                    if (_isStatic != importPackage.isStatic()) {
042                            if (_isStatic) {
043                                    return -1;
044                            }
045                            else {
046                                    return 1;
047                            }
048                    }
049    
050                    String importPackageImportString = importPackage.getImportString();
051    
052                    int value = _importString.compareTo(importPackageImportString);
053    
054                    if (_importString.startsWith(StringPool.EXCLAMATION) ||
055                            importPackageImportString.startsWith(StringPool.EXCLAMATION)) {
056    
057                            return value;
058                    }
059    
060                    if (!_bndImport) {
061                            return value;
062                    }
063    
064                    int startsWithWeight = StringUtil.startsWithWeight(
065                            _importString, importPackageImportString);
066    
067                    String importStringPart1 = _importString.substring(startsWithWeight);
068                    String importStringPart2 = importPackageImportString.substring(
069                            startsWithWeight);
070    
071                    if (importStringPart1.equals(StringPool.STAR) ||
072                            importStringPart2.equals(StringPool.STAR)) {
073    
074                            return -value;
075                    }
076    
077                    return value;
078            }
079    
080            @Override
081            public boolean equals(Object obj) {
082                    if (this == obj) {
083                            return true;
084                    }
085    
086                    if (!(obj instanceof ImportPackage)) {
087                            return false;
088                    }
089    
090                    ImportPackage importPackage = (ImportPackage)obj;
091    
092                    if ((_isStatic == importPackage.isStatic()) &&
093                            _importString.equals(importPackage.getImportString())) {
094    
095                            return true;
096                    }
097    
098                    return false;
099            }
100    
101            public String getImportString() {
102                    return _importString;
103            }
104    
105            public String getLine() {
106                    return _line;
107            }
108    
109            public String getPackageLevel() {
110                    int pos = _importString.indexOf(StringPool.SLASH);
111    
112                    if (pos != -1) {
113                            pos = _importString.indexOf(StringPool.SLASH, pos + 1);
114    
115                            if (pos == -1) {
116                                    return _importString;
117                            }
118    
119                            return _importString.substring(0, pos);
120                    }
121    
122                    pos = _importString.indexOf(StringPool.PERIOD);
123    
124                    pos = _importString.indexOf(StringPool.PERIOD, pos + 1);
125    
126                    if ((pos == -1) && !_bndImport) {
127                            pos = _importString.indexOf(StringPool.PERIOD);
128                    }
129    
130                    if (pos == -1) {
131                            return _importString;
132                    }
133    
134                    return _importString.substring(0, pos);
135            }
136    
137            @Override
138            public int hashCode() {
139                    return _importString.hashCode();
140            }
141    
142            public boolean isGroupedWith(ImportPackage importPackage) {
143                    if (_importString.equals(StringPool.STAR)) {
144                            return true;
145                    }
146    
147                    String importPackageImportString = importPackage.getImportString();
148    
149                    if (importPackageImportString.equals(StringPool.STAR)) {
150                            return true;
151                    }
152    
153                    if (_isStatic != importPackage.isStatic()) {
154                            return false;
155                    }
156    
157                    String packageLevel = getPackageLevel();
158    
159                    if (packageLevel.equals(importPackage.getPackageLevel())) {
160                            return true;
161                    }
162    
163                    return false;
164            }
165    
166            public boolean isStatic() {
167                    return _isStatic;
168            }
169    
170            private final boolean _bndImport;
171            private final String _importString;
172            private boolean _isStatic;
173            private final String _line;
174    
175    }