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

com.hazelcast.org.apache.calcite.util.CompositeList Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.org.apache.calcite.util;

import com.hazelcast.com.google.common.collect.ImmutableList;

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

/**
 * Read-only list that is the concatenation of sub-lists.
 *
 * 

The list is read-only; attempts to call methods such as * {@link #add(Object)} or {@link #set(int, Object)} will throw. * *

Changes to the backing lists, including changes in length, will be * reflected in this list. * *

This class is not thread-safe. Changes to backing lists will cause * unspecified behavior. * * @param Element type */ public class CompositeList extends AbstractList { private final ImmutableList> lists; /** * Creates a CompositeList. * * @param lists Constituent lists */ private CompositeList(ImmutableList> lists) { this.lists = lists; } /** * Creates a CompositeList. * * @param lists Constituent lists * @param Element type * @return List consisting of all lists */ @SafeVarargs public static CompositeList of(List... lists) { //noinspection unchecked return new CompositeList((ImmutableList) ImmutableList.copyOf(lists)); } /** * Creates a CompositeList. * * @param lists Constituent lists * @param Element type * @return List consisting of all lists */ public static CompositeList ofCopy(Iterable> lists) { final ImmutableList> list = ImmutableList.copyOf(lists); return new CompositeList<>(list); } /** * Creates a CompositeList of zero lists. * * @param Element type * @return List consisting of all lists */ public static List of() { return ImmutableList.of(); } /** * Creates a CompositeList of one list. * * @param list0 List * @param Element type * @return List consisting of all lists */ public static List of(List list0) { return list0; } /** * Creates a CompositeList of two lists. * * @param list0 First list * @param list1 Second list * @param Element type * @return List consisting of all lists */ public static CompositeList of(List list0, List list1) { //noinspection unchecked return new CompositeList((ImmutableList) ImmutableList.of(list0, list1)); } /** * Creates a CompositeList of three lists. * * @param list0 First list * @param list1 Second list * @param list2 Third list * @param Element type * @return List consisting of all lists */ public static CompositeList of(List list0, List list1, List list2) { //noinspection unchecked return new CompositeList((ImmutableList) ImmutableList.of(list0, list1, list2)); } public T get(int index) { for (List list : lists) { int nextIndex = index - list.size(); if (nextIndex < 0) { return list.get(index); } index = nextIndex; } throw new IndexOutOfBoundsException(); } public int size() { int n = 0; for (List list : lists) { n += list.size(); } return n; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy