|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Table structures for SciJava. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2012 - 2018 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research. |
| 7 | + * %% |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * #L% |
| 29 | + */ |
| 30 | + |
| 31 | +package org.scijava.table; |
| 32 | + |
| 33 | +import java.lang.reflect.Array; |
| 34 | +import java.util.AbstractList; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.List; |
| 37 | +import java.util.ListIterator; |
| 38 | +import java.util.Objects; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +/** |
| 42 | + * Simple default implementations of {@link Collection} and {@link List} |
| 43 | + * methods. |
| 44 | + * |
| 45 | + * @author Curtis Rueden |
| 46 | + */ |
| 47 | +final class SimpleCollections { |
| 48 | + |
| 49 | + private SimpleCollections() { |
| 50 | + // NB: Prevent instantiation of utility class. |
| 51 | + } |
| 52 | + |
| 53 | + static Object[] toArray(final List<?> list) { |
| 54 | + final Object[] array = new Object[list.size()]; |
| 55 | + for (int i = 0; i < array.length; i++) |
| 56 | + array[i] = list.get(i); |
| 57 | + return array; |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + static <A> A[] toArray(final List<?> list, final A[] a) { |
| 62 | + final A[] array = a.length >= list.size() ? a : // |
| 63 | + (A[]) Array.newInstance(a.getClass().getComponentType(), list.size()); |
| 64 | + for (int i = 0; i < list.size(); i++) |
| 65 | + array[i] = (A) list.get(i); |
| 66 | + return array; |
| 67 | + } |
| 68 | + |
| 69 | + static <E> boolean add(final List<E> list, final E o) { |
| 70 | + list.add(list.size(), o); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + static boolean remove(final List<?> list, final Object o) { |
| 75 | + final int index = list.indexOf(o); |
| 76 | + if (index < 0) return false; |
| 77 | + list.remove(index); |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + static boolean containsAll(final Collection<?> collection, |
| 82 | + final Collection<?> c) |
| 83 | + { |
| 84 | + for (final Object o : c) |
| 85 | + if (!collection.contains(o)) return false; |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + static <E> boolean addAll(final Collection<E> collection, |
| 90 | + final Collection<? extends E> c) |
| 91 | + { |
| 92 | + boolean changed = false; |
| 93 | + for (final E o : c) |
| 94 | + changed |= collection.add(o); |
| 95 | + return changed; |
| 96 | + } |
| 97 | + |
| 98 | + static <E> boolean addAll(final List<E> list, final int index, |
| 99 | + final Collection<? extends E> c) |
| 100 | + { |
| 101 | + int i = index; |
| 102 | + for (final E o : c) |
| 103 | + list.add(i++, o); |
| 104 | + return c.size() > 0; |
| 105 | + } |
| 106 | + |
| 107 | + static boolean removeAll(final Collection<?> collection, |
| 108 | + final Collection<?> c) |
| 109 | + { |
| 110 | + boolean changed = false; |
| 111 | + for (final Object o : c) |
| 112 | + changed |= collection.remove(o); |
| 113 | + return changed; |
| 114 | + } |
| 115 | + |
| 116 | + static boolean retainAll(final Collection<?> collection, |
| 117 | + final Collection<?> c) |
| 118 | + { |
| 119 | + final List<?> absent = collection.stream() // |
| 120 | + .filter(o -> !c.contains(o)) // |
| 121 | + .collect(Collectors.toList()); |
| 122 | + return collection.removeAll(absent); |
| 123 | + } |
| 124 | + |
| 125 | + static int indexOf(final List<?> list, final Object o) { |
| 126 | + for (int i = 0; i < list.size(); i++) |
| 127 | + if (Objects.equals(list.get(i), o)) return i; |
| 128 | + return -1; |
| 129 | + } |
| 130 | + |
| 131 | + static int lastIndexOf(final List<?> list, final Object o) { |
| 132 | + for (int i = list.size() - 1; i >= 0; i--) |
| 133 | + if (Objects.equals(list.get(i), o)) return i; |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + static <E> ListIterator<E> listIterator(final List<E> list, final int index) { |
| 138 | + return new ListIterator<E>() { |
| 139 | + |
| 140 | + private int i = index; |
| 141 | + private int last = -1; |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean hasNext() { |
| 145 | + return i < list.size(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public E next() { |
| 150 | + return list.get(last = i++); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean hasPrevious() { |
| 155 | + return i > 0; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public E previous() { |
| 160 | + return list.get(last = --i); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public int nextIndex() { |
| 165 | + return i; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public int previousIndex() { |
| 170 | + return i - 1; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void remove() { |
| 175 | + if (last < 0) throw new IllegalStateException(); |
| 176 | + list.remove(last); |
| 177 | + last = -1; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void set(final E e) { |
| 182 | + if (last < 0) throw new IllegalStateException(); |
| 183 | + list.set(last, e); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void add(final E e) { |
| 188 | + list.add(i++, e); |
| 189 | + last = -1; |
| 190 | + } |
| 191 | + }; |
| 192 | + } |
| 193 | + |
| 194 | + static <E> List<E> subList(final List<E> list, final int fromIndex, |
| 195 | + final int toIndex) |
| 196 | + { |
| 197 | + return new AbstractList<E>() { |
| 198 | + |
| 199 | + @Override |
| 200 | + public E get(final int index) { |
| 201 | + return list.get(index + fromIndex); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int size() { |
| 206 | + return toIndex - fromIndex; |
| 207 | + } |
| 208 | + }; |
| 209 | + } |
| 210 | +} |
0 commit comments