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