1.强制Object 的 equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用 equals,在不确定的情况下,需要使用工具方法ComparatorUtils#equals进行对象比较。
注意:如果为了更高的效率,也可以调用AssistUtils#equals。
if (ComparatorUtils.equals(a, b)) {
// do something
}
if ("abcde".equals(op)) {
// do something
}
if (a.equals(b)) {
// do something
}
2.在集合(List、Set、Map等)是否为空的是否,不允许使用 list.size() == 0的方式来判断,必须使用 list.isEmpty()。
3.配置文件的属性,都要求加上@Identifier注解,防止出现属性名重构修改的时候,持久化名字未做兼容。
@Identifier("privateSHA256Key")
private Conf<String> privateSHA256Key = Holders.simple("aki8r-mk89t-pety2-mndu8-9wrth-nht3r-012jd");
private Conf<String> smsAppKey = Holders.simple(StringUtils.EMPTY);
private Conf<String> smsAppSecret = Holders.simple(StringUtils.EMPTY);
4.重写hashCode和equals方法,需要使用下面的模板形式,不得使用IDEA自动生成的代码:
@Override
public boolean equals(Object obj) {
return obj instanceof ProductVersion
&& AssistUtils.equals(this.nameLocaleKey, ((ProductVersion)obj).nameLocaleKey)
&& AssistUtils.equals(this.name, ((ProductVersion) obj).name)
&& AssistUtils.equals(this.version, ((ProductVersion) obj).version)
&& AssistUtils.equals(this.level, ((ProductVersion) obj).level)
&& AssistUtils.equals(this.jarTime, ((ProductVersion) obj).jarTime);
}
@Override
public int hashCode() {
return AssistUtils.hashCode(nameLocaleKey, name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProductVersion that = (ProductVersion) o;
if (nameLocaleKey != null ? !nameLocaleKey.equals(that.nameLocaleKey) : that.nameLocaleKey != null)
return false;
if (version != null ? !version.equals(that.version) : that.version != null) return false;
if (jarTime != null ? !jarTime.equals(that.jarTime) : that.jarTime != null) return false;
if (level != null ? !level.equals(that.level) : that.level != null) return false;
return name != null ? name.equals(that.name) : that.name == null;
}
@Override
public int hashCode() {
int result = nameLocaleKey != null ? nameLocaleKey.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
5.重写toString方法,尽量使用下面的模板形式,不要使用IDEA自动生成的代码:
@Override
public String toString() {
return AssistUtils.toString(this);
}
如果希望排除掉一些属性加入toString中,可以用
@Override
public String toString() {
return AssistUtils.toString(this, "excludeProperty1", "excludeProperty2");
}
6.com.fr.report.core下的类,禁止被其他包下的类引用
package com.fr.report.execute
import com.fr.report.core.BoxFactory;
public class Executor {
public BoxFactory createBoxFactory() {
return new BoxFactory();
}
}
7.推荐使用枚举模式写单例,天然线程安全且简洁
public enum StateManager {
INSTANCE;
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}