001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.osgi.util;
013
014import java.util.Enumeration;
015import java.util.NoSuchElementException;
016
017/**
018 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
019 */
020public class CompoundEnumeration<E> implements Enumeration<E> {
021
022    private final Enumeration<E>[] enums;
023
024    private int index = 0;
025
026    @SuppressWarnings("unchecked")
027    public CompoundEnumeration(Enumeration<?>[] enums) {
028        this.enums = (Enumeration<E>[]) enums;
029    }
030
031    private boolean next() {
032        while (index < enums.length) {
033            if (enums[index] != null && enums[index].hasMoreElements()) {
034                return true;
035            }
036            index++;
037        }
038        return false;
039    }
040
041    @Override
042    public boolean hasMoreElements() {
043        return next();
044    }
045
046    @Override
047    public E nextElement() {
048        if (!next()) {
049            throw new NoSuchElementException();
050        }
051        return enums[index].nextElement();
052    }
053
054}