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 021package org.sonar.api.server.debt.internal; 022 023import com.google.common.base.Objects; 024import javax.annotation.CheckForNull; 025import javax.annotation.Nullable; 026import org.apache.commons.lang.StringUtils; 027import org.apache.commons.lang.builder.EqualsBuilder; 028import org.sonar.api.server.debt.DebtRemediationFunction; 029import org.sonar.api.utils.Duration; 030 031import static com.google.common.base.Preconditions.checkArgument; 032 033public class DefaultDebtRemediationFunction implements DebtRemediationFunction { 034 035 private static final int HOURS_IN_DAY = 24; 036 037 private final Type type; 038 private final String coefficient; 039 private final String offset; 040 041 public DefaultDebtRemediationFunction(@Nullable Type type, @Nullable String coefficient, @Nullable String offset) { 042 this.type = type; 043 this.coefficient = sanitizeValue("coefficient", coefficient); 044 this.offset = sanitizeValue("offset", offset); 045 validate(); 046 } 047 048 @CheckForNull 049 private static String sanitizeValue(String label, @Nullable String s) { 050 if (StringUtils.isNotBlank(s)) { 051 try { 052 Duration duration = Duration.decode(s, HOURS_IN_DAY); 053 return duration.encode(HOURS_IN_DAY); 054 } catch (Exception e) { 055 throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e); 056 } 057 } 058 return null; 059 } 060 061 @Override 062 public Type type() { 063 return type; 064 } 065 066 @Override 067 @CheckForNull 068 public String coefficient() { 069 return coefficient; 070 } 071 072 @Override 073 @CheckForNull 074 public String offset() { 075 return offset; 076 } 077 078 private void validate() { 079 checkArgument(type != null, "Remediation function type cannot be null"); 080 switch (type) { 081 case LINEAR: 082 checkArgument(this.coefficient != null && this.offset == null, "Linear functions must only have a non empty coefficient"); 083 break; 084 case LINEAR_OFFSET: 085 checkArgument(this.coefficient != null && this.offset != null, "Linear with offset functions must have both non null coefficient and offset"); 086 break; 087 case CONSTANT_ISSUE: 088 checkArgument(this.coefficient == null && this.offset != null, "Constant/issue functions must only have a non empty offset"); 089 break; 090 default: 091 throw new IllegalArgumentException(String.format("Unknown type on %s", this)); 092 } 093 } 094 095 @Override 096 public boolean equals(Object o) { 097 if (!(o instanceof DefaultDebtRemediationFunction)) { 098 return false; 099 } 100 if (this == o) { 101 return true; 102 } 103 DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o; 104 return new EqualsBuilder() 105 .append(coefficient, other.coefficient()) 106 .append(offset, other.offset()) 107 .append(type, other.type()) 108 .isEquals(); 109 } 110 111 @Override 112 public int hashCode() { 113 int result = type.hashCode(); 114 result = 31 * result + (coefficient != null ? coefficient.hashCode() : 0); 115 result = 31 * result + (offset != null ? offset.hashCode() : 0); 116 return result; 117 } 118 119 @Override 120 public String toString() { 121 return Objects.toStringHelper(DebtRemediationFunction.class) 122 .add("type", type) 123 .add("coefficient", coefficient) 124 .add("offset", offset) 125 .toString(); 126 } 127}