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.config.ap.impl.specialprops;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.config.SpecialPropertyType.CONFIG_PROPERTY_CHARSET;
022import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.ALLOWS_PREFERENCES;
023import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.PROPERTY_IS_MUTABLE;
024import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.SETTER_CHECK_NULL;
025import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
026
027import java.nio.charset.Charset;
028import java.util.Optional;
029import java.util.function.BiFunction;
030
031import org.apiguardian.api.API;
032import org.tquadrat.foundation.annotation.ClassVersion;
033import org.tquadrat.foundation.config.SpecialPropertyType;
034import org.tquadrat.foundation.config.ap.impl.CodeBuilder;
035import org.tquadrat.foundation.config.ap.impl.PropertySpecImpl;
036import org.tquadrat.foundation.config.cli.SimpleCmdLineValueHandler;
037import org.tquadrat.foundation.config.spi.prefs.SimplePreferenceAccessor;
038import org.tquadrat.foundation.javacomposer.ClassName;
039import org.tquadrat.foundation.javacomposer.CodeBlock;
040import org.tquadrat.foundation.javacomposer.TypeName;
041import org.tquadrat.foundation.util.stringconverter.CharsetStringConverter;
042
043/**
044 *  The implementation of
045 *  {@link SpecialPropertySpecBase}
046 *  for
047 *  {@link SpecialPropertyType#CONFIG_PROPERTY_CHARSET}.
048 *
049 *  @version $Id: CharsetProperty.java 1061 2023-09-25 16:32:43Z tquadrat $
050 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
051 *  @UMLGraph.link
052 *  @since 0.1.0
053 */
054@ClassVersion( sourceVersion = "$Id: CharsetProperty.java 1061 2023-09-25 16:32:43Z tquadrat $" )
055@API( status = STABLE, since = "0.1.0" )
056public final class CharsetProperty extends SpecialPropertySpecBase
057{
058        /*--------------*\
059    ====** Constructors **=====================================================
060        \*--------------*/
061    /**
062     *  Creates a new instance of {@code CharsetProperty}.
063     */
064    public CharsetProperty()
065    {
066        super( CONFIG_PROPERTY_CHARSET, ALLOWS_PREFERENCES, PROPERTY_IS_MUTABLE, SETTER_CHECK_NULL );
067    }   //  CharsetProperty()
068
069        /*---------*\
070    ====** Methods **==========================================================
071        \*---------*/
072    /**
073     *  Composes the constructor fragment for the initialisation of this
074     *  property.
075     *
076     *  @param  codeBuilder The factory for the code generation.
077     *  @param  property    The property.
078     *  @return The field specification.
079     */
080    @SuppressWarnings( "TypeMayBeWeakened" )
081    private static final CodeBlock composeConstructorFragment( final CodeBuilder codeBuilder, @SuppressWarnings( "UseOfConcreteClass" ) final PropertySpecImpl property )
082    {
083        final var builder = requireNonNullArgument( codeBuilder, "codeBuilder" ).getComposer()
084            .codeBlockBuilder()
085            .add(
086                """
087                
088                /*
089                 * Initialise the property '$N'.
090                 */
091                """, property.getPropertyName()
092            )
093            .addStatement( "$1N = defaultCharset()", property.getFieldName() )
094            .addStaticImport( Charset.class, "defaultCharset" );
095
096        //---* Create the return value *---------------------------------------
097        final var retValue = builder.build();
098
099        //---* Done *----------------------------------------------------------
100        return retValue;
101    }   //  composeConstructorFragment()
102
103    /**
104     *  {@inheritDoc}
105     */
106    @Override
107    public final Optional<TypeName> getCLIValueHandlerClass() { return Optional.of( ClassName.from( SimpleCmdLineValueHandler.class ) ); }
108
109    /**
110     *  {@inheritDoc}
111     */
112    @Override
113    public final Optional<BiFunction<CodeBuilder, PropertySpecImpl, CodeBlock>> getConstructorFragmentComposer() { return Optional.of( CharsetProperty::composeConstructorFragment );}
114
115    /**
116     *  {@inheritDoc}
117     */
118    @Override
119    public final Optional<TypeName> getPrefsAccessorClass() { return Optional.of( ClassName.from( SimplePreferenceAccessor.class ) ); }
120
121    /**
122     *  {@inheritDoc}
123     */
124    @Override
125    public final TypeName getPropertyType() { return ClassName.from( Charset.class ); }
126
127    /**
128     *  {@inheritDoc}
129     */
130    @Override
131    public final Optional<TypeName> getStringConverterClass() { return Optional.of( ClassName.from( CharsetStringConverter.class ) ); }
132}
133//  class CharsetProperty
134
135/*
136 *  End of File
137 */