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

What this is

This file 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.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.storagemodel;

import java.util.*;
import java.io.*;
import java.lang.reflect.Array;

import javax.jmi.reflect.*;

import org.netbeans.mdr.handlers.BaseObjectHandler;
import org.netbeans.mdr.persistence.*;
import org.netbeans.mdr.util.*;

/**
 * Instances of CompositeCollection are immutable collections uniting
 * several other collections. The methods of this class must be called in the following
 * order:
 *
 * 

    *
  1. the constructor {@link #CompositeCollection()} to create an instance
  2. *
  3. {@link addCollection(Collection} to add a collection forming a part of * the current collection
  4. *
  5. any accessor method
  6. *

* *

It is not allowed to add further collections after any of the accessors has * been called.

* *

Because the collections added using @link addCollection(Collection} are added * as is and their members are accessed only, when to be returned by methods of * this class resp. its {@link Iterator}, instances of CompositeCollection * are live if and only if all of the composed collections are live.

* *

[XXX]: Put this class into org.netbeans.mdr.util * package.

* * @author Martin Matula */ public class CompositeCollection implements Collection { private final ArrayList innerCollections = new ArrayList(); private volatile boolean canChange = true; public CompositeCollection() { } /** * Adds a new collection. Can only be called before any accessor call. */ public void addCollection(Collection collection) { if (canChange) { innerCollections.add(collection); } else { throw new DebugException("Set of collections cannot be changed after it was accessed."); } } public int size() { int size = 0; canChange = false; for (Iterator it = innerCollections.iterator(); it.hasNext();) { size += ((Collection) it.next()).size(); } return size; } public boolean contains(Object obj) { canChange = false; for (Iterator it = innerCollections.iterator(); it.hasNext();) { if (((Collection) it.next()).contains(obj)) return true; } return false; } public Iterator iterator() { canChange = false; ArrayList innerIterators = new ArrayList(); for (Iterator it = innerCollections.iterator(); it.hasNext();) { innerIterators.add(((Collection) it.next()).iterator()); } return new CompositeIterator(innerIterators.iterator()); } public boolean isEmpty() { canChange = false; for (Iterator it = innerCollections.iterator(); it.hasNext();) { if (!((Collection) it.next()).isEmpty()) return false; } return true; } public boolean containsAll(Collection collection) { for (Iterator it = collection.iterator(); it.hasNext();) { if (!contains(it.next())) return false; } return true; } public Object[] toArray(Object[] obj) { canChange = false; ArrayList value = new ArrayList(); for (Iterator it = innerCollections.iterator(); it.hasNext();) { value.addAll((Collection) it.next()); } return value.toArray(obj); } public Object[] toArray() { return toArray(new Object[0]); } /** * operation not supported */ public void clear() { throw new UnsupportedOperationException(); } /** * operation not supported */ public boolean addAll(Collection collection) { throw new UnsupportedOperationException(); } /** * operation not supported */ public boolean remove(Object obj) { throw new UnsupportedOperationException(); } /** * operation not supported */ public boolean add(Object obj) { throw new UnsupportedOperationException(); } /** * operation not supported */ public boolean retainAll(Collection collection) { throw new UnsupportedOperationException(); } /** * operation not supported */ public boolean removeAll(Collection collection) { throw new UnsupportedOperationException(); } /* --------------------------------------------------------------------- */ /* -- CompositeIterator (inner class) ---------------------------------- */ /* --------------------------------------------------------------------- */ protected class CompositeIterator implements Iterator { private final Iterator innerIterators; private Iterator currentIterator; protected CompositeIterator(Iterator innerIterators) { this.innerIterators = innerIterators; this.currentIterator = (Iterator) innerIterators.next(); hasNext(); } public boolean hasNext() { while (innerIterators.hasNext()) { if (currentIterator.hasNext()) { return true; } else { currentIterator = (Iterator) innerIterators.next(); } } return currentIterator.hasNext(); } public Object next() { Object result = currentIterator.next(); hasNext(); return result; } /** * operation not supported */ public void remove() { throw new UnsupportedOperationException(); } } }
... 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.