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.checks.profiles;
021    
022    import org.sonar.api.BatchExtension;
023    import org.sonar.api.ServerExtension;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.List;
028    
029    public class CheckProfile implements BatchExtension, ServerExtension {
030    
031      private String name;
032      private String language;
033      private List<Check> checks = new ArrayList<Check>();
034    
035      public CheckProfile(String name, String language) {
036        if (name == null) {
037          throw new IllegalArgumentException("Name can not be null");
038        }
039        if (language == null) {
040          throw new IllegalArgumentException("Language can not be null");
041        }
042        this.name = name;
043        this.language = language;
044      }
045    
046      public String getName() {
047        return name;
048      }
049    
050      public String getLanguage() {
051        return language;
052      }
053    
054      public List<Check> getChecks() {
055        return checks;
056      }
057    
058      public List<Check> getChecks(String repositoryKey) {
059        List<Check> result = new ArrayList<Check>();
060        for (Check check : getChecks()) {
061          if (check.getRepositoryKey().equals(repositoryKey)) {
062            result.add(check);
063          }
064        }
065        return result;
066      }
067    
068      public List<Check> getChecks(String repositoryKey, String templateKey) {
069        List<Check> result = new ArrayList<Check>();
070        List<Check> repoChecks = getChecks(repositoryKey);
071        for (Check repoCheck : repoChecks) {
072          if (repoCheck.getTemplateKey().equals(templateKey)) {
073            result.add(repoCheck);
074          }
075        }
076        return result;
077      }
078    
079      /**
080       * We assume there is only one check for this template
081       */
082      public Check getCheck(String repositoryKey, String templateKey) {
083        List<Check> repoChecks = getChecks(repositoryKey);
084        for (Check repoCheck : repoChecks) {
085          if (repoCheck.getTemplateKey().equals(templateKey)) {
086            return repoCheck;
087          }
088        }
089        return null;
090      }
091    
092      public void addCheck(Check check) {
093        checks.add(check);
094      }
095    
096      public void setChecks(Collection<Check> list) {
097        checks.clear();
098        checks.addAll(list);
099      }
100    
101      @Override
102      public boolean equals(Object o) {
103        if (this == o) {
104          return true;
105        }
106        if (o == null || getClass() != o.getClass()) {
107          return false;
108        }
109    
110        CheckProfile profile = (CheckProfile) o;
111        if (!language.equals(profile.language)) {
112          return false;
113        }
114        if (!name.equals(profile.name)) {
115          return false;
116        }
117        return true;
118      }
119    
120      @Override
121      public int hashCode() {
122        int result = name.hashCode();
123        result = 31 * result + language.hashCode();
124        return result;
125      }
126    
127      @Override
128      public String toString() {
129        return name;
130      }
131    }