package com.mindprod.example;

/**
 * Enum demonstrating use of an alias value. See a more sophisticated approach in the Rodents class, that allows
 * multiple aliases and uses a fast HashMap lookup.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
@SuppressWarnings( { "UnusedDeclaration", "WeakerAccess" } )
public enum BreedA
    {
        /**
         * Dachshund smooth or curly.
         */
        DACHSHUND( "wiener dog" ),
        /**
         * Dalmatian, allow spelling error.
         *
         * @noinspection WeakerAccess
         */
        DALMATIAN( "dalmation"/* do not correct spelling! */ ),
        /**
         * Labrador retrievers, allow short name.
         */
        LABRADOR( "lab" );

    /**
     * alternate external name for this enum constant, we allow just one. It would be easy to extend this to
     * an array of aliases using String... on the constructor.
     */
    private final String alias;
    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * convert alias string to equivalent canonical enum constant, like valueOf but accepts aliases matching the
     * alias name too, and does not care about case.
     *
     * @param s alias as string.
     * @return equivalent BreedA enum constant.
     * @noinspection WeakerAccess
     */
    public static BreedA valueOfAlias( String s )
        {
        try
            {
            return valueOf( s.toUpperCase() );
            }
        catch ( IllegalArgumentException e )
            {
            // usual method failed, try looking up alias
            // This seems long winded, why no HashSet?
            // Because Java won't let me access a static common
            // lookup in the enum constructors. There are problems with initialisation
            // enum constants at static init time.
            for ( BreedA candidateEnum : BreedA.values() )
                {
                if ( candidateEnum.getAlias().equalsIgnoreCase( s ) )
                    {
                    return candidateEnum;
                    }
                }
            // fell out the bottom of search over all enums and aliases
            // give up.
            throw new IllegalArgumentException( "unknown Breed: " + s );
            }
        }

    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * get alternative name for this breed.
     *
     * @return alias name for this breed.
     * @noinspection WeakerAccess
     */
    public String getAlias()
        {
        return alias;
        }

    // --------------------------- CONSTRUCTORS ---------------------------

    /**
     * constructor.
     *
     * @param alias, an alternate name for this breed.
     */
    BreedA( String alias )
        {
        this.alias = alias;
        }

    // --------------------------- main() method ---------------------------

    /**
     * Test harness.
     *
     * @param args not used.
     */
    @SuppressWarnings( { "UnusedParameters" } )
    public static void main( String[] args )
        {
        // prints DALMATIAN DALMATIAN DALMATIAN DACHSHUND
        BreedA myDog = BreedA.DALMATIAN;
        System.out.println( myDog );

        myDog = BreedA.valueOf( "DALMATIAN" );
        System.out.println( myDog );

        myDog = BreedA.valueOfAlias( "dalmation" );
        System.out.println( myDog );

        myDog = BreedA.valueOfAlias( "Wiener Dog" );
        System.out.println( myDog );
        }
    }