001/*
002 * ============================================================================
003 *  Copyright © 2002-2021 by Thomas Thrien.
004 *  All Rights Reserved.
005 * ============================================================================
006 *  Licensed to the public under the agreements of the GNU Lesser General Public
007 *  License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *       http://www.gnu.org/licenses/lgpl.html
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, WITHOUT
013 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 *  License for the specific language governing permissions and limitations
015 *  under the License.
016 */
017
018package org.tquadrat.foundation.javacomposer;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.lang.CommonConstants.EMPTY_STRING;
022
023import org.apiguardian.api.API;
024import org.tquadrat.foundation.annotation.ClassVersion;
025import org.tquadrat.foundation.javacomposer.internal.LayoutWriter;
026
027/**
028 *  The various possible layouts for the output created by
029 *  {@link JavaFile}.
030 *
031 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
032 *  @version $Id: Layout.java 855 2021-01-21 20:22:52Z tquadrat $
033 *
034 *  @UMLGraph.link
035 *  @since 0.0.5
036 */
037@ClassVersion( sourceVersion = "$Id: Layout.java 855 2021-01-21 20:22:52Z tquadrat $" )
038@API( status = STABLE, since = "0.0.5" )
039public enum Layout implements LayoutWriter
040{
041        /*------------------*\
042    ====** Enum Declaration **=================================================
043        \*------------------*/
044    /**
045     *  <p>{@summary The layout as used for the Foundation library.}</p>
046     *  <p>The layout of the generated Java file does not follow that of
047     *  the Foundation library source completely as this would require to much
048     *  effort to implement. Additionally, the code that is provided for the
049     *  method bodies will not be parsed, so variations in the layout here will
050     *  not be corrected.</p>
051     *  <p>Here must be especially mentioned the methods
052     *  {@link CodeBlock.Builder#beginControlFlow(String, Object...)},
053     *  {@link CodeBlock.Builder#nextControlFlow(String, Object...)}
054     *  and
055     *  {@link CodeBlock.Builder#endControlFlow(String, Object...)}:
056     *  these will generate code like this:</p>
057     *  <pre><code>  if( flag ) {
058     *      doThis();
059     *  } else {
060     *      doSomethingDifferent(); }</code></pre>
061     *  <p>while the Foundation layout would look like this:</p>
062     *  <pre><code>  if( flag )
063     *  {
064     *      doThis();
065     *  }
066     *  else
067     *  {
068     *      doSomethingDifferent();
069     *  }</code></pre>
070     *  <p>But these differences are seen as neglectable.</p>
071     *
072     *  TODO Adjust this comment!!
073     */
074    @API( status = STABLE, since = "0.0.5" )
075    LAYOUT_FOUNDATION( false, 4 ),
076
077    /**
078     *  The layout for the original JavaPoet.
079     */
080    @API( status = STABLE, since = "0.0.5" )
081    LAYOUT_JAVAPOET( false, 2 ),
082
083    /**
084     *  The layout for the original JavaPoet, but using the tabulator character
085     *  for the indentation, instead of blanks.
086     */
087    @API( status = STABLE, since = "0.2.0" )
088    LAYOUT_JAVAPOET_WITH_TAB( true, 4 ),
089
090    /**
091     *  The default layout; same as
092     *  {@link #LAYOUT_JAVAPOET}.
093     */
094    @API( status = STABLE, since = "0.0.5" )
095    LAYOUT_DEFAULT( "\t".equals( LAYOUT_JAVAPOET.m_Indentation ), LAYOUT_JAVAPOET.m_TabSize );
096
097        /*------------*\
098    ====** Attributes **=======================================================
099        \*------------*/
100    /**
101     *  The indentation String.
102     */
103    private final String m_Indentation;
104
105    /**
106     *  The tabulator size.
107     */
108    private final int m_TabSize;
109
110        /*--------------*\
111    ====** Constructors **=====================================================
112        \*--------------*/
113    /**
114     *  Creates a new instance for {@code Layout}.
115     *
116     *  @param  useTab  {@code true} if the tabulator should be used for the
117     *      indentation, {@code false} if blanks should be used.
118     *  @param  tabSize The tabulator size.
119     */
120    private Layout( final boolean useTab, final int tabSize )
121    {
122        assert tabSize >= 0 : "Invalid tabulator size";
123        m_TabSize = tabSize;
124        m_Indentation =  useTab ? "\t" : tabSize > 0 ? " ".repeat( tabSize ) : EMPTY_STRING;
125    }   //  Layout()
126
127        /*---------*\
128    ====** Methods **==========================================================
129        \*---------*/
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public final String indent() { return m_Indentation; }
135
136    /**
137     *  {@inheritDoc}
138     */
139    @Override
140    public final int tabsize() { return m_TabSize; }
141}
142//  enum Layout
143
144/*
145 *  End of File
146 */