alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Collections example source code file (TestFastArrayList1.java)

This example Commons Collections source code file (TestFastArrayList1.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Collections tags/keywords

a, a, b, b, c, c, concurrentmodificationexception, fastarraylist, iterator, list, list, n, testfastarraylist1, util, z

The Commons Collections TestFastArrayList1.java source code

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.commons.collections;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import junit.framework.Test;

/**
 * Test FastArrayList implementation in <strong>fast mode.
 * 
 * @version $Revision: 646780 $ $Date: 2008-04-10 13:48:07 +0100 (Thu, 10 Apr 2008) $
 *
 * @author Jason van Zyl
 */
public class TestFastArrayList1 extends TestFastArrayList {
    
    public TestFastArrayList1(String testName) {
        super(testName);
    }

    public static Test suite() {
        return BulkTest.makeSuite(TestFastArrayList1.class);
    }

    public static void main(String args[]) {
        String[] testCaseName = { TestFastArrayList1.class.getName()};
        junit.textui.TestRunner.main(testCaseName);
    }

    public void setUp() {
        list = (ArrayList) makeEmptyList();
    }

    public List makeEmptyList() {
        FastArrayList fal = new FastArrayList();
        fal.setFast(true);
        return (fal);
    }

    public void testIterateModify1() {
        List list = makeEmptyList();
        list.add("A");
        list.add("B");
        list.add("C");
        assertEquals(3, list.size());
        
        Iterator it = list.iterator();
        assertEquals("A", it.next());
        assertEquals(3, list.size());
        list.add(1, "Z");
        assertEquals(4, list.size());
        assertEquals("B", it.next());
        assertEquals("C", it.next());
        assertEquals(false, it.hasNext());
    }

    public void testIterateModify2() {
        List list = makeEmptyList();
        list.add("A");
        list.add("B");
        list.add("C");
        assertEquals(3, list.size());
        
        ListIterator it = list.listIterator();
        assertEquals("A", it.next());
        it.add("M");  // change via Iterator interface
        assertEquals(4, list.size());
        list.add(2, "Z");  // change via List interface
        assertEquals(5, list.size());
        assertEquals("B", it.next());
        try {
            it.set("N"); // fails as previously changed via List interface
            fail();
        } catch (ConcurrentModificationException ex) {}
        try {
            it.remove();
            fail();
        } catch (ConcurrentModificationException ex) {}
        try {
            it.add("N");
            fail();
        } catch (ConcurrentModificationException ex) {}
        assertEquals("C", it.next());
        assertEquals(false, it.hasNext());
    }

}

Other Commons Collections examples (source code examples)

Here is a short list of links related to this Commons Collections TestFastArrayList1.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.