读完部分effective java 自己写的类Entity

自己写的Entity类,有四个属性,int,char,Sting,double,用了构造器方法,可以四个参数都有,也可以之后几个,但int是必须的。类还实现了comparable接口,重写了compareTo,equals,hashCode三个方法,这是我能想到的比较OK的类。

[color=red]compareTo的比较方法是按int>char>String>double的顺序[/color]
equals方法中规中矩,比较而已
[color=red]hashCode用的书中的方法,待思考[/color]
package com.util;

public class Entity implements Comparable{

private final int i;
private final char c;
private final String s;
private final double d;

public static class Builder {
private final int i;
private char c;
private String s;
private double d;
public Builder(int i) {
this.i = i;
}
public Builder setC(char c) {
this.c = c;
return this;
}
public Builder setS(String s) {
this.s = s;
return this;
}
public Builder setD(double d) {
this.d = d;
return this;
}
public Entity build() {
return new Entity(this);
}
}
private Entity(Builder b) {
this.c = b.c;
this.d = b.d;
this.s = b.s;
this.i = b.i;
}

@Override
public int compareTo(Object o) {
if(this == o)
return 0;
if(!(o instanceof Entity))
throw new IllegalArgumentException("Cannot compare Pair with "
+ o.getClass().getName());
Entity e = (Entity)o;
if(i == e.i) {
if(c == e.c) {
if((s == null && e.s == null) || (s != null && s.compareTo(e.s) == 0)) {
if(Double.compare(d, e.d) == 0) {
return 0;
} else {
return Double.compare(d, e.d);
}
} else {
if( s == null && e.s != null) return -1;
else if( e.s == null && s != null) return 1;
else return s.compareTo(e.s);
}
} else {
return c - e.c > 0 ? 1 : -1;
}
} else {
return i - e.i > 0 ? 1 : -1;
}
}

@Override
public boolean equals(Object obj) {
if(!(obj instanceof Entity)) return false;
Entity e = (Entity)obj;
if(this.i == e.i)
if(this.c == e.c)
if( (s == null && e.s == null) || this.s.equals(e.s))
if(this.d == e.d)
return true;
return false;
}

@Override
public String toString() {
return "[int = " + i + ",char = " + c + ",String = " + s + ", double = " + d + "]";
}
@Override
public int hashCode() {
int result = 17;
result = 31*result + this.i;
result = 31*result + (int)this.c;
result = 31*result + (int)Math.round(d);
result = 31*result + (int)s.charAt(0);
return result ;
}


}

还有测试代码:
package com.util;

import junit.framework.Assert;

import org.junit.Test;


public class TestEntity {

@Test
public void testEquals() {
Entity e1 = new Entity.Builder(3).setC('b').build();
Entity e2 = new Entity.Builder(3).setC('b').build();
Entity e3 = new Entity.Builder(3).build();
Integer i = Integer.valueOf("3");
Assert.assertEquals(e1.equals(e2), true);
Assert.assertEquals(e1.equals(e3), false);
Assert.assertEquals(e3.equals(i), false);
}
@Test
public void testCompareTo() {
Entity e1 = new Entity.Builder(3).setC('b').build();
Entity e2 = new Entity.Builder(3).setC('b').build();
Entity e3 = new Entity.Builder(3).setC('a').build();
Entity e4 = new Entity.Builder(3).setC('b').setS("asd").build();

Assert.assertEquals(e1.compareTo(e1), 0);
Assert.assertEquals(e1.compareTo(e2), 0);
Assert.assertEquals(e1.compareTo(e3), 1);
Assert.assertEquals(e1.compareTo(e4), -1);
}
@Test(expected=IllegalArgumentException.class)
public void testCompareTo2() {
Entity e1 = new Entity.Builder(3).setC('b').build();
Integer i = Integer.valueOf("3");
Assert.assertEquals(e1.compareTo(i), 0);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值