Guava Eclipse Plugin Preferences
Use MoreObjects in toString Method (requires guava 18.0)
selected
package net.sf.guavaeclipse.test;
import com.google.common.base.MoreObjects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("intValue", intValue)
.add("strValue", strValue)
.toString();
}
}
deselected
package net.sf.guavaeclipse.test;
import com.google.common.base.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("intValue", intValue)
.add("strValue", strValue)
.toString();
}
}
super method behavior
Use super class Methods (toString(), equals() and hashCode())
package net.sf.guavaeclipse.test;
import com.google.common.base.Objects;
import com.google.common.base.MoreObjects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("super", super.toString())
.add("intValue", intValue)
.add("strValue", strValue)
.toString();
}
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
if (!super.equals(object))
return false;
SampleSimple that = (SampleSimple) object;
return Objects.equal(this.intValue, that.intValue)
&& Objects.equal(this.strValue, that.strValue);
}
return false;
}
}
Don't use super class Methods (toString(), equals() and hashCode())
package net.sf.guavaeclipse.test;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
public class ClassExtendsSomeComparable extends ClassWithComparable {
private int intValue2;
public int getIntValue2() {
return intValue2;
}
public void setIntValue2(int intValue2) {
this.intValue2 = intValue2;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("intValue2", intValue2).toString();
}
@Override
public int hashCode() {
return Objects.hashCode(intValue2);
}
@Override
public boolean equals(Object object) {
if (object instanceof ClassExtendsSomeComparable) {
ClassExtendsSomeComparable that = (ClassExtendsSomeComparable) object;
return Objects.equal(this.intValue2, that.intValue2);
}
return false;
}
}
Use super class Methods (Only if superclass is not "java.lang.Object")
example one
package net.sf.guavaeclipse.test;
import com.google.common.base.Objects;
import com.google.common.base.MoreObjects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("intValue", intValue).add("strValue", strValue)
.toString();
}
@Override
public int hashCode() {
return Objects.hashCode(intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return Objects.equal(this.intValue, that.intValue)
&& Objects.equal(this.strValue, that.strValue);
}
return false;
}
}
example two
package net.sf.guavaeclipse.test;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
public class ClassExtendsSomeComparable extends ClassWithComparable {
private int intValue2;
public int getIntValue2() {
return intValue2;
}
public void setIntValue2(int intValue2) {
this.intValue2 = intValue2;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("super", super.toString())
.add("intValue2", intValue2).toString();
}
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), intValue2);
}
@Override
public boolean equals(Object object) {
if (object instanceof ClassExtendsSomeComparable) {
if (!super.equals(object))
return false;
ClassExtendsSomeComparable that = (ClassExtendsSomeComparable) object;
return Objects.equal(this.intValue2, that.intValue2);
}
return false;
}
}
Guava Eclipse Plugin Preferences
Use java.util.Objects in equals/hashCode Methods (require JDK1.7 or higher)
selected
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public int hashCode() {
return Objects.hash(intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.strValue, that.strValue);
}
return false;
}
}
deselected
package net.sf.guavaeclipse.test;
import com.google.common.base.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public int hashCode() {
return Objects.hashCode(intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equal(this.strValue, that.strValue);
}
return false;
}
}
For primitives don't use Objects method in equals() to avoid casting
selected
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String stringValue;
@Override
public int hashCode() {
return Objects.hash(intValue, stringValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.stringValue, that.stringValue);
}
return false;
}
}
deselected
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String stringValue;
@Override
public int hashCode() {
return Objects.hash(intValue, stringValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return Objects.equals(this.intValue, that.intValue) && Objects.equals(this.stringValue, that.stringValue);
}
return false;
}
}
instanceOf or class equality check
use instanceOf in equals()
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public int hashCode() {
return Objects.hash(intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.strValue, that.strValue);
}
return false;
}
}
use class equality
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String stringValue;
@Override
public int hashCode() {
return Objects.hash(intValue, stringValue);
}
@Override
public boolean equals(Object object) {
if (object != null && getClass() == object.getClass()) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.stringValue, that.stringValue);
}
return false;
}
}
use fields directly or use getter methods in equals and hashCode
use fields
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
@Override
public int hashCode() {
return Objects.hash(intValue, strValue);
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.strValue, that.strValue);
}
return false;
}
}
use getter methods
it uses only exisitng getter methods. if a field is selected but doesn't have a getter method if will use the field directly
package net.sf.guavaeclipse.test;
import java.util.Objects;
public class SampleSimple {
private int intValue;
private String strValue;
public String getStrValue() {
return strValue;
}
@Override
public int hashCode() {
return Objects.hash(intValue, getStrValue());
}
@Override
public boolean equals(Object object) {
if (object instanceof SampleSimple) {
SampleSimple that = (SampleSimple) object;
return this.intValue == that.intValue && Objects.equals(this.getStrValue(), that.getStrValue());
}
return false;
}
}