001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 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 */
020package org.sonar.wsclient.issue.internal;
021
022import org.sonar.wsclient.base.Paging;
023import org.sonar.wsclient.component.Component;
024import org.sonar.wsclient.issue.ActionPlan;
025import org.sonar.wsclient.issue.Issue;
026import org.sonar.wsclient.issue.Issues;
027import org.sonar.wsclient.rule.Rule;
028import org.sonar.wsclient.user.User;
029
030import javax.annotation.CheckForNull;
031import java.util.ArrayList;
032import java.util.Collection;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * @since 3.6
039 */
040public class DefaultIssues implements Issues {
041
042  private final List<Issue> list = new ArrayList<Issue>();
043  private final Map<String, Rule> rulesByKey = new HashMap<String, Rule>();
044  private final Map<String, User> usersByKey = new HashMap<String, User>();
045  private final Map<Long, Component> componentsById = new HashMap<Long, Component>();
046  private final Map<String, Component> componentsByKey = new HashMap<String, Component>();
047  private final Map<String, Component> projectsByKey = new HashMap<String, Component>();
048  private final Map<String, ActionPlan> actionPlansByKey = new HashMap<String, ActionPlan>();
049  private Paging paging;
050
051  @Override
052  public List<Issue> list() {
053    return list;
054  }
055
056  @Override
057  public int size() {
058    return list.size();
059  }
060
061  @Override
062  public Collection<Rule> rules() {
063    return rulesByKey.values();
064  }
065
066  @Override
067  public Rule rule(Issue issue) {
068    return rulesByKey.get(issue.ruleKey());
069  }
070
071  @Override
072  public Collection<User> users() {
073    return usersByKey.values();
074  }
075
076  @Override
077  @CheckForNull
078  public User user(String login) {
079    return usersByKey.get(login);
080  }
081
082  @Override
083  public Collection<Component> components() {
084    return componentsByKey.values();
085  }
086
087  @Override
088  @CheckForNull
089  public Component component(Issue issue) {
090    return componentsByKey.get(issue.componentKey());
091  }
092
093  @Override
094  @CheckForNull
095  public Component componentById(long id) {
096    return componentsById.get(id);
097  }
098
099  @Override
100  @CheckForNull
101  public Component componentByKey(String key) {
102    return componentsByKey.get(key);
103  }
104
105  @Override
106  public Collection<Component> projects() {
107    return projectsByKey.values();
108  }
109
110  @Override
111  @CheckForNull
112  public Component project(Issue issue) {
113    return projectsByKey.get(issue.projectKey());
114  }
115
116  @Override
117  public Collection<ActionPlan> actionPlans() {
118    return actionPlansByKey.values();
119  }
120
121  @Override
122  @CheckForNull
123  public ActionPlan actionPlans(Issue issue) {
124    return actionPlansByKey.get(issue.actionPlan());
125  }
126
127  @Override
128  public Paging paging() {
129    return paging;
130  }
131
132  DefaultIssues add(Issue issue) {
133    list.add(issue);
134    return this;
135  }
136
137  DefaultIssues add(Rule rule) {
138    rulesByKey.put(rule.key(), rule);
139    return this;
140  }
141
142  DefaultIssues add(User user) {
143    usersByKey.put(user.login(), user);
144    return this;
145  }
146
147  DefaultIssues add(ActionPlan actionPlan) {
148    actionPlansByKey.put(actionPlan.key(), actionPlan);
149    return this;
150  }
151
152  DefaultIssues addComponent(Component c) {
153    componentsById.put(c.id(), c);
154    componentsByKey.put(c.key(), c);
155    return this;
156  }
157
158  DefaultIssues addProject(Component c) {
159    projectsByKey.put(c.key(), c);
160    return this;
161  }
162
163  DefaultIssues setPaging(Paging paging) {
164    this.paging = paging;
165    return this;
166  }
167}