Source code
Description: source: package: org.apache.commons.text, interface: Builder

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.text;
    018
    019/**
    020 * The Builder interface is designed to designate a class as a <em>builder</em>
    021 * object in the Builder design pattern. Builders are capable of creating and
    022 * configuring objects or results that normally take multiple steps to construct
    023 * or are very complex to derive.
    024 *
    025 * <p>
    026 * The builder interface defines a single method, {@link #build()}, that
    027 * classes must implement. The result of this method should be the final
    028 * configured object or result after all building operations are performed.
    029 * </p>
    030 *
    031 * <p>
    032 * It is a recommended practice that the methods supplied to configure the
    033 * object or result being built return a reference to {@code this} so that
    034 * method calls can be chained together.
    035 * </p>
    036 *
    037 * <p>
    038 * Example Builder:
    039 * </p>
    040 * <pre><code>
    041 * class FontBuilder implements Builder&lt;Font&gt; {
    042 *     private Font font;
    043 *
    044 *     public FontBuilder(String fontName) {
    045 *         this.font = new Font(fontName, Font.PLAIN, 12);
    046 *     }
    047 *
    048 *     public FontBuilder bold() {
    049 *         this.font = this.font.deriveFont(Font.BOLD);
    050 *         return this; // Reference returned so calls can be chained
    051 *     }
    052 *
    053 *     public FontBuilder size(float pointSize) {
    054 *         this.font = this.font.deriveFont(pointSize);
    055 *         return this; // Reference returned so calls can be chained
    056 *     }
    057 *
    058 *     // Other Font construction methods
    059 *
    060 *     public Font build() {
    061 *         return this.font;
    062 *     }
    063 * }
    064 * </code></pre>
    065 *
    066 * Example Builder Usage:
    067 * <pre><code>
    068 * Font bold14ptSansSerifFont = new FontBuilder(Font.SANS_SERIF).bold()
    069 *                                                              .size(14.0f)
    070 *                                                              .build();
    071 * </code></pre>
    072 *
    073 *
    074 * @param <T> the type of object that the builder will construct or compute.
    075 * @since 1.0
    076 */
    077public interface Builder<T> {
    078
    079    /**
    080     * Returns a reference to the object being constructed or result being
    081     * calculated by the builder.
    082     *
    083     * @return The object constructed or result calculated by the builder.
    084     */
    085    T build();
    086}