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

com.fitbur.mockito.bytebuddy.utility.CompoundList Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.mockito.bytebuddy.utility;

import java.util.AbstractList;
import java.util.Collections;
import java.util.List;

/**
 * A list representation of two lists as a single, compound list.
 *
 * @param  The type of the list's elements.
 */
public class CompoundList extends AbstractList {

    /**
     * The left list.
     */
    private final List left;

    /**
     * The right list.
     */
    private final List right;

    /**
     * Creates a new compound list.
     *
     * @param left  The left list.
     * @param right The right list.
     */
    protected CompoundList(List left, List right) {
        this.left = left;
        this.right = right;
    }

    /**
     * Creates a list of a single element and another list.
     *
     * @param left  The left element.
     * @param right The right list.
     * @param    The type of the list's elements.
     * @return A compound list representing the element and the list.
     */
    public static  List of(S left, List right) {
        return of(Collections.singletonList(left), right);
    }

    /**
     * Creates a list of a list and an element.
     *
     * @param left  The left left.
     * @param right The right element.
     * @param    The type of the list's elements.
     * @return A compound list representing the element and the list.
     */
    public static  List of(List left, S right) {
        return of(left, Collections.singletonList(right));
    }

    /**
     * Creates a list of a left and right list.
     *
     * @param left  The left list.
     * @param right The right list.
     * @param    The type of the list's elements.
     * @return A compound list representing the element and the list.
     */
    public static  List of(List left, List right) {
        return new CompoundList(left, right);
    }

    @Override
    public T get(int index) {
        int leftSize = left.size();
        return leftSize - index > 0
                ? left.get(index)
                : right.get(index - leftSize);
    }

    @Override
    public int size() {
        return left.size() + right.size();
    }
}