?????? Java ?????У?????????????????????????????????????????????????????????????е??????????????????????????????Щ????????????????????????????????????? Java ??????????Щ?????????????? String??BigInteger ?? BigDecimal ???
???????????????????????????o????
????· ???????????????
????· ??ù????в???????3???
????· ?????????????????????
????· ??????????????????????????????????
????????????????? Java ?????й???????????????????????????????????????????????????????????????е????Immutables ?? AutoValue???????????? Guava ?е????伯????
??????????????????????????????????????????????????????????????????
????????? Java ???????
????????д?????????????????????????????
????· ???????У?????????????? final?????????????????п???????????????????????
????· ???е?????????? final????????????????????????????λ??? builder ??????????????
????· ????????????? private??????????????????????????? public???????????????????????????
????????????????????????????????
public final class Autobot {
private final String name;
private final String fullname;
private final Boolean leader;
private final String group;
public Autobot(String name?? String fullname?? Boolean leader?? String group) {
this.name = name;
this.fullname = fullname;
this.leader = leader;
this.group = group;
}
public String name() {
return name;
}
public String fullname() {
return fullname;
}
public Boolean leader() {
return leader;
}
public String group() {
return group;
}
}
???????е???????? private final ???????? final ?????????????κε???????
????????Щ????????????????????????????????????????????ζ?????????μ???????????? Autobot ?????????? fusion(Autobot) ???????????????????? Autobot ?????????????????????????????
????public Autobot fusion(Autobot autobot) {
????return new Autobot(name.concat(autobot.name()));
????}
????????????????????????????????????????????????????????????????????????????????????????????????Ч?????????????????????????????棬?????????????????????????????????????????
????Android??????????????????????????Immutables ?? AutoValue????????? Android ??????????????????? Android Studio ?????????ú? APT1??annotation processor tool????
????Immutables ??????
??????????????????? Java ????α?д?????????????????????????д??????????????????п????????????????????????? Builder ?????????????????????????????д?????????????????????????????????????????
?????????????????????? Immutables ???????????????????????
????import org.immutables.value.Value;
????@Value.Immutable public abstract class Decepticon {
????public abstract String name();
????public abstract String fullname();
????public abstract Boolean leader();
????public abstract String group();
????}
??????? @Immutable ???????? IDE ?????????? Immutables ??????????????????????????????????????????????????????? Immutables ???????????????????? Immutable ????????????????? Immutable[????]?????????????????????????2??
????ImmutableDecepticon decepticon = ImmutableDecepticon.builder()
????.name("Megatron")
????.fullname("Megatron Galvatron")
????.group("Decepticons")
????.leader(true)
????.build();
????Immutable ?????????????? Decepticon ???????????????? 280 ?д????????????????????????Щ??????????????copyOf(Decepticon)??toString()??hashCode()??equals()????????????????? builder ?????????????
????Immutable ??????????????????????????????????????interfaces???????????????????????У?????????У?
????@Value.Immutable public interface ITransformer extends IAutobot?? IDecepticon {
????// it will generate and fields extended from IAutobot and IDecepticon
????}
????Immutables ???к?????????????????????????????????????? builder ???????????????? Immutables ??????????????????????????????????????
????import org.immutables.value.Value;
????@Value.Immutable public interface Car {
????enum MotorType {
????DIESEL??
????GAS
????}
????@Value.Parameter String manufacturer();
????@Value.Parameter MotorType motorType();
????}
????// create instance
????ImmutableCar car = new ImmutableCar("Nissan"?? Car.MotorType.GAS);
??????????£?Immutables ???? Guava ?? _Optional_ ?????
????AutoValue ??????
????AutoValue ?? Google ???????????????? Auto ????????????????????????? Java ?????????????? AutoFactory??AutoService ????Щ?????д??????????????ù????????????????д????????? AutoValue ?????? AutoValue ????????????????????????????
????import com.google.auto.value.AutoValue;
????@AutoValue abstract class Autobot {
????abstract String name();
????abstract String fullname();
????abstract Boolean leader();
????abstract String group();
????}
???????????AutoValue ???????????? AutoValue_Autobot ????
????AutoValue_Autobot autobot =
????new AutoValue_Autobot("Bumblebee"?? "Bumblebee Autobot"?? false?? "Autobot");
??????????????????У????????????? AutoValue ?????????????????????????????????????
import com.google.auto.value.AutoValue;
@AutoValue abstract class Autobot {
public static Autobot create(String name?? String fullname?? Boolean leader?? String group) {
return new AutoValue_Autobot(name?? fullname?? leader?? group);
}
abstract String name();
abstract String fullname();
abstract Boolean leader();
abstract String group();
}
?????????????????????????? Autobot ???????
????Autobot auto = Autobot.create("Bumblebee"?? "Bumblebee Autobot"?? false?? "Autobot");
?????????????????? Immutables ???????????AutoValue ?????????????equals()??toString() ?? hashCode() ??????????????в???У?顣
import com.google.auto.value.AutoValue;
@AutoValue abstract class Decepticon {
abstract String name();
abstract String fullname();
abstract Boolean leader();
abstract String group();
static Builder builder() {
return new AutoValue_Decepticon.Builder();
}
@AutoValue.Builder abstract static class Builder {
abstract Builder name(String name);
abstract Builder fullname(String fullname);
abstract Builder leader(Boolean leader);
abstract Builder group(String group);
abstract Decepticon build();
}
}
????AutoValue ??????????????? Builders????? Immutables ???????????????д?Щ?????????????????????????????????
?????????????builder ????????????????????
????Decepticon decepticon = Decepticon.builder()
????.name("Kakuryu")
????.fullname("Kakuryu Decepticon")
????.leader(false)
????.group("Decepticons")
????.build();
????AutoValue ??????????????????????extensions???????????????????????????????н????????????????????????????????£????? Jake Wharton ?? AutoValue Extensions3??
????Guava ??????
????Guava ???????????????????????????????????????????????? Guava ??????????????????????Щ????????????????????
????· ImmutableList
????· ImmutableSet
????· ImmutableSortedSet
????· ImmutableMap
????· ImmutableSortedMap
????· ImmutableMultiset
????· ImmutableSortedMultiset
????· ImmutableMultimap
????· ImmutableListMultimap
????· ImmutableSetMultimap
????· ImmutableBiMap
????· ImmutableClassToInstanceMap
????· ImmutableTable
????????????÷??????????????????????????????????????
????public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
????"red"??
????"orange"??
????"yellow"??
????"green"??
????"blue"??
????"purple");
????final ImmutableSet<Bar> bars = ImmutableSet.copyOf(bars); // defensive copy!
?????????? Guava ???????????????????? Cleaner code with Guava[^4]??????????????????