All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openstreetmap.atlas.utilities.collections.JoinedCollection Maven / Gradle / Ivy

The newest version!
package org.openstreetmap.atlas.utilities.collections;

import java.util.Optional;

import org.openstreetmap.atlas.exception.CoreException;

/**
 * A Joined Collection is simply an array of elements that have been joined together from a
 * ParallelIterator. This object is some what specific to the ParallelIterable object which uses it
 * to join single elements from multiple iterable lists.
 *
 * @author cuthbertm
 */
public class JoinedCollection
{
    private final Object[] elements;

    public JoinedCollection(final int originalSize)
    {
        this.elements = new Object[originalSize];
        for (int index = 0; index < this.elements.length; index++)
        {
            this.elements[index] = null;
        }
    }

    @SuppressWarnings("unchecked")
    public  Type get(final int index) throws ClassCastException, CoreException
    {
        if (index >= 0 && index < this.elements.length)
        {
            return (Type) this.elements[index];
        }
        throw new CoreException("Invalid index {}, needs to be value between -1 and {}", index,
                this.elements.length);
    }

    public  Optional getOption(final int index) throws ClassCastException
    {
        final Type returnType = get(index);
        if (returnType == null)
        {
            return Optional.empty();
        }
        return Optional.of(returnType);
    }

    public  void set(final int index, final Type value) throws CoreException
    {
        if (index >= 0 && index < this.elements.length)
        {
            this.elements[index] = value;
        }
        else
        {
            throw new CoreException("Invalid index {}, needs to be value between -1 and {}", index,
                    this.elements.length);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy