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