001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.java.api;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.resources.*;
024    
025    /**
026     * @since 2.6
027     * @deprecated in 4.2. Only file system is handled by SonarQube, not logical components.
028     */
029    @Deprecated
030    public final class JavaClass extends Resource {
031    
032      public static final String SCOPE = Scopes.PROGRAM_UNIT;
033      public static final String QUALIFIER = Qualifiers.CLASS;
034      public static final int UNKNOWN_LINE = -1;
035    
036      private int fromLine = UNKNOWN_LINE;
037      private int toLine = UNKNOWN_LINE;
038    
039      private JavaClass(String name) {
040        setKey(name);
041      }
042    
043      private JavaClass(String name, int fromLine, int toLine) {
044        setKey(name);
045        this.fromLine = fromLine;
046        this.toLine = toLine;
047      }
048    
049      public String getPackageName() {
050        if (StringUtils.contains(getKey(), JavaUtils.PACKAGE_SEPARATOR)) {
051          return StringUtils.substringBeforeLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
052        }
053        return "";
054      }
055    
056      public String getClassName() {
057        String className = StringUtils.substringAfterLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
058        return StringUtils.defaultIfEmpty(className, getKey());
059      }
060    
061      public int getFromLine() {
062        return fromLine;
063      }
064    
065      public int getToLine() {
066        return toLine;
067      }
068    
069      @Override
070      public String getName() {
071        return getKey();
072      }
073    
074      @Override
075      public String getLongName() {
076        return getKey();
077      }
078    
079      @Override
080      public String getDescription() {
081        return null;
082      }
083    
084      @Override
085      public Language getLanguage() {
086        return Java.INSTANCE;
087      }
088    
089      @Override
090      public String getScope() {
091        return SCOPE;
092      }
093    
094      @Override
095      public String getQualifier() {
096        return QUALIFIER;
097      }
098    
099      @Override
100      public Resource getParent() {
101        return null;
102      }
103    
104      @Override
105      public boolean matchFilePattern(String antPattern) {
106        return false;
107      }
108    
109      @Override
110      public String toString() {
111        return getName();
112      }
113    
114      public static JavaClass create(String name) {
115        return new JavaClass(name);
116      }
117    
118      public static JavaClass create(String packageName, String className) {
119        if (StringUtils.isBlank(packageName)) {
120          return new JavaClass(className);
121        }
122        return new JavaClass(toName(packageName, className));
123      }
124    
125      private static String toName(String packageName, String className) {
126        if (StringUtils.isBlank(packageName)) {
127          return className;
128        }
129        return new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString();
130      }
131    
132      public static class Builder {
133        private String name;
134        private int fromLine = UNKNOWN_LINE;
135        private int toLine = UNKNOWN_LINE;
136    
137        public Builder setName(String name) {
138          this.name = name;
139          return this;
140        }
141    
142        public Builder setName(String packageName, String className) {
143          this.name = toName(packageName, className);
144          return this;
145        }
146    
147        public Builder setFromLine(int fromLine) {
148          this.fromLine = Math.max(UNKNOWN_LINE, fromLine);
149          return this;
150        }
151    
152        public Builder setToLine(int toLine) {
153          this.toLine = Math.max(UNKNOWN_LINE, toLine);
154          return this;
155        }
156    
157        public JavaClass create() {
158          return new JavaClass(name, fromLine, toLine);
159        }
160      }
161    }