001/*
002 * ============================================================================
003 *  Copyright © 2002-2023 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.internal;
019
020import static java.lang.String.join;
021import static org.apiguardian.api.API.Status.INTERNAL;
022import static org.tquadrat.foundation.lang.CommonConstants.EMPTY_STRING;
023import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
024
025import org.apiguardian.api.API;
026import org.tquadrat.foundation.annotation.ClassVersion;
027import org.tquadrat.foundation.javacomposer.CodeBlock;
028import org.tquadrat.foundation.javacomposer.CodeProcessor;
029import org.tquadrat.foundation.javacomposer.JavaComposer;
030
031/**
032 *  An implementation of
033 *  {@link CodeProcessor}
034 *  that creates an instance of
035 *  {@link CodeBlock}
036 *  from the given String template.
037 *
038 *  @version $Id: CodeProcessorImpl.java 1079 2023-10-22 17:44:34Z tquadrat $
039 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
040 *  @UMLGraph.link
041 *  @since 0.3.0
042 */
043@ClassVersion( sourceVersion = "$Id: CodeProcessorImpl.java 1079 2023-10-22 17:44:34Z tquadrat $" )
044@API( status = INTERNAL, since = "0.3.0" )
045public final class CodeProcessorImpl implements CodeProcessor
046{
047        /*---------------*\
048    ====** Inner Classes **====================================================
049        \*---------------*/
050
051        /*-----------*\
052    ====** Constants **========================================================
053        \*-----------*/
054
055        /*------------*\
056    ====** Attributes **=======================================================
057        \*------------*/
058    /**
059     *  The instance of
060     *  {@link JavaComposer}
061     *  that is used to create the
062     *  {@link CodeBlock}
063     *  instances.
064     */
065    @SuppressWarnings( "UseOfConcreteClass" )
066    private final JavaComposer m_Composer;
067
068        /*------------------------*\
069    ====** Static Initialisations **===========================================
070        \*------------------------*/
071
072        /*--------------*\
073    ====** Constructors **=====================================================
074        \*--------------*/
075    /**
076     *  Creates a new instance of {@code CodeProcessor}.
077     *
078     *  @param  composer    The instance of
079     *      {@link JavaComposer}
080     *      that is used to create the
081     *      {@link CodeBlock}
082     *      instances.
083     */
084    public CodeProcessorImpl( @SuppressWarnings( "UseOfConcreteClass" ) final JavaComposer composer )
085    {
086        m_Composer = requireNonNullArgument( composer, "composer" );
087    }   //  CodeProcessorImpl()
088
089        /*---------*\
090    ====** Methods **==========================================================
091        \*---------*/
092    /**
093     *  {@inheritDoc}
094     */
095    @Override
096    public CodeBlock process( final StringTemplate template )
097    {
098        final var format = join( EMPTY_STRING, template.fragments() );
099        final var args = template.values().toArray( Object []::new );
100        final var builder = m_Composer.codeBlockBuilder().add( format, args );
101        final var retValue = builder.build();
102
103        //---* Done *----------------------------------------------------------
104        return retValue;
105    }   //  process()
106}
107//  class CodeProcessorImpl
108
109/*
110 *  End of File
111 */