suraj1291993

Employee_FreeBuilder

Sep 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 KB | None | 0 0
  1. package com.test;
  2.  
  3. import com.google.common.annotations.VisibleForTesting;
  4. import com.google.common.base.Joiner;
  5. import com.google.common.base.Preconditions;
  6. import java.util.EnumSet;
  7. import java.util.Objects;
  8. import java.util.function.UnaryOperator;
  9. import javax.annotation.Generated;
  10.  
  11. /**
  12.  * Auto-generated superclass of {@link Employee.Builder}, derived from the API of {@link Employee}.
  13.  */
  14. @Generated("org.inferred.freebuilder.processor.CodeGenerator")
  15. abstract class Employee_Builder {
  16.  
  17.   /** Creates a new builder using {@code value} as a template. */
  18.   public static Employee.Builder from(Employee value) {
  19.     return Employee.builder().mergeFrom(value);
  20.   }
  21.  
  22.   private static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();
  23.  
  24.   private enum Property {
  25.     NAME("name"),
  26.     ID("id"),
  27.     ;
  28.  
  29.     private final String name;
  30.  
  31.     private Property(String name) {
  32.       this.name = name;
  33.     }
  34.  
  35.     @Override
  36.     public String toString() {
  37.       return name;
  38.     }
  39.   }
  40.  
  41.   private String name;
  42.   private int id;
  43.   private final EnumSet<Employee_Builder.Property> _unsetProperties =
  44.       EnumSet.allOf(Employee_Builder.Property.class);
  45.  
  46.   /**
  47.    * Sets the value to be returned by {@link Employee#name()}.
  48.    *
  49.    * @return this {@code Builder} object
  50.    * @throws NullPointerException if {@code name} is null
  51.    */
  52.   public Employee.Builder name(String name) {
  53.     this.name = Preconditions.checkNotNull(name);
  54.     _unsetProperties.remove(Employee_Builder.Property.NAME);
  55.     return (Employee.Builder) this;
  56.   }
  57.  
  58.   /**
  59.    * Replaces the value to be returned by {@link Employee#name()} by applying {@code mapper} to it
  60.    * and using the result.
  61.    *
  62.    * @return this {@code Builder} object
  63.    * @throws NullPointerException if {@code mapper} is null or returns null
  64.    * @throws IllegalStateException if the field has not been set
  65.    */
  66.   public Employee.Builder mapName(UnaryOperator<String> mapper) {
  67.     Preconditions.checkNotNull(mapper);
  68.     return name(mapper.apply(name()));
  69.   }
  70.  
  71.   /**
  72.    * Returns the value that will be returned by {@link Employee#name()}.
  73.    *
  74.    * @throws IllegalStateException if the field has not been set
  75.    */
  76.   public String name() {
  77.     Preconditions.checkState(
  78.         !_unsetProperties.contains(Employee_Builder.Property.NAME), "name not set");
  79.     return name;
  80.   }
  81.  
  82.   /**
  83.    * Sets the value to be returned by {@link Employee#id()}.
  84.    *
  85.    * @return this {@code Builder} object
  86.    */
  87.   public Employee.Builder id(int id) {
  88.     this.id = id;
  89.     _unsetProperties.remove(Employee_Builder.Property.ID);
  90.     return (Employee.Builder) this;
  91.   }
  92.  
  93.   /**
  94.    * Replaces the value to be returned by {@link Employee#id()} by applying {@code mapper} to it and
  95.    * using the result.
  96.    *
  97.    * @return this {@code Builder} object
  98.    * @throws NullPointerException if {@code mapper} is null or returns null
  99.    * @throws IllegalStateException if the field has not been set
  100.    */
  101.   public Employee.Builder mapId(UnaryOperator<Integer> mapper) {
  102.     Preconditions.checkNotNull(mapper);
  103.     return id(mapper.apply(id()));
  104.   }
  105.  
  106.   /**
  107.    * Returns the value that will be returned by {@link Employee#id()}.
  108.    *
  109.    * @throws IllegalStateException if the field has not been set
  110.    */
  111.   public int id() {
  112.     Preconditions.checkState(
  113.         !_unsetProperties.contains(Employee_Builder.Property.ID), "id not set");
  114.     return id;
  115.   }
  116.  
  117.   /** Sets all property values using the given {@code Employee} as a template. */
  118.   public Employee.Builder mergeFrom(Employee value) {
  119.     Employee_Builder _defaults = Employee.builder();
  120.     if (_defaults._unsetProperties.contains(Employee_Builder.Property.NAME)
  121.         || !Objects.equals(value.name(), _defaults.name())) {
  122.       name(value.name());
  123.     }
  124.     if (_defaults._unsetProperties.contains(Employee_Builder.Property.ID)
  125.         || !Objects.equals(value.id(), _defaults.id())) {
  126.       id(value.id());
  127.     }
  128.     return (Employee.Builder) this;
  129.   }
  130.  
  131.   /**
  132.    * Copies values from the given {@code Builder}. Does not affect any properties not set on the
  133.    * input.
  134.    */
  135.   public Employee.Builder mergeFrom(Employee.Builder template) {
  136.     // Upcast to access private fields; otherwise, oddly, we get an access violation.
  137.     Employee_Builder base = template;
  138.     Employee_Builder _defaults = Employee.builder();
  139.     if (!base._unsetProperties.contains(Employee_Builder.Property.NAME)
  140.         && (_defaults._unsetProperties.contains(Employee_Builder.Property.NAME)
  141.             || !Objects.equals(template.name(), _defaults.name()))) {
  142.       name(template.name());
  143.     }
  144.     if (!base._unsetProperties.contains(Employee_Builder.Property.ID)
  145.         && (_defaults._unsetProperties.contains(Employee_Builder.Property.ID)
  146.             || !Objects.equals(template.id(), _defaults.id()))) {
  147.       id(template.id());
  148.     }
  149.     return (Employee.Builder) this;
  150.   }
  151.  
  152.   /** Resets the state of this builder. */
  153.   public Employee.Builder clear() {
  154.     Employee_Builder _defaults = Employee.builder();
  155.     name = _defaults.name;
  156.     id = _defaults.id;
  157.     _unsetProperties.clear();
  158.     _unsetProperties.addAll(_defaults._unsetProperties);
  159.     return (Employee.Builder) this;
  160.   }
  161.  
  162.   /**
  163.    * Returns a newly-created {@link Employee} based on the contents of the {@code Builder}.
  164.    *
  165.    * @throws IllegalStateException if any field has not been set
  166.    */
  167.   public Employee build() {
  168.     Preconditions.checkState(_unsetProperties.isEmpty(), "Not set: %s", _unsetProperties);
  169.     return new Employee_Builder.Value(this);
  170.   }
  171.  
  172.   /**
  173.    * Returns a newly-created partial {@link Employee} for use in unit tests. State checking will not
  174.    * be performed. Unset properties will throw an {@link UnsupportedOperationException} when
  175.    * accessed via the partial object.
  176.    *
  177.    * <p>Partials should only ever be used in tests. They permit writing robust test cases that won't
  178.    * fail if this type gains more application-level constraints (e.g. new required fields) in
  179.    * future. If you require partially complete values in production code, consider using a Builder.
  180.    */
  181.   @VisibleForTesting()
  182.   public Employee buildPartial() {
  183.     return new Employee_Builder.Partial(this);
  184.   }
  185.  
  186.   private static final class Value extends Employee {
  187.     private final String name;
  188.     private final int id;
  189.  
  190.     private Value(Employee_Builder builder) {
  191.       this.name = builder.name;
  192.       this.id = builder.id;
  193.     }
  194.  
  195.     @Override
  196.     public String name() {
  197.       return name;
  198.     }
  199.  
  200.     @Override
  201.     public int id() {
  202.       return id;
  203.     }
  204.  
  205.     @Override
  206.     public boolean equals(Object obj) {
  207.       if (!(obj instanceof Employee_Builder.Value)) {
  208.         return false;
  209.       }
  210.       Employee_Builder.Value other = (Employee_Builder.Value) obj;
  211.       return Objects.equals(name, other.name) && Objects.equals(id, other.id);
  212.     }
  213.  
  214.     @Override
  215.     public int hashCode() {
  216.       return Objects.hash(name, id);
  217.     }
  218.  
  219.     @Override
  220.     public String toString() {
  221.       return "Employee{" + "name=" + name + ", " + "id=" + id + "}";
  222.     }
  223.   }
  224.  
  225.   private static final class Partial extends Employee {
  226.     private final String name;
  227.     private final int id;
  228.     private final EnumSet<Employee_Builder.Property> _unsetProperties;
  229.  
  230.     Partial(Employee_Builder builder) {
  231.       this.name = builder.name;
  232.       this.id = builder.id;
  233.       this._unsetProperties = builder._unsetProperties.clone();
  234.     }
  235.  
  236.     @Override
  237.     public String name() {
  238.       if (_unsetProperties.contains(Employee_Builder.Property.NAME)) {
  239.         throw new UnsupportedOperationException("name not set");
  240.       }
  241.       return name;
  242.     }
  243.  
  244.     @Override
  245.     public int id() {
  246.       if (_unsetProperties.contains(Employee_Builder.Property.ID)) {
  247.         throw new UnsupportedOperationException("id not set");
  248.       }
  249.       return id;
  250.     }
  251.  
  252.     @Override
  253.     public boolean equals(Object obj) {
  254.       if (!(obj instanceof Employee_Builder.Partial)) {
  255.         return false;
  256.       }
  257.       Employee_Builder.Partial other = (Employee_Builder.Partial) obj;
  258.       return Objects.equals(name, other.name)
  259.           && Objects.equals(id, other.id)
  260.           && Objects.equals(_unsetProperties, other._unsetProperties);
  261.     }
  262.  
  263.     @Override
  264.     public int hashCode() {
  265.       return Objects.hash(name, id, _unsetProperties);
  266.     }
  267.  
  268.     @Override
  269.     public String toString() {
  270.       return "partial Employee{"
  271.           + COMMA_JOINER.join(
  272.               (!_unsetProperties.contains(Employee_Builder.Property.NAME) ? "name=" + name : null),
  273.               (!_unsetProperties.contains(Employee_Builder.Property.ID) ? "id=" + id : null))
  274.           + "}";
  275.     }
  276.   }
  277. }
Add Comment
Please, Sign In to add comment