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.util.internal;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
022
023import java.util.Comparator;
024
025import org.apiguardian.api.API;
026import org.tquadrat.foundation.annotation.ClassVersion;
027import org.tquadrat.foundation.lang.StringConverter;
028
029/**
030 *  An implementation of
031 *  {@link Comparator}
032 *  that compares object instances based on their String representation.
033 *
034 *  @param  <T> The type of the objects to compare.
035 *
036 *  @version $Id: StringBasedComparator.java 1060 2023-09-24 19:21:40Z tquadrat $
037 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
038 *  @UMLGraph.link
039 *  @since 0.1.0
040 */
041@ClassVersion( sourceVersion = "$Id: StringBasedComparator.java 1060 2023-09-24 19:21:40Z tquadrat $" )
042@API( status = STABLE, since = "0.1.0" )
043public class StringBasedComparator<T> implements Comparator<T>
044{
045        /*------------*\
046    ====** Attributes **=======================================================
047        \*------------*/
048    /**
049     *  The instance of
050     *  {@link StringConverter}
051     *  that is used to translate the object instances to Strings.
052     */
053    private final StringConverter<T> m_StringConverter;
054
055        /*--------------*\
056    ====** Constructors **=====================================================
057        \*--------------*/
058    /**
059     *  Creates a new instance of {@code StringBasedComparator}.
060     *
061     *  @param  stringConverter The instance of
062     *      {@link StringConverter}
063     *      that is used to translate the object instances to Strings.
064     */
065    public StringBasedComparator( final StringConverter<T> stringConverter )
066    {
067        m_StringConverter = requireNonNullArgument( stringConverter, "stringConverter" );
068    }   //  StringBaseComparator()
069
070        /*---------*\
071    ====** Methods **==========================================================
072        \*---------*/
073    /**
074     *  {@inheritDoc}
075     */
076    @Override
077    public int compare( final T o1, final T o2 )
078    {
079        return Integer.signum( Comparator.<String>naturalOrder().compare( m_StringConverter.toString( o1 ), m_StringConverter.toString( o2 ) ) );
080    }   //  compare()
081}
082//  class StringBasedComparator
083
084/*
085 *  End of File
086 */