aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java
blob: f535a3889f83bc48335e3162092fdc30a5e5dd12 (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
/*
 * SonarQube
 * Copyright (C) 2009-2021 SonarSource SA
 * mailto:info 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 org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;

/**
 * @since 4.3
 */
@ServerSide
@ComputeEngineSide
public class Durations {

  private static final String MINUTES_FORMAT = "%smin";
  private static final String HOURS_FORMAT = "%sh";
  private static final String DAYS_FORMAT = "%sd";

  private static final int HOURS_IN_DAY = 8;

  /**
   * @deprecated since 6.3, only one format is available
   */
  @Deprecated
  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
  }

  /**
   * 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")
   * <br>
   * @throws IllegalArgumentException
   */
  public Duration decode(String duration) {
    return Duration.decode(duration, HOURS_IN_DAY);
  }

  /**
   * Return the string value of the Duration.
   * <br>
   * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h"
   */
  public String encode(Duration duration) {
    return duration.encode(HOURS_IN_DAY);
  }

  /**
   * Return the formatted work duration.
   *
   * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
   */
  @Deprecated
  public String format(Locale locale, Duration duration, DurationFormat format) {
    return format(duration);
  }

  /**
   * Return the formatted work duration.
   * <br>
   * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10d 2d
   *
   * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
   */
  @Deprecated
  public String format(Locale locale, Duration duration) {
    return format(duration);
  }

  /**
   * Return the formatted work duration using the english bundles.
   * <br>
   * Example : format(Duration.encode("9d 10h")) -&gt; 10d 2h
   *
   */
  public String format(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 / HOURS_IN_DAY / 60)).intValue();
    long remainingDuration = absDuration - (days * HOURS_IN_DAY * 60);
    int hours = ((Double) ((double) remainingDuration / 60)).intValue();
    remainingDuration = remainingDuration - (hours * 60);
    int minutes = (int) remainingDuration;

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

  private static String format(int days, int hours, int minutes, boolean isNegative) {
    StringBuilder message = new StringBuilder();
    if (days > 0) {
      message.append(String.format(DAYS_FORMAT, isNegative ? (-1 * days) : days));
    }
    if (displayHours(days, hours)) {
      addSpaceIfNeeded(message);
      message.append(String.format(HOURS_FORMAT, isNegative && message.length() == 0 ? (-1 * hours) : hours));
    }
    if (displayMinutes(days, hours, minutes)) {
      addSpaceIfNeeded(message);
      message.append(String.format(MINUTES_FORMAT, isNegative && message.length() == 0 ? (-1 * minutes) : minutes));
    }
    return message.toString();
  }

  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(" ");
    }
  }

}