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

com.github.awsjavakit.misc.lists.PartitionedList Maven / Gradle / Ivy

The newest version!
package com.github.awsjavakit.misc.lists;

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

public class PartitionedList implements List> {

  private final List contents;
  private final int partitionSize;
  private final int numberOfPartitions;

  public PartitionedList(List contents, int partitionSize) {
    this.contents = contents;
    this.partitionSize = partitionSize;
    this.numberOfPartitions = calculateNumberOfPartitions(contents, partitionSize);
  }

  @Override
  public int size() {
    return this.numberOfPartitions;
  }

  @Override
  public boolean isEmpty() {
    return contents.isEmpty();
  }

  @Override
  public boolean contains(Object o) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Iterator> iterator() {
    return new PartitionedListIterator<>(this);
  }

  @Override
  public Object[] toArray() {
    var lists = this.stream().toList();
    return lists.toArray();
  }

  @Override
  public  T[] toArray(T[] a) {
    var lists = this.stream().toList();
    return lists.toArray(a);
  }

  @Override
  public boolean add(List ts) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean remove(Object o) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean containsAll(Collection c) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean addAll(Collection> c) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean addAll(int index, Collection> c) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean removeAll(Collection c) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean retainAll(Collection c) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void clear() {
    throw new UnsupportedOperationException();
  }

  @Override
  public List get(int index) {
    var fromIndex = calculatePartitionStart(index);
    var untilIndex = calculatePartitionEndExclusive(index);
    return contents.subList(fromIndex, untilIndex);
  }

  @Override
  public List set(int index, List element) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void add(int index, List element) {
    throw new UnsupportedOperationException();
  }

  @Override
  public List remove(int index) {
    throw new UnsupportedOperationException();
  }

  @Override
  public int indexOf(Object o) {
    throw new UnsupportedOperationException();
  }

  @Override
  public int lastIndexOf(Object o) {
    throw new UnsupportedOperationException();
  }

  @Override
  public ListIterator> listIterator() {
    throw new UnsupportedOperationException();
  }

  @Override
  public ListIterator> listIterator(int index) {
    throw new UnsupportedOperationException();
  }

  @Override
  public List> subList(int fromIndex, int toIndex) {
    throw new UnsupportedOperationException();
  }

  private int calculateNumberOfPartitions(List contents, int partitionSize) {
    return (int) Math.ceil((double) contents.size() / (double) partitionSize);
  }

  private int calculatePartitionEndExclusive(int index) {
    var candidate = partitionSize * (index + 1);
    return Math.min(candidate, contents.size());

  }

  private int calculatePartitionStart(int partitionIndex) {
    if (partitionIndex >= this.size()) {
      throw indexOutOfBounds(partitionIndex);
    }
    return partitionIndex * partitionSize;
  }

  private IndexOutOfBoundsException indexOutOfBounds(int partitionIndex) {
    return new IndexOutOfBoundsException(formatMessage(partitionIndex));
  }

  private String formatMessage(int partitionIndex) {
    return String.format("Index %d is out of bounds for length %d", partitionIndex, size());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy