/*
 * @(#)TestEnumSet.java
 *
 * Summary: Test out the features of an EnumSet.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2007-07-29
 */
package com.mindprod.example;

import java.util.EnumSet;

/**
 * Test out the features of an EnumSet.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-07-29
 * @since 2007-07-29
 */
public final class TestEnumSet
    {
    // --------------------------- main() method ---------------------------

    /**
     * test driver.
     *
     * @param args not used
     */
    @SuppressWarnings( { "RedundantTypeArguments" } )
    public static void main( String[] args )
        {
        // create a set by explicitly listing the individual elements
        EnumSet<TreeSpecies> pair =
                EnumSet.<TreeSpecies>of( TreeSpecies.ASPEN, TreeSpecies.CEDAR
                        /* ... */ );
        // Note . comes BEFORE <TreeSpecies>.
        // Or trust the compiler static type inference,
        // shortening to EnumSet.of( ASPEN, CEDAR );

        // prints [ASPEN, CEDAR]
        System.out.println( pair );

        // get all but those selected
        EnumSet<TreeSpecies> most = EnumSet.complementOf( pair );
        // If you leave out the <TreeSpecies> in the line above,
        // you revert to untyped Enums,
        // but the compiler will not warn you!

        // prints [ALDER, BIRCH, FIR, HEMLOCK, PINE, SEQUOIA, SPRUCE]
        System.out.println( most );

        // Iterate over all the elements of the set.
        // Prints ALDER, BIRCH, FIR, HEMLOCK, PINE, SEQUOIA, SPRUCE one per line.
        for ( TreeSpecies s : most )
            {
            System.out.println( s );
            }

        // get empty set
        EnumSet<TreeSpecies> empty = EnumSet.noneOf( TreeSpecies.class );

        // prints []
        System.out.println( empty );

        // get everything
        EnumSet<TreeSpecies> everything = EnumSet.allOf( TreeSpecies.class );

        // prints [ALDER, ASPEN, BIRCH, CEDAR, FIR, HEMLOCK, PINE, SEQUOIA, SPRUCE]
        System.out.println( everything );

        // get just one element
        EnumSet<TreeSpecies> single = EnumSet.of( TreeSpecies.SEQUOIA );

        // prints [SEQUOIA]
        System.out.println( single );

        // This is the most c o m m o n method, the main reason to concoct an EnumSet.
        // You commonly construct an EnumSet combo of things to do, then one by one test if you are supposed to do them.
        // Is CEDAR an element of pair? Prints true
        System.out.println( pair.contains( TreeSpecies.CEDAR ) );

        // is pair a subset of most? Prints false
        // Note containsAll, not contains.
        // You won't get an error message if you forget
        System.out.println( most.containsAll( pair ) );

        // is pair a subset of everything? Prints true
        System.out.println( everything.containsAll( pair ) );
        }
    }

/**
 * top level default scope enum class, in same *.java file as public class. Normally it would be public in its own
 * *.java file.
 */
@SuppressWarnings( { "UnusedDeclaration" } )
enum TreeSpecies
    {
        ALDER, ASPEN, BIRCH, CEDAR, FIR,
        HEMLOCK, PINE, SEQUOIA, SPRUCE
    }