aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java
blob: febbf03ee9872dc51a70037027c4d5652b0300d3 (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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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.utils;

import java.util.Locale;
import javax.annotation.CheckForNull;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.config.Settings;
import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ServerSide;

/**
 * Used through ruby code <pre>Internal.durations</pre>
 *
 * @since 4.3
 */
@BatchSide
@ServerSide
@ComputeEngineSide
public class Durations {

  public enum DurationFormat {
    /**
     * Display duration with only one or two members.
     * For instance, Duration.decode("1d 1h 10min", 8) will return "1d 1h" and Duration.decode("12d 5h", 8) will return "12d"
     */
    SHORT
  }

  private final Settings settings;
  private final I18n i18n;

  public Durations(Settings settings, I18n i18n) {
    this.settings = settings;
    this.i18n = i18n;
  }

  /**
   * Create a Duration object from a number of minutes
   */
  public Duration create(long minutes) {
    return Duration.create(minutes);
  }

  /**
   * Convert the text to a Duration
   * <br>
   * Example : decode("9d 10 h") -&gt; Duration.encode("10d2h") (if sonar.technicalDebt.hoursInDay property is set to 8)
   * <br>
   * @throws IllegalArgumentException
   */
  public Duration decode(String duration) {
    return Duration.decode(duration, hoursInDay());
  }

  /**
   * Return the string value of the Duration.
   * <br>
   * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h" (if sonar.technicalDebt.hoursInDay property is set to 8)
   */
  public String encode(Duration duration) {
    return duration.encode(hoursInDay());
  }

  /**
   * Return the formatted work duration.
   * <br>
   * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10j 2h (if sonar.technicalDebt.hoursInDay property is set to 8)
   *
   */
  public String format(Locale locale, Duration duration, DurationFormat format) {
    return format(locale, duration);
  }

  /**
   * Return the formatted work duration.
   * <br>
   * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10j 2h (if sonar.technicalDebt.hoursInDay property is set to 8)
   *
   */
  public String format(Locale locale, Duration duration) {
    Long durationInMinutes = duration.toMinutes();
    if (durationInMinutes == 0) {
      return "0";
    }
    boolean isNegative = durationInMinutes < 0;
    Long absDuration = Math.abs(durationInMinutes);

    int days = ((Double) ((double) absDuration / hoursInDay() / 60)).intValue();
    Long remainingDuration = absDuration - (days * hoursInDay() * 60);
    int hours = ((Double) (remainingDuration.doubleValue() / 60)).intValue();
    remainingDuration = remainingDuration - (hours * 60);
    int minutes = remainingDuration.intValue();

    return format(locale, days, hours, minutes, isNegative);
  }

  private String format(Locale locale, int days, int hours, int minutes, boolean isNegative) {
    StringBuilder message = new StringBuilder();
    if (days > 0) {
      message.append(message(locale, "work_duration.x_days", isNegative ? (-1 * days) : days));
    }
    if (displayHours(days, hours)) {
      addSpaceIfNeeded(message);
      message.append(message(locale, "work_duration.x_hours", isNegative && message.length() == 0 ? (-1 * hours) : hours));
    }
    if (displayMinutes(days, hours, minutes)) {
      addSpaceIfNeeded(message);
      message.append(message(locale, "work_duration.x_minutes", isNegative && message.length() == 0 ? (-1 * minutes) : minutes));
    }
    return message.toString();
  }

  private String message(Locale locale, String key, @CheckForNull Object parameter) {
    return i18n.message(locale, key, null, parameter);
  }

  private static boolean displayHours(int days, int hours) {
    return hours > 0 && days < 10;
  }

  private static boolean displayMinutes(int days, int hours, int minutes) {
    return minutes > 0 && hours < 10 && days == 0;
  }

  private static void addSpaceIfNeeded(StringBuilder message) {
    if (message.length() > 0) {
      message.append(" ");
    }
  }

  private int hoursInDay() {
    return settings.getInt(CoreProperties.HOURS_IN_DAY);
  }

}