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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.measures;
import com.google.common.annotations.Beta;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.math.NumberUtils;
import org.sonar.api.technicaldebt.batch.Characteristic;
import org.sonar.api.technicaldebt.batch.Requirement;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
/**
* A class to handle measures.
*
* @since 1.10
*/
public class Measure<G extends Serializable> implements Serializable {
private static final String INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5 = "Index should be in range from 1 to 5";
protected static final int MAX_TEXT_SIZE = 96;
/**
* Default precision when saving a float type metric
*/
public static final int DEFAULT_PRECISION = 1;
protected String metricKey;
protected Metric<G> metric;
protected Double value;
protected String data;
protected String description;
protected Metric.Level alertStatus;
protected String alertText;
protected Integer tendency;
protected Date date;
protected Double variation1, variation2, variation3, variation4, variation5;
protected String url;
protected Characteristic characteristic;
protected Requirement requirement;
protected Integer personId;
protected PersistenceMode persistenceMode = PersistenceMode.FULL;
private boolean fromCore;
public Measure(String metricKey) {
this.metricKey = metricKey;
}
/**
* Creates a measure with a metric
*
* @param metric the metric
*/
public Measure(Metric metric) {
this.metric = metric;
this.metricKey = metric.getKey();
}
/**
* Creates a measure with a metric and a value
*
* @param metric the metric
* @param value its value
*/
public Measure(Metric metric, Double value) {
this.metric = metric;
this.metricKey = metric.getKey();
setValue(value);
}
/**
* Creates a measure with a metric, a value and a precision for the value
*
* @param metric the metric
* @param value its value
* @param precision the value precision
*/
public Measure(Metric metric, Double value, int precision) {
this.metric = metric;
this.metricKey = metric.getKey();
setValue(value, precision);
}
/**
* Creates a measure with a metric, a value and a data field
*
* @param metric the metric
* @param value the value
* @param data the data field
*/
public Measure(Metric metric, Double value, String data) {
this.metric = metric;
this.metricKey = metric.getKey();
setValue(value);
setData(data);
}
/**
* * Creates a measure with a metric and a data field
*
* @param metric the metric
* @param data the data field
*/
public Measure(Metric metric, String data) {
this.metric = metric;
this.metricKey = metric.getKey();
setData(data);
}
/**
* Creates a measure with a metric and an alert level
*
* @param metric the metric
* @param level the alert level
*/
public Measure(Metric metric, Metric.Level level) {
this.metric = metric;
this.metricKey = metric.getKey();
if (level != null) {
this.data = level.toString();
}
}
/**
* Creates an empty measure
*/
public Measure() {
}
/**
* Gets the persistence mode of the measure. Default persistence mode is FULL, except when instantiating the measure with a String
* parameter.
*/
public PersistenceMode getPersistenceMode() {
return persistenceMode;
}
/**
* <p>
* Sets the persistence mode of a measure.
* </p>
* <p>
* <b>WARNING : </b>Being able to reuse measures saved in memory is only possible within the same tree. In a multi-module project for
* example, a measure save in memory at the module level will not be accessible by the root project. In that case, database should be
* used.
* </p>
*
* @param mode the mode
* @return the measure object instance
*/
public Measure setPersistenceMode(@Nullable PersistenceMode mode) {
if (mode == null) {
this.persistenceMode = PersistenceMode.FULL;
} else {
this.persistenceMode = mode;
}
return this;
}
/**
* @return return the measures underlying metric
*/
public Metric getMetric() {
return metric;
}
public String getMetricKey() {
return metricKey;
}
/**
* Set the underlying metric
*
* @param metric the metric
* @return the measure object instance
*/
public Measure setMetric(Metric metric) {
this.metric = metric;
this.metricKey = metric.getKey();
return this;
}
/**
* @return transforms and returns the data fields as a level of alert
*/
public Metric.Level getDataAsLevel() {
if (data != null) {
return Metric.Level.valueOf(data);
}
return null;
}
public boolean hasData() {
return data != null;
}
/**
* @return the date of the measure, i.e. the date the measure was taken. Used only in TimeMachine queries
*/
public Date getDate() {
return date;
}
/**
* Sets the date of the measure - Used only in TimeMachine queries
*
* @param date the date
* @return the measure object instance
*/
public Measure setDate(Date date) {
this.date = date;
return this;
}
/**
* @return the value of the measure as a double
*/
public Double getValue() {
return value;
}
/**
* For internal use.
*/
public G value() {
switch (getMetric().getType()) {
case BOOL:
return value == null ? null : (G) Boolean.valueOf(Double.doubleToRawLongBits(value) != 0L);
case INT:
case MILLISEC:
case RATING:
return value == null ? null : (G) Integer.valueOf(value.intValue());
case FLOAT:
case PERCENT:
return value == null ? null : (G) value;
case STRING:
case LEVEL:
case DATA:
case DISTRIB:
return data == null ? null : (G) data;
case WORK_DUR:
return value == null ? null : (G) Long.valueOf(value.longValue());
default:
if (getMetric().isNumericType()) {
return value == null ? null : (G) value;
} else if (getMetric().isDataType()) {
return data == null ? null : (G) data;
} else {
throw new UnsupportedOperationException("Unsupported type :" + getMetric().getType());
}
}
}
/**
* @return the value of the measure as an int
*/
public Integer getIntValue() {
if (value == null) {
return null;
}
return value.intValue();
}
/**
* Sets the measure value with the default precision of 1
*
* @param v the measure value
* @return the measure object instance
*/
public Measure setValue(@Nullable Double v) {
return setValue(v, DEFAULT_PRECISION);
}
/**
* For internal use
*/
public Measure setRawValue(@Nullable Double v) {
this.value = v;
return this;
}
/**
* Sets the measure value as an int
*
* @param i the value
* @return the measure object instance
*/
public Measure setIntValue(Integer i) {
if (i == null) {
this.value = null;
} else {
this.value = Double.valueOf(i);
}
return this;
}
/**
* Sets the measure value with a given precision
*
* @param v the measure value
* @param precision the measure value precision
* @return the measure object instance
*/
public Measure setValue(@Nullable Double v, int precision) {
if (v != null) {
if (Double.isNaN(v)) {
throw new IllegalArgumentException("Measure value can not be NaN");
}
this.value = scaleValue(v, precision);
} else {
this.value = null;
}
return this;
}
private double scaleValue(double value, int scale) {
BigDecimal bd = BigDecimal.valueOf(value);
return bd.setScale(scale, RoundingMode.HALF_UP).doubleValue();
}
/**
* @return the data field of the measure
*/
@CheckForNull
public String getData() {
return data;
}
/**
* Sets the data field of the measure.
*
* @param s the data
* @return the measure object instance
*/
public Measure setData(String s) {
this.data = s;
return this;
}
/**
* Sets an alert level as the data field
*
* @param level the alert level
* @return the measure object instance
*/
public Measure setData(Metric.Level level) {
if (level == null) {
this.data = null;
} else {
this.data = level.toString();
}
return this;
}
/**
* @since 2.7
*/
public Measure unsetData() {
this.data = null;
return this;
}
/**
* @return the description of the measure
*/
public String getDescription() {
return description;
}
/**
* Sets the measure description
*
* @param description the description
* @return the measure object instance
*/
public Measure setDescription(String description) {
this.description = description;
return this;
}
/**
* @return the alert status of the measure
*/
public Metric.Level getAlertStatus() {
return alertStatus;
}
/**
* Set the alert status of the measure
*
* @param status the status
* @return the measure object instance
*/
public Measure setAlertStatus(@Nullable Metric.Level status) {
this.alertStatus = status;
return this;
}
/**
* @return the text associated to the alert on the measure
*/
public String getAlertText() {
return alertText;
}
/**
* Sets the text associated to the alert on the measure
*
* @param alertText the text
* @return the measure object instance
*/
public Measure setAlertText(@Nullable String alertText) {
this.alertText = alertText;
return this;
}
/**
* Gets the measure tendency
*
* @return the tendency
*/
public Integer getTendency() {
return tendency;
}
/**
* Sets the tendency for the measure - Internal use only
*
* @param tendency the tendency
* @return the measure object instance
*/
public Measure setTendency(@Nullable Integer tendency) {
this.tendency = tendency;
return this;
}
/**
* Called by views when cloning measures
* @deprecated since 4.4 not used
*/
@Deprecated
public Measure setId(Long id) {
return this;
}
/**
* @return the first variation value
* @since 2.5
*/
public Double getVariation1() {
return variation1;
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation1(@Nullable Double d) {
this.variation1 = d;
return this;
}
/**
* @return the second variation value
* @since 2.5
*/
public Double getVariation2() {
return variation2;
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation2(@Nullable Double d) {
this.variation2 = d;
return this;
}
/**
* @return the third variation value
* @since 2.5
*/
public Double getVariation3() {
return variation3;
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation3(@Nullable Double d) {
this.variation3 = d;
return this;
}
/**
* @return the third variation value
* @since 2.5
*/
public Double getVariation4() {
return variation4;
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation4(@Nullable Double d) {
this.variation4 = d;
return this;
}
/**
* @return the third variation value
* @since 2.5
*/
public Double getVariation5() {
return variation5;
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation5(@Nullable Double d) {
this.variation5 = d;
return this;
}
/**
* @since 2.5
*/
public Double getVariation(int index) {
switch (index) {
case 1:
return variation1;
case 2:
return variation2;
case 3:
return variation3;
case 4:
return variation4;
case 5:
return variation5;
default:
throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5);
}
}
/**
* Internal use only
*
* @since 2.5
*/
public Measure setVariation(int index, Double d) {
switch (index) {
case 1:
variation1 = d;
break;
case 2:
variation2 = d;
break;
case 3:
variation3 = d;
break;
case 4:
variation4 = d;
break;
case 5:
variation5 = d;
break;
default:
throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5);
}
return this;
}
/**
* @return the url of the measure
*/
public String getUrl() {
return url;
}
/**
* Sets the URL of the measure
*
* @param url the url
* @return the measure object instance
*/
public Measure setUrl(String url) {
this.url = url;
return this;
}
/**
* @since 4.1
*/
@CheckForNull
public final Characteristic getCharacteristic() {
return characteristic;
}
/**
* @since 4.1
*/
public final Measure setCharacteristic(@Nullable Characteristic characteristic) {
this.characteristic = characteristic;
return this;
}
/**
* @since 4.1
* @deprecated since 4.3.
*/
@Deprecated
@CheckForNull
public final Requirement getRequirement() {
return requirement;
}
/**
* @since 4.1
* @deprecated since 4.3
*/
@Deprecated
public final Measure setRequirement(@Nullable Requirement requirement) {
this.requirement = requirement;
return this;
}
/**
* @since 2.14
*/
@CheckForNull
@Beta
public Integer getPersonId() {
return personId;
}
/**
* @since 2.14
*/
@Beta
public Measure setPersonId(@Nullable Integer i) {
this.personId = i;
return this;
}
/**
* @since 3.2
*/
public boolean isBestValue() {
Double bestValue = metric.getBestValue();
return metric.isOptimizedBestValue() == Boolean.TRUE
&& bestValue != null
&& (value == null || NumberUtils.compare(bestValue, value) == 0)
&& allNull(alertStatus, description, tendency, url, data)
&& isZeroVariation(variation1, variation2, variation3, variation4, variation5);
}
/**
* For internal use
*/
public boolean isFromCore() {
return fromCore;
}
/**
* For internal use
*/
public void setFromCore(boolean fromCore) {
this.fromCore = fromCore;
}
private static boolean isZeroVariation(Double... variations) {
for (Double variation : variations) {
if (!((variation == null) || NumberUtils.compare(variation, 0.0) == 0)) {
return false;
}
}
return true;
}
private static boolean allNull(Object... values) {
for (Object value : values) {
if (null != value) {
return false;
}
}
return true;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Measure measure = (Measure) o;
if (metricKey != null ? !metricKey.equals(measure.metricKey) : measure.metricKey != null) {
return false;
}
if (characteristic != null ? !characteristic.equals(measure.characteristic) : measure.characteristic != null) {
return false;
}
if (personId != null ? !personId.equals(measure.personId) : measure.personId != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = metricKey != null ? metricKey.hashCode() : 0;
result = 31 * result + (characteristic != null ? characteristic.hashCode() : 0);
result = 31 * result + (personId != null ? personId.hashCode() : 0);
return result;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
|