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.lang;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.lang.Objects.isNull;
022import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
023import static org.tquadrat.foundation.lang.Stringer.DEFAULT_STRINGER;
024
025import java.io.Serial;
026import java.util.function.Function;
027
028import org.apiguardian.api.API;
029import org.tquadrat.foundation.annotation.ClassVersion;
030
031/**
032 *  This implementation of
033 *  {@link StringConverter}
034 *  allows to create an instance of {@code StringConverter} for an arbitrary
035 *  type on the fly.
036 *
037 *  @param  <T> The Object type for the conversion.
038 *
039 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
040 *  @version $Id: GenericStringConverter.java 1078 2023-10-19 14:39:47Z tquadrat $
041 *  @since 0.0.6
042 *
043 *  @UMLGraph.link
044 */
045@ClassVersion( sourceVersion = "$Id: GenericStringConverter.java 1078 2023-10-19 14:39:47Z tquadrat $" )
046@API( status = STABLE, since = "0.0.6" )
047public class GenericStringConverter<T> implements StringConverter<T>
048{
049        /*------------*\
050    ====** Attributes **=======================================================
051        \*------------*/
052    /**
053     *  The parser.
054     *
055     *  @serial
056     */
057    private final Function<CharSequence,T> m_Parser;
058
059    /**
060     *  The stringer.
061     *
062     *  @serial
063     */
064    private final Stringer<T> m_Stringer;
065
066        /*------------------------*\
067    ====** Static Initialisations **===========================================
068        \*------------------------*/
069    /**
070     *  The serial version UID for objects of this class: {@value}.
071     *
072     *  @hidden
073     */
074    @Serial
075    private static final long serialVersionUID = 1L;
076
077        /*--------------*\
078    ====** Constructors **=====================================================
079        \*--------------*/
080    /**
081     *  Creates a new {@code GenericStringConverter} instance.
082     *
083     *  @param  parser  The function that translates a String to an object of
084     *      type {@code T}.
085     *  @param  stringer    The function that converts an object of type
086     *      {@code T} to a String.
087     */
088    @SuppressWarnings( "unchecked" )
089    public GenericStringConverter( final Function<? extends CharSequence,T> parser, final Stringer<T> stringer )
090    {
091        m_Parser = (Function<CharSequence,T>) requireNonNullArgument( parser, "parser" );
092        m_Stringer = requireNonNullArgument( stringer, "stringer" );
093    }   //  GenericStringConverter()
094
095    /**
096     *  Creates a new {@code GenericStringConverter} instance that uses
097     *  {@link Stringer#DEFAULT_STRINGER}
098     *  to convert an object of type {@code T} to a String.
099     *
100     *  @param  parser  The function that translates a String to an object of
101     *      type {@code T}.
102     */
103    @SuppressWarnings( "unchecked" )
104    public GenericStringConverter( final Function<? extends CharSequence,T> parser )
105    {
106        this( parser, (Stringer<T>) DEFAULT_STRINGER );
107    }   //  GenericStringConverter()
108
109        /*---------*\
110    ====** Methods **==========================================================
111        \*---------*/
112    /**
113     *  {@inheritDoc}
114     */
115    @Override
116    public final T fromString( final CharSequence source ) throws IllegalArgumentException
117    {
118        final var retValue = isNull( source ) ? null : m_Parser.apply( source );
119
120        //---* Done *----------------------------------------------------------
121        return retValue;
122    }   //  fromString()
123
124    /**
125     *  {@inheritDoc}
126     */
127    @Override
128    public final String toString( final T source )
129    {
130        final var retValue = isNull( source ) ? null : m_Stringer.toString( source );
131
132        //---* Done *----------------------------------------------------------
133        return retValue;
134    }   //  toString()
135}
136//  class GenericStringConverter
137
138/*
139 *  End of File
140 */