001/*
002 * ============================================================================
003 *  Copyright © 2002-2022 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.config.ap.impl.specialprops;
019
020import static javax.lang.model.element.Modifier.FINAL;
021import static javax.lang.model.element.Modifier.PRIVATE;
022import static org.apiguardian.api.API.Status.STABLE;
023import static org.tquadrat.foundation.config.SpecialPropertyType.CONFIG_PROPERTY_PID;
024import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
025
026import java.util.Optional;
027import java.util.function.BiFunction;
028
029import org.apiguardian.api.API;
030import org.tquadrat.foundation.annotation.ClassVersion;
031import org.tquadrat.foundation.config.SpecialPropertyType;
032import org.tquadrat.foundation.config.ap.impl.CodeBuilder;
033import org.tquadrat.foundation.config.ap.impl.PropertySpecImpl;
034import org.tquadrat.foundation.javacomposer.CodeBlock;
035import org.tquadrat.foundation.javacomposer.FieldSpec;
036import org.tquadrat.foundation.javacomposer.TypeName;
037import org.tquadrat.foundation.util.SystemUtils;
038
039/**
040 *  The implementation of
041 *  {@link SpecialPropertySpecBase}
042 *  for
043 *  {@link SpecialPropertyType#CONFIG_PROPERTY_PID}.
044 *
045 *  @version $Id: ProcessIdProperty.java 1001 2022-01-29 16:42:15Z tquadrat $
046 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
047 *  @UMLGraph.link
048 *  @since 0.1.0
049 */
050@ClassVersion( sourceVersion = "$Id: ProcessIdProperty.java 1001 2022-01-29 16:42:15Z tquadrat $" )
051@API( status = STABLE, since = "0.1.0" )
052public final class ProcessIdProperty extends SpecialPropertySpecBase
053{
054        /*--------------*\
055    ====** Constructors **=====================================================
056        \*--------------*/
057    /**
058     *  Creates a new instance of {@code ProcessIdProperty}.
059     */
060    public ProcessIdProperty()
061    {
062        super( CONFIG_PROPERTY_PID );
063    }   //  ProcessIdProperty()
064
065        /*---------*\
066    ====** Methods **==========================================================
067        \*---------*/
068    /**
069     *  Composes the constructor fragment for the initialisation of this
070     *  property.
071     *
072     *  @param  codeBuilder The factory for the code generation.
073     *  @param  property    The property.
074     *  @return The field specification.
075     */
076    @SuppressWarnings( {"TypeMayBeWeakened", "UseOfConcreteClass"} )
077    private static final CodeBlock composeConstructorFragment( final CodeBuilder codeBuilder, final PropertySpecImpl property )
078    {
079        final var builder = requireNonNullArgument( codeBuilder, "codeBuilder" ).getComposer()
080            .codeBlockBuilder()
081            .add(
082                """
083                
084                /*
085                 * Initialise the property '$N'.
086                 */
087                """, property.getPropertyName()
088            )
089            .addStatement( "$1N = getPID()", property.getFieldName() )
090            .addStaticImport( SystemUtils.class, "getPID" );
091
092        //---* Create the return value *---------------------------------------
093        final var retValue = builder.build();
094
095        //---* Done *----------------------------------------------------------
096        return retValue;
097    }   //  composeConstructorFragment()
098
099    /**
100     *  The method that composes a field for the 'processId' property.
101     *
102     *  @param  codeBuilder The factory for the code generation.
103     *  @param  property    The property.
104     *  @return The field specification.
105     */
106    @SuppressWarnings( {"TypeMayBeWeakened", "UseOfConcreteClass"} )
107    private static FieldSpec composeField( final CodeBuilder codeBuilder, final PropertySpecImpl property )
108    {
109        final var composer = requireNonNullArgument( codeBuilder, "codeBuilder" ).getComposer();
110
111        final var builder = composer.fieldBuilder( property.getPropertyType(), property.getFieldName(), PRIVATE )
112            .addJavadoc(
113                """
114                Special Property: "$L".
115                """, property.getPropertyName() )
116            .addModifiers( FINAL );
117
118        //---* Create the return value *--------------------------------------
119        final var retValue = builder.build();
120
121        //---* Done *----------------------------------------------------------
122        return retValue;
123    }   //  composeField()
124
125    /**
126     *  {@inheritDoc}
127     */
128    @Override
129    public final Optional<TypeName> getCLIValueHandlerClass() { return Optional.empty(); }
130
131    /**
132     *  {@inheritDoc}
133     */
134    @Override
135    public final Optional<BiFunction<CodeBuilder, PropertySpecImpl, CodeBlock>> getConstructorFragmentComposer() { return Optional.of( ProcessIdProperty::composeConstructorFragment );}
136
137    /**
138     * {@inheritDoc}
139     */
140    @Override
141    public final Optional<BiFunction<CodeBuilder,PropertySpecImpl, FieldSpec>> getFieldComposer() { return Optional.of( ProcessIdProperty::composeField ); }
142
143    /**
144     *  {@inheritDoc}
145     */
146    @Override
147    public final Optional<TypeName> getPrefsAccessorClass() { return Optional.empty(); }
148
149    /**
150     *  {@inheritDoc}
151     */
152    @Override
153    public final TypeName getPropertyType() { return TypeName.from( long.class ); }
154
155    /**
156     *  {@inheritDoc}
157     */
158    @Override
159    public final Optional<TypeName> getStringConverterClass() { return Optional.empty(); }
160}
161//  class ProcessIdProperty
162
163/*
164 *  End of File
165 */