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