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

io.vlingo.common.collection.ResettableReadOnlyList Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.common.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Implements a {@code List} with the express purpose of reuse,
 * meaning there is no new memory allocation when you need to wrap
 * a {@code T[]} with a {@code List} and access via {@code Iterator}.
 * This abstraction is not meant for general purpose use because it is
 * built for one special use case, to make read-only access to {@code List}
 * as fast as possible and produce very little garbage. Specifically this
 * fits use within {@code Actor} implementations while handling a single
 * message delivery. All other uses are at your own risk.
 * 
 * @param  the type of elements
 */
public class ResettableReadOnlyList implements List {
  private T[] all;
  private final ResettableReadOnlyIterator iterator;
  private final T[] one;

  @SuppressWarnings("unchecked")
  public ResettableReadOnlyList() {
    this.one = (T[]) new Object[1];
    this.iterator = new ResettableReadOnlyIterator();
  }

  @SuppressWarnings("unchecked")
  public  List asList() {
    return (List) this;
  }

  public void wrap(final T[] all) {
    this.all = all;
    this.iterator.current = 0;
  }

  public void wrap(final T all) {
    one[0] = all;
    this.all = one;
    this.iterator.current = 0;
  }

  @Override
  public int size() {
    return all.length;
  }

  @Override
  public boolean isEmpty() {
    return all.length == 0;
  }

  @Override
  public boolean contains(final Object o) {
    throw new UnsupportedOperationException("Must override");
  }

  @Override
  public Iterator iterator() {
    this.iterator.current = 0;
    return iterator;
  }

  @Override
  public Object[] toArray() {
    return all;
  }

  @Override
  public  A[] toArray(final A[] a) {
    System.arraycopy(all, 0, a, 0, Math.min(all.length, a.length));
    return a;
  }

  @Override
  public boolean add(final T e) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public boolean remove(final Object o) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public boolean containsAll(final Collection c) {
    throw new UnsupportedOperationException("Must override");
  }

  @Override
  public boolean addAll(Collection c) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public boolean addAll(final int index, final Collection c) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public boolean removeAll(final Collection c) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public boolean retainAll(final Collection c) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public void clear() {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public T get(final int index) {
    return all[index];
  }

  @Override
  public T set(final int index, final T element) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public void add(final int index, final T element) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public T remove(final int index) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public int indexOf(final Object o) {
    throw new UnsupportedOperationException("Must override");
  }

  @Override
  public int lastIndexOf(final Object o) {
    throw new UnsupportedOperationException("Must override");
  }

  @Override
  public ListIterator listIterator() {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public ListIterator listIterator(final int index) {
    throw new UnsupportedOperationException("Read-only access");
  }

  @Override
  public List subList(final int fromIndex, final int toIndex) {
    throw new UnsupportedOperationException("Must override");
  }

  private final class ResettableReadOnlyIterator implements Iterator {
    private int current;

    @Override
    public boolean hasNext() {
      return current < size();
    }

    @Override
    public T next() {
      return get(current++);
    }

    private ResettableReadOnlyIterator() {
      this.current = 0;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy