Java 存在哪些工具或库,它们将interface
只使用访问器方法定义并自动生成不可变对象类以及用于增量构建新实例或通过创建新实例来更改现有实例的“构建器”类?
示例输入:
public interface Car {
String getModelName();
int getWheelCount();
}
示例输出:
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
@Immutable
public final class ImmutableCar implements Car {
@NotThreadSafe
public static final class Builder implements Car {
private String modelName;
private int wheelCount;
public Builder() {
}
public Builder(final Car car) {
modelName = car.getModelName();
wheelCount = car.getWheelCount();
}
public ImmutableCar build() {
return new ImmutableCar(wheelCount, modelName);
}
@Override
public String getModelName() {
return modelName;
}
@Override
public int getWheelCount() {
return wheelCount;
}
public void setModelName(final String modelName) {
this.modelName = modelName;
}
public void setWheelCount(final int wheelCount) {
this.wheelCount = wheelCount;
}
}
private final String modelName;
private final int wheelCount;
public ImmutableCar(final int wheelCount, final String modelName) {
this.wheelCount = wheelCount;
this.modelName = modelName;
}
@Override
public String getModelName() {
return modelName;
}
@Override
public int getWheelCount() {
return wheelCount;
}
}