001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
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 */
028 public final class JavaClass extends Resource {
029
030 public static final String SCOPE = Scopes.PROGRAM_UNIT;
031 public static final String QUALIFIER = Qualifiers.CLASS;
032 public static final int UNKNOWN_LINE = -1;
033
034 private int fromLine = UNKNOWN_LINE;
035 private int toLine = UNKNOWN_LINE;
036
037 private JavaClass(String name) {
038 setKey(name);
039 }
040
041 private JavaClass(String name, int fromLine, int toLine) {
042 setKey(name);
043 this.fromLine = fromLine;
044 this.toLine = toLine;
045 }
046
047 public String getPackageName() {
048 if (StringUtils.contains(getKey(), JavaUtils.PACKAGE_SEPARATOR)) {
049 return StringUtils.substringBeforeLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
050 }
051 return "";
052 }
053
054 public String getClassName() {
055 String className = StringUtils.substringAfterLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
056 return StringUtils.defaultIfEmpty(className, getKey());
057 }
058
059 public int getFromLine() {
060 return fromLine;
061 }
062
063 public int getToLine() {
064 return toLine;
065 }
066
067 @Override
068 public String getName() {
069 return getKey();
070 }
071
072 @Override
073 public String getLongName() {
074 return getKey();
075 }
076
077 @Override
078 public String getDescription() {
079 return null;
080 }
081
082 @Override
083 public Language getLanguage() {
084 return Java.INSTANCE;
085 }
086
087 @Override
088 public String getScope() {
089 return SCOPE;
090 }
091
092 @Override
093 public String getQualifier() {
094 return QUALIFIER;
095 }
096
097 @Override
098 public Resource getParent() {
099 return null;
100 }
101
102 @Override
103 public boolean matchFilePattern(String antPattern) {
104 return false;
105 }
106
107 @Override
108 public String toString() {
109 return getName();
110 }
111
112 public static JavaClass create(String name) {
113 return new JavaClass(name);
114 }
115
116 public static JavaClass create(String packageName, String className) {
117 if (StringUtils.isBlank(packageName)) {
118 return new JavaClass(className);
119 }
120 return new JavaClass(toName(packageName, className));
121 }
122
123 private static String toName(String packageName, String className) {
124 if (StringUtils.isBlank(packageName)) {
125 return className;
126 }
127 return new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString();
128 }
129
130 public static class Builder {
131 private String name;
132 private int fromLine = UNKNOWN_LINE;
133 private int toLine = UNKNOWN_LINE;
134
135 public Builder setName(String name) {
136 this.name = name;
137 return this;
138 }
139
140 public Builder setName(String packageName, String className) {
141 this.name = toName(packageName, className);
142 return this;
143 }
144
145 public Builder setFromLine(int fromLine) {
146 this.fromLine = Math.max(UNKNOWN_LINE, fromLine);
147 return this;
148 }
149
150 public Builder setToLine(int toLine) {
151 this.toLine = Math.max(UNKNOWN_LINE, toLine);
152 return this;
153 }
154
155 public JavaClass create() {
156 return new JavaClass(name, fromLine, toLine);
157 }
158 }
159 }