/* * [TestEnumSet.java] * * Summary: Test out the features of an EnumSet. * * Copyright: (c) 2009-2012 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.7+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2007-07-29 */ package com.mindprod.example; import java.util.EnumSet; import static java.lang.System.out; /** * 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] 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] 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 ) { out.println( s ); } // get empty set EnumSet<TreeSpecies> empty = EnumSet.noneOf( TreeSpecies.class ); // prints [] out.println( empty ); // get everything EnumSet<TreeSpecies> everything = EnumSet.allOf( TreeSpecies.class ); // prints [ALDER, ASPEN, BIRCH, CEDAR, FIR, HEMLOCK, PINE, SEQUOIA, SPRUCE] out.println( everything ); // get just one element EnumSet<TreeSpecies> single = EnumSet.of( TreeSpecies.SEQUOIA ); // prints [SEQUOIA] 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 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 out.println( most.containsAll( pair ) ); // is pair a subset of everything? Prints true out.println( everything.containsAll( pair ) ); } } /** * top level default scope enum class, in same *.java file as import static java.lang.System.*; 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 }