com.fitbur.bytebuddy.utility.CompoundList Maven / Gradle / Ivy
package com.fitbur.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 extends T> left;
/**
* The right list.
*/
private final List extends T> right;
/**
* Creates a new compound list.
*
* @param left The left list.
* @param right The right list.
*/
protected CompoundList(List extends T> left, List extends T> 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 extends S> 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 extends S> 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 extends S> left, List extends S> 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();
}
}