aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/batch/TimeMachineQuery.java
blob: 3b90c5ecc49e275d61b4caf4809e184e46b2be0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2012 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.api.batch;

import com.google.common.collect.Lists;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Resource;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * A class to query TimeMachine on a given resource
 * <p/>
 * <p>The query is constructed by setting filters on metrics and on dates</p>
 * <p>It is to be noted that all filters will be applied regardless of their coherence</p>
 *
 * @since 1.10
 */
public class TimeMachineQuery {

  private Resource resource;
  private List<Metric> metrics;
  private List<String> metricKeys;
  private Date from;
  private Date to;
  private boolean onlyLastAnalysis = false;
  private boolean fromCurrentAnalysis = false;
  private boolean toCurrentAnalysis = false;

  /**
   * <p>Create a TimeMachine query for a given resource
   * </p>
   * Apart from the resource the query is empty, i.e. will return all data for the resource
   *
   * @param resource the resource
   */
  public TimeMachineQuery(Resource resource) {
    this.resource = resource;
  }

  /**
   * @return the resource of the Query
   */
  public Resource getResource() {
    return resource;
  }

  /**
   * Sets the resource of the query
   *
   * @param resource the resource
   * @return this
   */
  public TimeMachineQuery setResource(Resource resource) {
    this.resource = resource;
    return this;
  }

  /**
   * @return the metrics beloging to the query
   */
  public List<Metric> getMetrics() {
    return metrics;
  }

  /**
   * Sets the metrics to return
   *
   * @param metrics the list of metrics
   * @return this
   */
  public TimeMachineQuery setMetrics(List<Metric> metrics) {
    this.metrics = metrics;
    this.metricKeys = Lists.newLinkedList();
    for (Metric metric : this.metrics) {
      this.metricKeys.add(metric.getKey());
    }
    return this;
  }

  public TimeMachineQuery setMetricKeys(String... metricKeys) {
    this.metricKeys = Arrays.asList(metricKeys);
    return this;
  }

  public List<String> getMetricKeys() {
    return metricKeys;
  }

  public TimeMachineQuery setMetricKeys(List<String> metricKeys) {
    this.metricKeys = metricKeys;
    return this;
  }

  /**
   * Sets the metrics to return
   *
   * @param metrics the list of metrics
   * @return this
   */
  public TimeMachineQuery setMetrics(Metric... metrics) {
    this.metrics = Arrays.asList(metrics);
    this.metricKeys = Lists.newLinkedList();
    for (Metric metric : this.metrics) {
      this.metricKeys.add(metric.getKey());
    }
    return this;
  }

  /**
   * Unsets the metrics
   *
   * @return this
   */
  public TimeMachineQuery unsetMetrics() {
    this.metrics = null;
    return this;
  }

  /**
   * @return the from date of the query
   */
  public Date getFrom() {
    return from;
  }

  /**
   * Sets the from date to be used in the query
   *
   * @param from the from date
   * @return this
   */
  public TimeMachineQuery setFrom(Date from) {
    this.from = from;
    return this;
  }

  /**
   * @param b whether to use the latest analysis as a from date
   * @return this
   */
  public TimeMachineQuery setFromCurrentAnalysis(boolean b) {
    this.fromCurrentAnalysis = b;
    return this;
  }

  /**
   * @param b whether to use the latest analysis as a to date
   * @return this
   */
  public TimeMachineQuery setToCurrentAnalysis(boolean b) {
    this.toCurrentAnalysis = b;
    return this;
  }

  /**
   * @return whether the latest analysis is used as a from date
   */
  public boolean isFromCurrentAnalysis() {
    return fromCurrentAnalysis;
  }

  /**
   * @return whether the latest analysis is used as a to date
   */
  public boolean isToCurrentAnalysis() {
    return toCurrentAnalysis;
  }

  /**
   * @return the to date of the query
   */
  public Date getTo() {
    return to;
  }

  /**
   * Sets the to date to be used in the query
   *
   * @param to the to date
   * @return this
   */
  public TimeMachineQuery setTo(Date to) {
    this.to = to;
    return this;
  }

  /**
   * @return whether to return only the latest analysis
   */
  public boolean isOnlyLastAnalysis() {
    return onlyLastAnalysis;
  }

  /**
   *
   * @param onlyLastAnalysis whether to only return the latest analysis
   * @return this
   */
  public TimeMachineQuery setOnlyLastAnalysis(boolean onlyLastAnalysis) {
    this.onlyLastAnalysis = onlyLastAnalysis;
    return this;
  }

  @Override
  public String toString() {
    return new ToStringBuilder(this)
        .append("resource", resource)
        .append("metrics", metrics)
        .append("from", from)
        .append("to", to)
        .toString();
  }
}