001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 org.apache.commons.lang.ArrayUtils;
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.lang.builder.ToStringBuilder;
025import org.apache.commons.lang.builder.ToStringStyle;
026
027import javax.annotation.Nullable;
028
029public final class WorkUnit {
030
031  public static final String DAYS = "d";
032  public static final String MINUTES = "mn";
033  public static final String HOURS = "h";
034  public static final String DEFAULT_UNIT = DAYS;
035  private static final String[] UNITS = {DAYS, MINUTES, HOURS};
036
037  public static final double DEFAULT_VALUE = 0.0;
038
039  private double value = 0d;
040  private String unit = DEFAULT_UNIT;
041
042  WorkUnit(double value, String unit) {
043    this.value = value;
044    this.unit = unit;
045  }
046
047  public double getValue() {
048    return value;
049  }
050
051  public String getUnit() {
052    return unit;
053  }
054
055  public static WorkUnit create(@Nullable Double value, @Nullable String unit) {
056    String defaultIfEmptyUnit = StringUtils.defaultIfEmpty(unit, DEFAULT_UNIT);
057    if (!ArrayUtils.contains(UNITS, defaultIfEmptyUnit)) {
058      throw new IllegalArgumentException("Unit can not be: " + defaultIfEmptyUnit + ". Possible values are " + ArrayUtils.toString(UNITS));
059    }
060    double d = value != null ? value : DEFAULT_VALUE;
061    if (d < 0.0) {
062      throw new IllegalArgumentException("Value can not be negative: " + d);
063    }
064    return new WorkUnit(d, defaultIfEmptyUnit);
065  }
066
067  public static WorkUnit create() {
068    return create(0d, DEFAULT_UNIT);
069  }
070
071  @Override
072  public boolean equals(Object o) {
073    if (this == o) {
074      return true;
075    }
076    if (o == null || getClass() != o.getClass()) {
077      return false;
078    }
079
080    WorkUnit workUnit = (WorkUnit) o;
081
082    if (Double.compare(workUnit.value, value) != 0) {
083      return false;
084    }
085    if (!unit.equals(workUnit.unit)) {
086      return false;
087    }
088
089    return true;
090  }
091
092  @Override
093  public int hashCode() {
094    int result;
095    long temp;
096    temp = Double.doubleToLongBits(value);
097    result = (int) (temp ^ (temp >>> 32));
098    result = 31 * result + unit.hashCode();
099    return result;
100  }
101
102  @Override
103  public String toString() {
104    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
105  }
106}