/** Builds a list. */ public static <E> List<E> cons(E first, List<? extends E> rest) { return ConsList.of(first, rest); } }
@Override public boolean equals(Object o) { return o == this || o instanceof List && toList().equals(o); }
/** Creates a ConsList. * It consists of an element pre-pended to another list. * If the other list is mutable, creates an immutable copy. */ public static <E> List<E> of(E first, List<? extends E> rest) { if (rest instanceof ConsList || rest instanceof ImmutableList && !rest.isEmpty()) { //noinspection unchecked return new ConsList<>(first, (List<E>) rest); } else { return ImmutableList.<E>builder().add(first).addAll(rest).build(); } }
@Nonnull public <T> T[] toArray(@Nonnull T[] a) { final int s = size(); if (s > a.length) { a = Arrays.copyOf(a, s); } else if (s < a.length) { a[s] = null; } int i = 0; for (ConsList c = this;; c = (ConsList) c.rest) { //noinspection unchecked a[i++] = (T) c.first; if (!(c.rest instanceof ConsList)) { Object[] a2 = c.rest.toArray(); //noinspection SuspiciousSystemArraycopy System.arraycopy(a2, 0, a, i, a2.length); return a; } } }
/** Creates a ConsList. * It consists of an element pre-pended to another list. * If the other list is mutable, creates an immutable copy. */ public static <E> List<E> of(E first, List<? extends E> rest) { if (rest instanceof ConsList || rest instanceof ImmutableList && !rest.isEmpty()) { //noinspection unchecked return new ConsList<>(first, (List<E>) rest); } else { return ImmutableList.<E>builder().add(first).addAll(rest).build(); } }
@Nonnull public <T> T[] toArray(@Nonnull T[] a) { final int s = size(); if (s > a.length) { a = Arrays.copyOf(a, s); } else if (s < a.length) { a[s] = null; } int i = 0; for (ConsList c = this;; c = (ConsList) c.rest) { //noinspection unchecked a[i++] = (T) c.first; if (!(c.rest instanceof ConsList)) { Object[] a2 = c.rest.toArray(); //noinspection SuspiciousSystemArraycopy System.arraycopy(a2, 0, a, i, a2.length); return a; } } }
/** Builds a list. */ public static <E> List<E> cons(E first, List<? extends E> rest) { return ConsList.of(first, rest); } }
@Override public boolean equals(Object o) { return o == this || o instanceof List && toList().equals(o); }
private List<Integer> makeConsList(int start, int end) { List<Integer> list = null; for (int i = end - 1; i >= start; i--) { if (i == end - 1) { list = Collections.singletonList(i); } else { list = ConsList.of(i, list); } } return list; }
public int lastIndexOf(Object o) { return toList().lastIndexOf(o); } }
private List<Integer> makeConsList(int start, int end) { List<Integer> list = null; for (int i = end - 1; i >= start; i--) { if (i == end - 1) { list = Collections.singletonList(i); } else { list = ConsList.of(i, list); } } return list; }
public int lastIndexOf(Object o) { return toList().lastIndexOf(o); } }
@Test public void testCons() { final List<String> abc0 = Arrays.asList("a", "b", "c"); final List<String> abc = ConsList.of("a", ImmutableList.of("b", "c")); assertThat(abc.size(), is(3)); assertThat(abc, is(abc0)); final List<String> abc2 = ConsList.of("a", bc); assertThat(abc2.size(), is(3)); assertThat(abc2, is(abc0)); assertThat(abc2, is(abc0)); final List<String> bc3 = ConsList.of("b", Collections.singletonList("c")); final List<String> abc3 = ConsList.of("a", bc3); assertThat(abc3.size(), is(3)); assertThat(abc3, is(abc0)); final List<String> a = ConsList.of("a", ImmutableList.of()); assertThat(a.size(), is(1)); assertThat(a, is(Collections.singletonList("a")));
@Override public String toString() { return toList().toString(); }
@Test public void testCons() { final List<String> abc0 = Arrays.asList("a", "b", "c"); final List<String> abc = ConsList.of("a", ImmutableList.of("b", "c")); assertThat(abc.size(), is(3)); assertThat(abc, is(abc0)); final List<String> abc2 = ConsList.of("a", bc); assertThat(abc2.size(), is(3)); assertThat(abc2, is(abc0)); assertThat(abc2, is(abc0)); final List<String> bc3 = ConsList.of("b", Collections.singletonList("c")); final List<String> abc3 = ConsList.of("a", bc3); assertThat(abc3.size(), is(3)); assertThat(abc3, is(abc0)); final List<String> a = ConsList.of("a", ImmutableList.of()); assertThat(a.size(), is(1)); assertThat(a, is(Collections.singletonList("a")));
public int indexOf(Object o) { return toList().indexOf(o); }
@Override public int hashCode() { return toList().hashCode(); }
@Override public int hashCode() { return toList().hashCode(); }
public int indexOf(Object o) { return toList().indexOf(o); }
@Override public String toString() { return toList().toString(); }