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 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 029import java.io.Serializable; 030 031/** 032 * @deprecated since 4.2. Use WorkDuration instead 033 */ 034@Deprecated 035public 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 return unit.equals(workUnit.unit); 092 093 } 094 095 @Override 096 public int hashCode() { 097 int result; 098 long temp; 099 temp = Double.doubleToLongBits(value); 100 result = (int) (temp ^ (temp >>> 32)); 101 result = 31 * result + unit.hashCode(); 102 return result; 103 } 104 105 @Override 106 public String toString() { 107 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 108 } 109}