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    
019    /**
020     * @author Carlos Sierra Andr??s
021     */
022    public class ImportPackage implements Comparable<ImportPackage> {
023    
024            public ImportPackage(String importString, boolean isStatic, String line) {
025                    _importString = importString;
026                    _isStatic = isStatic;
027                    _line = line;
028            }
029    
030            @Override
031            public int compareTo(ImportPackage importPackage) {
032                    if (_isStatic != importPackage.isStatic()) {
033                            if (_isStatic) {
034                                    return -1;
035                            }
036                            else {
037                                    return 1;
038                            }
039                    }
040    
041                    return _importString.compareTo(importPackage.getImportString());
042            }
043    
044            @Override
045            public boolean equals(Object obj) {
046                    if (this == obj) {
047                            return true;
048                    }
049    
050                    if (!(obj instanceof ImportPackage)) {
051                            return false;
052                    }
053    
054                    ImportPackage importPackage = (ImportPackage)obj;
055    
056                    if ((_isStatic == importPackage.isStatic()) &&
057                            _importString.equals(importPackage.getImportString())) {
058    
059                            return true;
060                    }
061    
062                    return false;
063            }
064    
065            public String getImportString() {
066                    return _importString;
067            }
068    
069            public String getLine() {
070                    return _line;
071            }
072    
073            public String getPackageLevel() {
074                    int pos = _importString.indexOf(StringPool.SLASH);
075    
076                    if (pos != -1) {
077                            pos = _importString.indexOf(StringPool.SLASH, pos + 1);
078    
079                            if (pos == -1) {
080                                    return _importString;
081                            }
082    
083                            return _importString.substring(0, pos);
084                    }
085    
086                    pos = _importString.indexOf(StringPool.PERIOD);
087    
088                    pos = _importString.indexOf(StringPool.PERIOD, pos + 1);
089    
090                    if (pos == -1) {
091                            pos = _importString.indexOf(StringPool.PERIOD);
092                    }
093    
094                    return _importString.substring(0, pos);
095            }
096    
097            @Override
098            public int hashCode() {
099                    return _importString.hashCode();
100            }
101    
102            public boolean isGroupedWith(ImportPackage importPackage) {
103                    if (_isStatic != importPackage.isStatic()) {
104                            return false;
105                    }
106    
107                    String packageLevel = getPackageLevel();
108    
109                    if (packageLevel.equals(importPackage.getPackageLevel())) {
110                            return true;
111                    }
112    
113                    return false;
114            }
115    
116            public boolean isStatic() {
117                    return _isStatic;
118            }
119    
120            private final String _importString;
121            private boolean _isStatic;
122            private final String _line;
123    
124    }