/*
 * @(#)TestArraysAsListBacking.java
 *
 * Summary: Explore Arrays.asList(int[]) backing feature.
 *
 * 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-10-03
 */
package com.mindprod.example;

import java.util.Arrays;
import java.util.List;

/**
 * Explore Arrays.asList(int[]) backing feature.
 * <p/>
 * When you use Arrays.asList to create a fixed size List from an array, changes to the List are reflected in the
 * backing array.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-10-03
 * @since 2007-10-03
 */
public class TestArraysAsListBacking
    {
    // ------------------------------ CONSTANTS ------------------------------

    private static final int DECKSIZE = 52;

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

    /**
     * main method.
     *
     * @param args the command line argument, not used
     */
    public static void main( String[] args )
        {
        Integer deck[] = new Integer[DECKSIZE];

        for ( int i = 0; i < DECKSIZE; i++ )
            {
            // autobox
            deck[ i ] = i;
            }
        List<Integer> list = Arrays.asList( deck );
        System.out.println( list.getClass() );
        // output is:
        // class java.util.Arrays$ArrayList
        // as expected if you look at the asList source code.

        System.out.println( list.size() );
        // output is:
        // 52
        // as expected

        Object elem = list.get( 0 );
        System.out.println( elem.getClass() );
        // class java.lang.Integer
        // as you would expect

        // test if backing array store truly tracks as advertised.
        list.set( 0, -1 );
        System.out.println( list.get( 0 ) );
        // output is:
        // -1
        // as expected

        System.out.println( deck[ 0 ] );
        // output is:
        // -1
        // as advertised!

        // try growing the List improperly.
        list.add( -2 );
        // Throws UnsupportedOperationException
        }
    }