Demonstrates the use of final collections : Collections « Collections Data Structure « Java






Demonstrates the use of final collections

   
/*
 *     file: FinalCollections.java
 *  package: oreilly.hcj.finalstory
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */

import java.awt.Color;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Demonstrates the use of final collections.
 * 
 * @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 * @version $Revision: 1.3 $
 */
public class FinalCollections {
  /**
   * Main method for demonstration.
   * 
   * @param args
   *          Command line arguments.
   */
  public static final void main(final String[] args) {
    Rainbow.someMethod();
    System.out.println("------------------");
    try {
      RainbowBetter.someMethod();
    } catch (final UnsupportedOperationException ex) {
      ex.printStackTrace(System.out);
      System.out.println("Told you it would exception =)");
    }
  }

  /**
   * A class that demonstrates the rainbow.
   * 
   * @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   * @version $Revision: 1.3 $
   */
  public static class Rainbow {
    /** The set of valid colors of the rainbow. */
    public static final Set VALID_COLORS;

    static {
      VALID_COLORS = new HashSet();
      VALID_COLORS.add(Color.red);
      VALID_COLORS.add(Color.orange);
      VALID_COLORS.add(Color.yellow);
      VALID_COLORS.add(Color.green);
      VALID_COLORS.add(Color.blue);
      VALID_COLORS.add(Color.decode("#4B0082")); // indigo
      VALID_COLORS.add(Color.decode("#8A2BE2")); // violet
    }

    /**
     * A demo method.
     */
    public static final void someMethod() {
      Set colors = Rainbow.VALID_COLORS;
      colors.add(Color.black); // <= logic error but allowed by compiler
      System.out.println(colors);
    }
  }

  /**
   * A better version of the rainbow class using an unmodifiable set.
   * 
   * @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   * @version $Revision: 1.3 $
   */
  public static class RainbowBetter {
    /** The valid colors of the rainbow. */
    public static final Set VALID_COLORS;

    static {
      Set temp = new HashSet();
      temp.add(Color.red);
      temp.add(Color.orange);
      temp.add(Color.yellow);
      temp.add(Color.green);
      temp.add(Color.blue);
      temp.add(Color.decode("#4B0082")); // indigo
      temp.add(Color.decode("#8A2BE2")); // violet
      VALID_COLORS = Collections.unmodifiableSet(temp);
    }

    /**
     * Some demo method.
     */
    public static final void someMethod() {
      Set colors = RainbowBetter.VALID_COLORS;
      colors.add(Color.black); // <= exception here
      System.out.println(colors);
    }
  }
}

/* ########## End of File ########## */

   
    
    
  








Related examples in the same category

1.Collections.min with Comparator
2.Minimum and maximum number in array
3.Shuffling the Elements of a List or Array: use Collections.shuffle() to randomly reorder the elements in a list
4.Shuffle the elements in the array
5.Create an empty collection object
6.Collections.reverse
7.Use Collections.sort to sort custom class and user defined Comparator
8.Collections.shuffle to shuffle a list
9.Collections.fill
10.Use Collections.shuffle to shuffle listUse Collections.shuffle to shuffle list
11.Making a Collection Read-Only
12.Shuffle generic list
13.Create List containing n Copies of Specified Object Example
14.Create and demonstrate an immutable collection.
15.Sort and Search a LinkedList.
16.Finding an Element in a Sorted List
17.Use reverse(), rotate(), and shuffle().
18.This program demonstrates the random shuffle and sort algorithmsThis program demonstrates the random shuffle and sort algorithms