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 */ 020package org.sonar.api.batch.sensor.dependency.internal; 021 022import org.sonar.api.batch.sensor.internal.SensorStorage; 023 024import com.google.common.base.Preconditions; 025import org.apache.commons.lang.builder.EqualsBuilder; 026import org.apache.commons.lang.builder.HashCodeBuilder; 027import org.sonar.api.batch.fs.InputFile; 028import org.sonar.api.batch.fs.internal.DefaultInputFile; 029import org.sonar.api.batch.sensor.dependency.Dependency; 030import org.sonar.api.batch.sensor.dependency.NewDependency; 031import org.sonar.api.batch.sensor.internal.DefaultStorable; 032 033import javax.annotation.Nullable; 034 035public class DefaultDependency extends DefaultStorable implements Dependency, NewDependency { 036 037 private String fromKey; 038 private String toKey; 039 private int weight = 1; 040 041 public DefaultDependency() { 042 super(null); 043 } 044 045 public DefaultDependency(@Nullable SensorStorage storage) { 046 super(storage); 047 } 048 049 @Override 050 public DefaultDependency from(InputFile from) { 051 Preconditions.checkNotNull(from, "InputFile should be non null"); 052 this.fromKey = ((DefaultInputFile) from).key(); 053 return this; 054 } 055 056 @Override 057 public DefaultDependency to(InputFile to) { 058 Preconditions.checkNotNull(to, "InputFile should be non null"); 059 this.toKey = ((DefaultInputFile) to).key(); 060 return this; 061 } 062 063 @Override 064 public DefaultDependency weight(int weight) { 065 Preconditions.checkArgument(weight > 1, "weight should be greater than 1"); 066 this.weight = weight; 067 return this; 068 } 069 070 @Override 071 public void doSave() { 072 Preconditions.checkState(!this.fromKey.equals(this.toKey), "From and To can't be the same inputFile"); 073 Preconditions.checkNotNull(this.fromKey, "From inputFile can't be null"); 074 Preconditions.checkNotNull(this.toKey, "To inputFile can't be null"); 075 storage.store((Dependency) this); 076 } 077 078 @Override 079 public String fromKey() { 080 return this.fromKey; 081 } 082 083 public DefaultDependency setFromKey(String fromKey) { 084 this.fromKey = fromKey; 085 return this; 086 } 087 088 @Override 089 public String toKey() { 090 return this.toKey; 091 } 092 093 public DefaultDependency setToKey(String toKey) { 094 this.toKey = toKey; 095 return this; 096 } 097 098 @Override 099 public int weight() { 100 return this.weight; 101 } 102 103 public DefaultDependency setWeight(int weight) { 104 this.weight = weight; 105 return this; 106 } 107 108 // For testing purpose 109 110 @Override 111 public boolean equals(Object obj) { 112 if (obj == null) { 113 return false; 114 } 115 if (obj == this) { 116 return true; 117 } 118 if (obj.getClass() != getClass()) { 119 return false; 120 } 121 DefaultDependency rhs = (DefaultDependency) obj; 122 return new EqualsBuilder() 123 .append(fromKey, rhs.fromKey) 124 .append(toKey, rhs.toKey) 125 .append(weight, rhs.weight) 126 .isEquals(); 127 } 128 129 @Override 130 public int hashCode() { 131 return new HashCodeBuilder(27, 45). 132 append(fromKey). 133 append(toKey). 134 append(weight). 135 toHashCode(); 136 } 137 138}