Path: blob/master/test/jdk/java/util/Spliterator/SpliteratorLateBindingFailFastHelper.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.AbstractList;24import java.util.ArrayList;25import java.util.Collection;26import java.util.Collections;27import java.util.HashMap;28import java.util.Iterator;29import java.util.LinkedHashMap;30import java.util.List;31import java.util.Map;32import java.util.RandomAccess;33import java.util.Spliterator;34import java.util.function.Consumer;35import java.util.function.Function;36import java.util.function.Supplier;3738class SpliteratorLateBindingFailFastHelper {3940interface Source<T> {41Spliterator<T> spliterator();4243void update();4445default boolean bindOnCharacteristics() {46return false;47}48}4950static class IntSource<T> implements Source<Integer> {51final T b;52final Function<? super T, Spliterator.OfInt> toSpliterator;53final Consumer<T> updater;54final boolean bindOnCharacteristics;5556public IntSource(T b, Function<? super T, Spliterator.OfInt> toSpliterator,57Consumer<T> updater) {58this(b, toSpliterator, updater, false);59}6061public IntSource(T b, Function<? super T, Spliterator.OfInt> toSpliterator,62Consumer<T> updater, boolean bindOnCharacteristics) {63this.b = b;64this.toSpliterator = toSpliterator;65this.updater = updater;66this.bindOnCharacteristics = bindOnCharacteristics;67}6869@Override70public Spliterator.OfInt spliterator() {71return toSpliterator.apply(b);72}7374@Override75public void update() {76updater.accept(b);77}7879@Override80public boolean bindOnCharacteristics() {81return bindOnCharacteristics;82}83}8485static class SpliteratorDataBuilder<T> {86final List<Object[]> data;8788final T newValue;8990final List<T> exp;9192final Map<T, T> mExp;9394SpliteratorDataBuilder(List<Object[]> data, T newValue, List<T> exp) {95this.data = data;96this.newValue = newValue;97this.exp = exp;98this.mExp = createMap(exp);99}100101Map<T, T> createMap(List<T> l) {102Map<T, T> m = new LinkedHashMap<>();103for (T t : l) {104m.put(t, t);105}106return m;107}108109void add(String description, Supplier<Source<?>> s) {110data.add(new Object[]{description, s});111}112113void addCollection(Function<Collection<T>, ? extends Collection<T>> f) {114class CollectionSource implements Source<T> {115final Collection<T> c = f.apply(exp);116117final Consumer<Collection<T>> updater;118119CollectionSource(Consumer<Collection<T>> updater) {120this.updater = updater;121}122123@Override124public Spliterator<T> spliterator() {125return c.spliterator();126}127128@Override129public void update() {130updater.accept(c);131}132}133134String description = "new " + f.apply(Collections.<T>emptyList()).getClass().getName() + ".spliterator() ";135add(description + "ADD", () -> new CollectionSource(c -> c.add(newValue)));136add(description + "REMOVE", () -> new CollectionSource(c -> c.remove(c.iterator().next())));137}138139void addList(Function<Collection<T>, ? extends List<T>> l) {140addCollection(l);141addCollection(l.andThen(list -> list.subList(0, list.size())));142}143144void addMap(Function<Map<T, T>, ? extends Map<T, T>> mapConstructor) {145class MapSource<U> implements Source<U> {146final Map<T, T> m = mapConstructor.apply(mExp);147148final Collection<U> c;149150final Consumer<Map<T, T>> updater;151152MapSource(Function<Map<T, T>, Collection<U>> f, Consumer<Map<T, T>> updater) {153this.c = f.apply(m);154this.updater = updater;155}156157@Override158public Spliterator<U> spliterator() {159return c.spliterator();160}161162@Override163public void update() {164updater.accept(m);165}166}167168Map<String, Consumer<Map<T, T>>> actions = new HashMap<>();169actions.put("ADD", m -> m.put(newValue, newValue));170actions.put("REMOVE", m -> m.remove(m.keySet().iterator().next()));171172String description = "new " + mapConstructor.apply(Collections.<T, T>emptyMap()).getClass().getName();173for (Map.Entry<String, Consumer<Map<T, T>>> e : actions.entrySet()) {174add(description + ".keySet().spliterator() " + e.getKey(),175() -> new MapSource<>(m -> m.keySet(), e.getValue()));176add(description + ".values().spliterator() " + e.getKey(),177() -> new MapSource<>(m -> m.values(), e.getValue()));178add(description + ".entrySet().spliterator() " + e.getKey(),179() -> new MapSource<>(m -> m.entrySet(), e.getValue()));180}181}182}183184static class AbstractRandomAccessListImpl extends AbstractList<Integer> implements RandomAccess {185List<Integer> l;186187AbstractRandomAccessListImpl(Collection<Integer> c) {188this.l = new ArrayList<>(c);189}190191@Override192public boolean add(Integer integer) {193modCount++;194return l.add(integer);195}196197@Override198public Iterator<Integer> iterator() {199return l.iterator();200}201202@Override203public Integer get(int index) {204return l.get(index);205}206207@Override208public boolean remove(Object o) {209modCount++;210return l.remove(o);211}212213@Override214public int size() {215return l.size();216}217218@Override219public List<Integer> subList(int fromIndex, int toIndex) {220return l.subList(fromIndex, toIndex);221}222}223}224225226