001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
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.api.resources;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.apache.commons.lang.builder.ToStringBuilder;
024 import org.sonar.api.utils.WildcardPattern;
025
026 /**
027 * A class that represents a Java package in Sonar
028 *
029 * @since 1.10
030 */
031 public class JavaPackage implements Resource {
032
033 /**
034 * Default package name for classes without package definition
035 */
036 public static final String DEFAULT_PACKAGE_NAME = "[default]";
037
038 private String key;
039
040 /**
041 * Defaul constructor
042 */
043 public JavaPackage() {
044 this(null);
045 }
046
047 /**
048 * Creates a JavaPackage from its key. Will use DEFAULT_PACKAGE_NAME if key is null
049 */
050 public JavaPackage(String key) {
051 this.key = StringUtils.defaultIfEmpty(StringUtils.trim(key), DEFAULT_PACKAGE_NAME);
052 }
053
054 /**
055 * @return whether the JavaPackage key is the defult key
056 */
057 public boolean isDefault() {
058 return StringUtils.equals(getKey(), DEFAULT_PACKAGE_NAME);
059 }
060
061 /**
062 * {@inheritDoc}
063 */
064 public boolean matchFilePattern(String antPattern) {
065 String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, ".");
066 WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, ".");
067 return matcher.match(getKey());
068 }
069
070 /**
071 * {@inheritDoc}
072 */
073 public String getKey() {
074 return key;
075 }
076
077 /**
078 * {@inheritDoc}
079 */
080 public String getDescription() {
081 return null;
082 }
083
084 /**
085 * @return SCOPE_SPACE
086 */
087 public String getScope() {
088 return Resource.SCOPE_SPACE;
089 }
090
091 /**
092 * @return QUALIFIER_PACKAGE
093 */
094 public String getQualifier() {
095 return Resource.QUALIFIER_PACKAGE;
096 }
097
098 /**
099 * {@inheritDoc}
100 */
101 public String getName() {
102 return key;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public Resource<?> getParent() {
109 return null;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public String getLongName() {
116 return null;
117 }
118
119 /**
120 * @return Java
121 */
122 public Language getLanguage() {
123 return Java.INSTANCE;
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (!(obj instanceof JavaPackage)) {
129 return false;
130 }
131 if (this == obj) {
132 return true;
133 }
134 JavaPackage other = (JavaPackage) obj;
135 return StringUtils.equals(key, other.getKey());
136 }
137
138 @Override
139 public int hashCode() {
140 return key.hashCode();
141 }
142
143 @Override
144 public String toString() {
145 return new ToStringBuilder(this)
146 .append("key", key)
147 .toString();
148 }
149 }