Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test;
- import com.google.common.annotations.VisibleForTesting;
- import com.google.common.base.Joiner;
- import com.google.common.base.Preconditions;
- import java.util.EnumSet;
- import java.util.Objects;
- import java.util.function.UnaryOperator;
- import javax.annotation.Generated;
- /**
- * Auto-generated superclass of {@link Employee.Builder}, derived from the API of {@link Employee}.
- */
- @Generated("org.inferred.freebuilder.processor.CodeGenerator")
- abstract class Employee_Builder {
- /** Creates a new builder using {@code value} as a template. */
- public static Employee.Builder from(Employee value) {
- return Employee.builder().mergeFrom(value);
- }
- private static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();
- private enum Property {
- NAME("name"),
- ID("id"),
- ;
- private final String name;
- private Property(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return name;
- }
- }
- private String name;
- private int id;
- private final EnumSet<Employee_Builder.Property> _unsetProperties =
- EnumSet.allOf(Employee_Builder.Property.class);
- /**
- * Sets the value to be returned by {@link Employee#name()}.
- *
- * @return this {@code Builder} object
- * @throws NullPointerException if {@code name} is null
- */
- public Employee.Builder name(String name) {
- this.name = Preconditions.checkNotNull(name);
- _unsetProperties.remove(Employee_Builder.Property.NAME);
- return (Employee.Builder) this;
- }
- /**
- * Replaces the value to be returned by {@link Employee#name()} by applying {@code mapper} to it
- * and using the result.
- *
- * @return this {@code Builder} object
- * @throws NullPointerException if {@code mapper} is null or returns null
- * @throws IllegalStateException if the field has not been set
- */
- public Employee.Builder mapName(UnaryOperator<String> mapper) {
- Preconditions.checkNotNull(mapper);
- return name(mapper.apply(name()));
- }
- /**
- * Returns the value that will be returned by {@link Employee#name()}.
- *
- * @throws IllegalStateException if the field has not been set
- */
- public String name() {
- Preconditions.checkState(
- !_unsetProperties.contains(Employee_Builder.Property.NAME), "name not set");
- return name;
- }
- /**
- * Sets the value to be returned by {@link Employee#id()}.
- *
- * @return this {@code Builder} object
- */
- public Employee.Builder id(int id) {
- this.id = id;
- _unsetProperties.remove(Employee_Builder.Property.ID);
- return (Employee.Builder) this;
- }
- /**
- * Replaces the value to be returned by {@link Employee#id()} by applying {@code mapper} to it and
- * using the result.
- *
- * @return this {@code Builder} object
- * @throws NullPointerException if {@code mapper} is null or returns null
- * @throws IllegalStateException if the field has not been set
- */
- public Employee.Builder mapId(UnaryOperator<Integer> mapper) {
- Preconditions.checkNotNull(mapper);
- return id(mapper.apply(id()));
- }
- /**
- * Returns the value that will be returned by {@link Employee#id()}.
- *
- * @throws IllegalStateException if the field has not been set
- */
- public int id() {
- Preconditions.checkState(
- !_unsetProperties.contains(Employee_Builder.Property.ID), "id not set");
- return id;
- }
- /** Sets all property values using the given {@code Employee} as a template. */
- public Employee.Builder mergeFrom(Employee value) {
- Employee_Builder _defaults = Employee.builder();
- if (_defaults._unsetProperties.contains(Employee_Builder.Property.NAME)
- || !Objects.equals(value.name(), _defaults.name())) {
- name(value.name());
- }
- if (_defaults._unsetProperties.contains(Employee_Builder.Property.ID)
- || !Objects.equals(value.id(), _defaults.id())) {
- id(value.id());
- }
- return (Employee.Builder) this;
- }
- /**
- * Copies values from the given {@code Builder}. Does not affect any properties not set on the
- * input.
- */
- public Employee.Builder mergeFrom(Employee.Builder template) {
- // Upcast to access private fields; otherwise, oddly, we get an access violation.
- Employee_Builder base = template;
- Employee_Builder _defaults = Employee.builder();
- if (!base._unsetProperties.contains(Employee_Builder.Property.NAME)
- && (_defaults._unsetProperties.contains(Employee_Builder.Property.NAME)
- || !Objects.equals(template.name(), _defaults.name()))) {
- name(template.name());
- }
- if (!base._unsetProperties.contains(Employee_Builder.Property.ID)
- && (_defaults._unsetProperties.contains(Employee_Builder.Property.ID)
- || !Objects.equals(template.id(), _defaults.id()))) {
- id(template.id());
- }
- return (Employee.Builder) this;
- }
- /** Resets the state of this builder. */
- public Employee.Builder clear() {
- Employee_Builder _defaults = Employee.builder();
- name = _defaults.name;
- id = _defaults.id;
- _unsetProperties.clear();
- _unsetProperties.addAll(_defaults._unsetProperties);
- return (Employee.Builder) this;
- }
- /**
- * Returns a newly-created {@link Employee} based on the contents of the {@code Builder}.
- *
- * @throws IllegalStateException if any field has not been set
- */
- public Employee build() {
- Preconditions.checkState(_unsetProperties.isEmpty(), "Not set: %s", _unsetProperties);
- return new Employee_Builder.Value(this);
- }
- /**
- * Returns a newly-created partial {@link Employee} for use in unit tests. State checking will not
- * be performed. Unset properties will throw an {@link UnsupportedOperationException} when
- * accessed via the partial object.
- *
- * <p>Partials should only ever be used in tests. They permit writing robust test cases that won't
- * fail if this type gains more application-level constraints (e.g. new required fields) in
- * future. If you require partially complete values in production code, consider using a Builder.
- */
- @VisibleForTesting()
- public Employee buildPartial() {
- return new Employee_Builder.Partial(this);
- }
- private static final class Value extends Employee {
- private final String name;
- private final int id;
- private Value(Employee_Builder builder) {
- this.name = builder.name;
- this.id = builder.id;
- }
- @Override
- public String name() {
- return name;
- }
- @Override
- public int id() {
- return id;
- }
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof Employee_Builder.Value)) {
- return false;
- }
- Employee_Builder.Value other = (Employee_Builder.Value) obj;
- return Objects.equals(name, other.name) && Objects.equals(id, other.id);
- }
- @Override
- public int hashCode() {
- return Objects.hash(name, id);
- }
- @Override
- public String toString() {
- return "Employee{" + "name=" + name + ", " + "id=" + id + "}";
- }
- }
- private static final class Partial extends Employee {
- private final String name;
- private final int id;
- private final EnumSet<Employee_Builder.Property> _unsetProperties;
- Partial(Employee_Builder builder) {
- this.name = builder.name;
- this.id = builder.id;
- this._unsetProperties = builder._unsetProperties.clone();
- }
- @Override
- public String name() {
- if (_unsetProperties.contains(Employee_Builder.Property.NAME)) {
- throw new UnsupportedOperationException("name not set");
- }
- return name;
- }
- @Override
- public int id() {
- if (_unsetProperties.contains(Employee_Builder.Property.ID)) {
- throw new UnsupportedOperationException("id not set");
- }
- return id;
- }
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof Employee_Builder.Partial)) {
- return false;
- }
- Employee_Builder.Partial other = (Employee_Builder.Partial) obj;
- return Objects.equals(name, other.name)
- && Objects.equals(id, other.id)
- && Objects.equals(_unsetProperties, other._unsetProperties);
- }
- @Override
- public int hashCode() {
- return Objects.hash(name, id, _unsetProperties);
- }
- @Override
- public String toString() {
- return "partial Employee{"
- + COMMA_JOINER.join(
- (!_unsetProperties.contains(Employee_Builder.Property.NAME) ? "name=" + name : null),
- (!_unsetProperties.contains(Employee_Builder.Property.ID) ? "id=" + id : null))
- + "}";
- }
- }
- }
Add Comment
Please, Sign In to add comment