AbstractSupplier.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.build;

  18. import org.apache.commons.io.function.IOSupplier;

  19. /**
  20.  * Abstracts supplying an instance of {@code T}.
  21.  * <p>
  22.  * Extend this class to implement the builder pattern.
  23.  * </p>
  24.  * <p>
  25.  * For example, here is a builder, a domain class, and a test.
  26.  * </p>
  27.  * <p>
  28.  * The builder:
  29.  * </p>
  30.  * <pre>
  31.     &#47;**
  32.      &#42; Builds Foo instances.
  33.      &#42;&#47;
  34.     public static class Builder extends AbstractSupplier&#60;Foo, Builder&#62; {

  35.         private String bar1;
  36.         private String bar2;
  37.         private String bar3;

  38.         &#47;**
  39.          &#42; Builds a new Foo.
  40.          &#42;&#47;
  41.         &#64;Override
  42.         public Foo get() {
  43.             return new Foo(bar1, bar2, bar3);
  44.         }

  45.         public Builder setBar1(final String bar1) {
  46.             this.bar1 = bar1;
  47.             return this;
  48.         }

  49.         public Builder setBar2(final String bar2) {
  50.             this.bar2 = bar2;
  51.             return this;
  52.         }

  53.         public Builder setBar3(final String bar3) {
  54.             this.bar3 = bar3;
  55.             return this;
  56.         }
  57.     }
  58.  * </pre>
  59.  * <p>
  60.  * The domain class:
  61.  * </p>
  62.  * <pre>
  63.     &#47;**
  64.      &#42; Domain class.
  65.      &#42;&#47;
  66.     public class Foo {

  67.         public static Builder builder() {
  68.             return new Builder();
  69.         }

  70.         private final String bar1;
  71.         private final String bar2;
  72.         private final String bar3;

  73.         private Foo(final String bar1, final String bar2, final String bar3) {
  74.             this.bar1 = bar1;
  75.             this.bar2 = bar2;
  76.             this.bar3 = bar3;
  77.         }

  78.         public String getBar1() {
  79.             return bar1;
  80.         }

  81.         public String getBar2() {
  82.             return bar2;
  83.         }

  84.         public String getBar3() {
  85.             return bar3;
  86.         }

  87.     }
  88.  * </pre>
  89.  * <p>
  90.  * The test:
  91.  * </p>
  92.  * <pre>
  93.     &#64;Test
  94.     public void test() {
  95.         final Foo foo = Foo.builder()
  96.             .setBar1("value1")
  97.             .setBar2("value2")
  98.             .setBar3("value3")
  99.             .get();
  100.         assertEquals("value1", foo.getBar1());
  101.         assertEquals("value2", foo.getBar2());
  102.         assertEquals("value3", foo.getBar3());
  103.     }
  104.  * </pre>
  105.  *
  106.  * @param <T> the type of instances to build.
  107.  * @param <B> the type of builder subclass.
  108.  * @since 2.12.0
  109.  */
  110. public abstract class AbstractSupplier<T, B extends AbstractSupplier<T, B>> implements IOSupplier<T> {

  111.     /**
  112.      * Constructs a new instance for subclasses.
  113.      */
  114.     public AbstractSupplier() {
  115.         // empty
  116.     }

  117.     /**
  118.      * Returns this instance typed as the subclass type {@code B}.
  119.      * <p>
  120.      * This is the same as the expression:
  121.      * </p>
  122.      * <pre>
  123.      * (B) this
  124.      * </pre>
  125.      *
  126.      * @return this instance typed as the subclass type {@code B}.
  127.      */
  128.     @SuppressWarnings("unchecked")
  129.     protected B asThis() {
  130.         return (B) this;
  131.     }

  132. }