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.batch;
021    
022    import org.apache.commons.lang.builder.ToStringBuilder;
023    import org.sonar.api.measures.Metric;
024    import org.sonar.api.resources.Resource;
025    
026    import java.util.Arrays;
027    import java.util.Date;
028    import java.util.List;
029    
030    /**
031     * A class to query TimeMachine on a given resource
032     * <p/>
033     * <p>The query is constructed by setting filters on metrics and on dates</p>
034     * <p>It is to be noted that all filters will be applied regardless of their coherence</p>
035     *
036     * @since 1.10
037     */
038    public class TimeMachineQuery {
039    
040      private Resource resource;
041      private List<Metric> metrics;
042      private Date from;
043      private Date to;
044      private boolean onlyLastAnalysis = false;
045      private boolean fromCurrentAnalysis = false;
046      private boolean toCurrentAnalysis = false;
047    
048      /**
049       * <p>Create a TimeMachine query for a given resource
050       * </p>
051       * Apart from the resource the query is empty, i.e. will return all data for the resource
052       *
053       * @param resource the resource
054       */
055      public TimeMachineQuery(Resource resource) {
056        this.resource = resource;
057      }
058    
059      /**
060       * @return the resource of the Query
061       */
062      public Resource getResource() {
063        return resource;
064      }
065    
066      /**
067       * Sets the resource of the query
068       *
069       * @param resource the resource
070       * @return this
071       */
072      public TimeMachineQuery setResource(Resource resource) {
073        this.resource = resource;
074        return this;
075      }
076    
077      /**
078       * @return the metrics beloging to the query
079       */
080      public List<Metric> getMetrics() {
081        return metrics;
082      }
083    
084      /**
085       * Sets the metrics to return
086       *
087       * @param metrics the list of metrics
088       * @return this
089       */
090      public TimeMachineQuery setMetrics(List<Metric> metrics) {
091        this.metrics = metrics;
092        return this;
093      }
094    
095      /**
096       * Sets the metrics to return
097       *
098       * @param metrics the list of metrics
099       * @return this
100       */
101      public TimeMachineQuery setMetrics(Metric... metrics) {
102        this.metrics = Arrays.asList(metrics);
103        return this;
104      }
105    
106      /**
107       * Unsets the metrics
108       *
109       * @return this
110       */
111      public TimeMachineQuery unsetMetrics() {
112        this.metrics = null;
113        return this;
114      }
115    
116      /**
117       * @return the from date of the query
118       */
119      public Date getFrom() {
120        return from;
121      }
122    
123      /**
124       * Sets the from date to be used in the query
125       *
126       * @param from the from date
127       * @return this
128       */
129      public TimeMachineQuery setFrom(Date from) {
130        this.from = from;
131        return this;
132      }
133    
134      /**
135       * @param b whether to use the latest analysis as a from date
136       * @return this
137       */
138      public TimeMachineQuery setFromCurrentAnalysis(boolean b) {
139        this.fromCurrentAnalysis = b;
140        return this;
141      }
142    
143      /**
144       * @param b whether to use the latest analysis as a to date
145       * @return this
146       */
147      public TimeMachineQuery setToCurrentAnalysis(boolean b) {
148        this.toCurrentAnalysis = b;
149        return this;
150      }
151    
152      /**
153       * @return whether the latest analysis is used as a from date
154       */
155      public boolean isFromCurrentAnalysis() {
156        return fromCurrentAnalysis;
157      }
158    
159      /**
160       * @return whether the latest analysis is used as a to date
161       */
162      public boolean isToCurrentAnalysis() {
163        return toCurrentAnalysis;
164      }
165    
166      /**
167       * @return the to date of the query
168       */
169      public Date getTo() {
170        return to;
171      }
172    
173      /**
174       * Sets the to date to be used in the query
175       *
176       * @param to the to date
177       * @return this
178       */
179      public TimeMachineQuery setTo(Date to) {
180        this.to = to;
181        return this;
182      }
183    
184      /**
185       * @return whether to return only the latest analysis
186       */
187      public boolean isOnlyLastAnalysis() {
188        return onlyLastAnalysis;
189      }
190    
191      /**
192       *
193       * @param onlyLastAnalysis whether to only return the latest analysis
194       * @return this
195       */
196      public TimeMachineQuery setOnlyLastAnalysis(boolean onlyLastAnalysis) {
197        this.onlyLastAnalysis = onlyLastAnalysis;
198        return this;
199      }
200    
201      @Override
202      public String toString() {
203        return new ToStringBuilder(this)
204            .append("resource", resource)
205            .append("metrics", metrics)
206            .append("from", from)
207            .append("to", to)
208            .toString();
209      }
210    }