001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.utils;
021
022import java.util.Locale;
023import org.sonar.api.batch.ScannerSide;
024import org.sonar.api.ce.ComputeEngineSide;
025import org.sonar.api.server.ServerSide;
026
027/**
028 * @since 4.3
029 */
030@ScannerSide
031@ServerSide
032@ComputeEngineSide
033public class Durations {
034
035  private static final String MINUTES_FORMAT = "%smin";
036  private static final String HOURS_FORMAT = "%sh";
037  private static final String DAYS_FORMAT = "%sd";
038
039  private static final int HOURS_IN_DAY = 8;
040
041  /**
042   * @deprecated since 6.3, only one format is available
043   */
044  @Deprecated
045  public enum DurationFormat {
046    /**
047     * Display duration with only one or two members.
048     * For instance, Duration.decode("1d 1h 10min", 8) will return "1d 1h" and Duration.decode("12d 5h", 8) will return "12d"
049     */
050    SHORT
051  }
052
053  /**
054   * Create a Duration object from a number of minutes
055   */
056  public Duration create(long minutes) {
057    return Duration.create(minutes);
058  }
059
060  /**
061   * Convert the text to a Duration
062   * <br>
063   * Example : decode("9d 10 h") -&gt; Duration.encode("10d2h")
064   * <br>
065   * @throws IllegalArgumentException
066   */
067  public Duration decode(String duration) {
068    return Duration.decode(duration, HOURS_IN_DAY);
069  }
070
071  /**
072   * Return the string value of the Duration.
073   * <br>
074   * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h"
075   */
076  public String encode(Duration duration) {
077    return duration.encode(HOURS_IN_DAY);
078  }
079
080  /**
081   * Return the formatted work duration.
082   *
083   * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
084   */
085  @Deprecated
086  public String format(Locale locale, Duration duration, DurationFormat format) {
087    return format(duration);
088  }
089
090  /**
091   * Return the formatted work duration.
092   * <br>
093   * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10d 2d
094   *
095   * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
096   */
097  @Deprecated
098  public String format(Locale locale, Duration duration) {
099    return format(duration);
100  }
101
102  /**
103   * Return the formatted work duration using the english bundles.
104   * <br>
105   * Example : format(Duration.encode("9d 10h")) -&gt; 10d 2h
106   *
107   */
108  public String format(Duration duration) {
109    Long durationInMinutes = duration.toMinutes();
110    if (durationInMinutes == 0) {
111      return "0";
112    }
113    boolean isNegative = durationInMinutes < 0;
114    Long absDuration = Math.abs(durationInMinutes);
115
116    int days = ((Double) ((double) absDuration / HOURS_IN_DAY / 60)).intValue();
117    Long remainingDuration = absDuration - (days * HOURS_IN_DAY * 60);
118    int hours = ((Double) (remainingDuration.doubleValue() / 60)).intValue();
119    remainingDuration = remainingDuration - (hours * 60);
120    int minutes = remainingDuration.intValue();
121
122    return format(days, hours, minutes, isNegative);
123  }
124
125  private static String format(int days, int hours, int minutes, boolean isNegative) {
126    StringBuilder message = new StringBuilder();
127    if (days > 0) {
128      message.append(String.format(DAYS_FORMAT, isNegative ? (-1 * days) : days));
129    }
130    if (displayHours(days, hours)) {
131      addSpaceIfNeeded(message);
132      message.append(String.format(HOURS_FORMAT, isNegative && message.length() == 0 ? (-1 * hours) : hours));
133    }
134    if (displayMinutes(days, hours, minutes)) {
135      addSpaceIfNeeded(message);
136      message.append(String.format(MINUTES_FORMAT, isNegative && message.length() == 0 ? (-1 * minutes) : minutes));
137    }
138    return message.toString();
139  }
140
141  private static boolean displayHours(int days, int hours) {
142    return hours > 0 && days < 10;
143  }
144
145  private static boolean displayMinutes(int days, int hours, int minutes) {
146    return minutes > 0 && hours < 10 && days == 0;
147  }
148
149  private static void addSpaceIfNeeded(StringBuilder message) {
150    if (message.length() > 0) {
151      message.append(" ");
152    }
153  }
154
155}