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.api.resources; 021 022 import com.google.common.annotations.Beta; 023 import com.google.common.base.Preconditions; 024 import com.google.common.base.Strings; 025 026 import javax.annotation.Nullable; 027 import javax.annotation.concurrent.Immutable; 028 029 /** 030 * Experimental extension to declare types of resources. 031 * 032 * @since 2.14 033 */ 034 @Beta 035 @Immutable 036 public final class ResourceType { 037 038 public static class Builder { 039 private String qualifier; 040 private String iconPath; 041 private boolean availableForFilters = false; 042 private boolean hasSourceCode = false; 043 044 public Builder(String qualifier) { 045 this.qualifier = qualifier; 046 } 047 048 /** 049 * @param iconPath path to icon, relative to context of web-application (e.g. "/images/q/DIR.png") 050 */ 051 public Builder setIconPath(@Nullable String iconPath) { 052 this.iconPath = iconPath; 053 return this; 054 } 055 056 public Builder availableForFilters() { 057 this.availableForFilters = true; 058 return this; 059 } 060 061 public Builder hasSourceCode() { 062 this.hasSourceCode = true; 063 return this; 064 } 065 066 public ResourceType build() { 067 if (Strings.isNullOrEmpty(iconPath)) { 068 iconPath = "/images/q/" + qualifier + ".png"; 069 } 070 return new ResourceType(this); 071 } 072 } 073 074 public static Builder builder(String qualifier) { 075 Preconditions.checkNotNull(qualifier); 076 Preconditions.checkArgument(qualifier.length() <= 10, "Qualifier is limited to 10 characters"); 077 return new Builder(qualifier); 078 } 079 080 private final String qualifier; 081 private final String iconPath; 082 private final boolean hasSourceCode; 083 private final boolean availableForFilters; 084 085 private ResourceType(Builder builder) { 086 this.qualifier = builder.qualifier; 087 this.iconPath = builder.iconPath; 088 this.availableForFilters = builder.availableForFilters; 089 this.hasSourceCode = builder.hasSourceCode; 090 } 091 092 /** 093 * Qualifier is the unique key 094 */ 095 public String getQualifier() { 096 return qualifier; 097 } 098 099 public String getIconPath() { 100 return iconPath; 101 } 102 103 public boolean isAvailableForFilters() { 104 return availableForFilters; 105 } 106 107 public boolean hasSourceCode() { 108 return hasSourceCode; 109 } 110 111 @Override 112 public boolean equals(Object o) { 113 if (this == o) { 114 return true; 115 } 116 if (o == null || getClass() != o.getClass()) { 117 return false; 118 } 119 120 ResourceType that = (ResourceType) o; 121 return qualifier.equals(that.qualifier); 122 } 123 124 @Override 125 public int hashCode() { 126 return qualifier.hashCode(); 127 } 128 }