Update README.md

This commit is contained in:
Jordan Zimmerman
2022-04-08 09:01:07 +01:00
committed by GitHub
parent 0718e37f76
commit b2149622e4

262
README.md
View File

@@ -95,162 +95,170 @@ The full builder class is defined as:
```java ```java
public class NameAndAgeBuilder { public class NameAndAgeBuilder {
private String name; private String name;
private int age; private int age;
private NameAndAgeBuilder() { private NameAndAgeBuilder() {
} }
private NameAndAgeBuilder(String name, int age) { private NameAndAgeBuilder(String name, int age) {
this.name = name; this.name = name;
this.age = age; this.age = age;
} }
/** /**
* Static constructor/builder. Can be used instead of new NameAndAge(...) * Static constructor/builder. Can be used instead of new NameAndAge(...)
*/ */
public static NameAndAge NameAndAge(String name, int age) { public static NameAndAge NameAndAge(String name, int age) {
return new NameAndAge(name, age); return new NameAndAge(name, age);
} }
/** /**
* Return a new builder with all fields set to default Java values * Return a new builder with all fields set to default Java values
*/ */
public static NameAndAgeBuilder builder() { public static NameAndAgeBuilder builder() {
return new NameAndAgeBuilder(); return new NameAndAgeBuilder();
} }
/** /**
* Return a new builder with all fields set to the values taken from the given record instance * Return a new builder with all fields set to the values taken from the given record instance
*/ */
public static NameAndAgeBuilder builder(NameAndAge from) { public static NameAndAgeBuilder builder(NameAndAge from) {
return new NameAndAgeBuilder(from.name(), from.age()); return new NameAndAgeBuilder(from.name(), from.age());
} }
/** /**
* Return a "with"er for an existing record instance * Return a "with"er for an existing record instance
*/ */
public static NameAndAgeBuilder.With from(NameAndAge from) { public static NameAndAgeBuilder.With from(NameAndAge from) {
return new NameAndAgeBuilder.With() { return new _FromWith(from);
@Override }
public String name() {
return from.name();
}
@Override /**
public int age() { * Return a stream of the record components as map entries keyed with the component name and the value as the component value
return from.age(); */
} public static Stream<Map.Entry<String, Object>> stream(NameAndAge record) {
}; return Stream.of(new AbstractMap.SimpleImmutableEntry<>("name", record.name()),
} new AbstractMap.SimpleImmutableEntry<>("age", record.age()));
}
/** /**
* Return a stream of the record components as map entries keyed with the component name and the value as the component value * Return a new record instance with all fields set to the current values in this builder
*/ */
public static Stream<Map.Entry<String, Object>> stream(NameAndAge record) { public NameAndAge build() {
return Stream.of(Map.entry("name", record.name()), return new NameAndAge(name, age);
Map.entry("age", record.age())); }
}
/** @Override
* Return a new record instance with all fields set to the current values in this builder public String toString() {
*/ return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
public NameAndAge build() { }
return new NameAndAge(name, age);
}
@Override @Override
public String toString() { public int hashCode() {
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]"; return Objects.hash(name, age);
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
return Objects.hash(name, age); return (this == o) || ((o instanceof NameAndAgeBuilder r)
} && Objects.equals(name, r.name)
&& (age == r.age));
}
@Override /**
public boolean equals(Object o) { * Set a new value for the {@code name} record component in the builder
return (this == o) || ((o instanceof NameAndAgeBuilder r) */
&& Objects.equals(name, r.name) public NameAndAgeBuilder name(String name) {
&& (age == r.age)); this.name = name;
} return this;
}
/**
* Set a new value for the {@code name} record component in the builder
*/
public NameAndAgeBuilder name(String name) {
this.name = name;
return this;
}
/**
* Return the current value for the {@code name} record component in the builder
*/
public String name() {
return name;
}
/**
* Set a new value for the {@code age} record component in the builder
*/
public NameAndAgeBuilder age(int age) {
this.age = age;
return this;
}
/**
* Return the current value for the {@code age} record component in the builder
*/
public int age() {
return age;
}
/**
* Add withers to {@code NameAndAge}
*/
public interface With {
/** /**
* Return the current value for the {@code name} record component in the builder * Return the current value for the {@code name} record component in the builder
*/ */
String name(); public String name() {
return name;
}
/**
* Set a new value for the {@code age} record component in the builder
*/
public NameAndAgeBuilder age(int age) {
this.age = age;
return this;
}
/** /**
* Return the current value for the {@code age} record component in the builder * Return the current value for the {@code age} record component in the builder
*/ */
int age(); public int age() {
return age;
/**
* Return a new record builder using the current values
*/
default NameAndAgeBuilder with() {
return new NameAndAgeBuilder(name(), age());
} }
/** /**
* Return a new record built from the builder passed to the given consumer * Add withers to {@code NameAndAge}
*/ */
default NameAndAge with(Consumer<NameAndAgeBuilder> consumer) { public interface With {
NameAndAgeBuilder builder = with(); /**
consumer.accept(builder); * Return the current value for the {@code name} record component in the builder
return builder.build(); */
String name();
/**
* Return the current value for the {@code age} record component in the builder
*/
int age();
/**
* Return a new record builder using the current values
*/
default NameAndAgeBuilder with() {
return new NameAndAgeBuilder(name(), age());
}
/**
* Return a new record built from the builder passed to the given consumer
*/
default NameAndAge with(Consumer<NameAndAgeBuilder> consumer) {
NameAndAgeBuilder builder = with();
consumer.accept(builder);
return builder.build();
}
/**
* Return a new instance of {@code NameAndAge} with a new value for {@code name}
*/
default NameAndAge withName(String name) {
return new NameAndAge(name, age());
}
/**
* Return a new instance of {@code NameAndAge} with a new value for {@code age}
*/
default NameAndAge withAge(int age) {
return new NameAndAge(name(), age);
}
} }
/** private static final class _FromWith implements NameAndAgeBuilder.With {
* Return a new instance of {@code NameAndAge} with a new value for {@code name} private final NameAndAge from;
*/
default NameAndAge withName(String name) {
return new NameAndAge(name, age());
}
/** private _FromWith(NameAndAge from) {
* Return a new instance of {@code NameAndAge} with a new value for {@code age} this.from = from;
*/ }
default NameAndAge withAge(int age) {
return new NameAndAge(name(), age); @Override
public String name() {
return from.name();
}
@Override
public int age() {
return from.age();
}
} }
}
} }
``` ```