You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeasureValueFormatter.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.measure.ws;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.measures.Metric;
  24. import org.sonar.db.measure.LiveMeasureDto;
  25. import org.sonar.db.measure.MeasureDto;
  26. import org.sonar.db.metric.MetricDto;
  27. public class MeasureValueFormatter {
  28. private static final double DELTA = 0.000001D;
  29. private MeasureValueFormatter() {
  30. // static methods
  31. }
  32. @CheckForNull
  33. public static String formatMeasureValue(LiveMeasureDto measure, MetricDto metric) {
  34. Double doubleValue = measure.getValue();
  35. String stringValue = measure.getDataAsString();
  36. return formatMeasureValue(doubleValue == null ? Double.NaN : doubleValue, stringValue, metric);
  37. }
  38. @CheckForNull
  39. static String formatMeasureValue(MeasureDto measure, MetricDto metric) {
  40. Double doubleValue = measure.getValue();
  41. String stringValue = measure.getData();
  42. return formatMeasureValue(doubleValue == null ? Double.NaN : doubleValue, stringValue, metric);
  43. }
  44. @CheckForNull
  45. static String formatMeasureValue(double doubleValue, @Nullable String stringValue, MetricDto metric) {
  46. Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType());
  47. switch (metricType) {
  48. case BOOL:
  49. return formatBoolean(doubleValue);
  50. case INT:
  51. return formatInteger(doubleValue);
  52. case MILLISEC:
  53. case WORK_DUR:
  54. return formatLong(doubleValue);
  55. case FLOAT:
  56. case PERCENT:
  57. case RATING:
  58. return String.valueOf(doubleValue);
  59. case LEVEL:
  60. case STRING:
  61. case DATA:
  62. case DISTRIB:
  63. return stringValue;
  64. default:
  65. throw new IllegalArgumentException("Unsupported metric type: " + metricType.name());
  66. }
  67. }
  68. static String formatNumericalValue(double value, MetricDto metric) {
  69. Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType());
  70. switch (metricType) {
  71. case BOOL:
  72. return formatBoolean(value);
  73. case INT:
  74. return formatInteger(value);
  75. case MILLISEC:
  76. case WORK_DUR:
  77. return formatLong(value);
  78. case FLOAT:
  79. case PERCENT:
  80. case RATING:
  81. return String.valueOf(value);
  82. case LEVEL:
  83. case STRING:
  84. case DATA:
  85. case DISTRIB:
  86. default:
  87. throw new IllegalArgumentException(String.format("Unsupported metric type '%s' for numerical value", metricType.name()));
  88. }
  89. }
  90. private static String formatBoolean(double value) {
  91. return Math.abs(value - 1.0D) < DELTA ? "true" : "false";
  92. }
  93. private static String formatInteger(double value) {
  94. return String.valueOf((int) value);
  95. }
  96. private static String formatLong(double value) {
  97. return String.valueOf((long) value);
  98. }
  99. }