package me.grdryn.lisztomania;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BalancedSubList<E> implements List<E> {

    private static final Logger logger = LoggerFactory
            .getLogger(BalancedSubList.class);

    private final List<E> backingList;

    public BalancedSubList(final List<E> originalList, final int percentage) {

        backingList = new ArrayList<>();

        int indexOfOriginalList = 0;
        final double percentageValue = (originalList.size() * (percentage / 100.0));
        long elementsForPercentage;

        if (percentageValue < 1.0) {
            elementsForPercentage = 1;
        } else {
            elementsForPercentage = Math.round(percentageValue);
        }

        while (backingList.size() < elementsForPercentage) {
            backingList.add(originalList.get(indexOfOriginalList++));
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder((this.size() * 3) + 3);

        sb.append('[');
        for (final E e : this) {
            sb.append(e).append(", ");
        }

        final int indexOfTrailingComma = sb.lastIndexOf(", ");
        if (indexOfTrailingComma != -1) {
            sb.delete(indexOfTrailingComma, indexOfTrailingComma + 2);
        }
        sb.append(']');

        return sb.toString();
    }

    @Override
    public boolean add(final E arg0) {
        return backingList.add(arg0);
    }

    @Override
    public void add(final int index, final E element) {
        backingList.add(index, element);
    }

    @Override
    public boolean addAll(final Collection<? extends E> arg0) {
        return backingList.addAll(arg0);
    }

    @Override
    public boolean addAll(final int arg0, final Collection<? extends E> arg1) {
        return backingList.addAll(arg0, arg1);
    }

    @Override
    public void clear() {
        backingList.clear();

    }

    @Override
    public boolean contains(final Object arg0) {
        return backingList.contains(arg0);
    }

    @Override
    public boolean containsAll(final Collection<?> arg0) {
        return backingList.containsAll(arg0);
    }

    @Override
    public E get(final int arg0) {
        return backingList.get(arg0);
    }

    @Override
    public int indexOf(final Object arg0) {
        return backingList.indexOf(arg0);
    }

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

    @Override
    public Iterator<E> iterator() {
        return backingList.iterator();
    }

    @Override
    public int lastIndexOf(final Object arg0) {
        return backingList.lastIndexOf(arg0);
    }

    @Override
    public ListIterator<E> listIterator() {
        return backingList.listIterator();
    }

    @Override
    public ListIterator<E> listIterator(final int arg0) {
        return backingList.listIterator(arg0);
    }

    @Override
    public boolean remove(final Object arg0) {
        return backingList.remove(arg0);
    }

    @Override
    public E remove(final int arg0) {
        return backingList.remove(arg0);
    }

    @Override
    public boolean removeAll(final Collection<?> arg0) {
        return backingList.removeAll(arg0);
    }

    @Override
    public boolean retainAll(final Collection<?> arg0) {
        return backingList.retainAll(arg0);
    }

    @Override
    public E set(final int arg0, final E arg1) {
        return backingList.set(arg0, arg1);
    }

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

    @Override
    public List<E> subList(final int arg0, final int arg1) {
        return backingList.subList(arg0, arg1);
    }

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

    @Override
    public <T> T[] toArray(final T[] arg0) {
        return backingList.toArray(arg0);
    }

}

@@ -553,77 +553,79 @@
     
+f
 in
-t indexOfOriginalList = 0;%0A        final double percentageValue = (
+al long elementsForPercentage = getBackingListSize(%0A                
 orig
@@ -643,12 +643,10 @@
 ze()
+,
  
-* (
 perc
@@ -655,28 +655,20 @@
 tage
- / 100.0)
 );%0A
+%0A
         
 long
@@ -663,20 +663,49 @@
         
-long
+populateBackingList(originalList,
  element
@@ -722,72 +722,158 @@
 tage
+)
 ;%0A
-%0A        if (percentageValue %3C 1.0) %7B%0A            elementsForP
+    %7D%0A%0A    private long getBackingListSize(final int originalSize, final int percentage) %7B%0A        final double percentageValue = originalSize * (p
 erce
@@ -882,11 +882,16 @@
 age 
-=
+/
  1
+00.0)
 ;%0A  
@@ -900,52 +900,44 @@
     
-%7D else %7B%0A            elementsForPercentage =
+return (percentageValue %3C 1.0) ? 1 :
  Mat
@@ -962,29 +962,155 @@
 Value);%0A    
+%7D%0A%0A
     
-%7D
+private void populateBackingList(final List%3CE%3E inList,%0A            final long numElements) %7B%0A%0A        int indexOfInList = 0;
 %0A%0A        wh
@@ -1135,37 +1135,27 @@
 ize() %3C 
-e
+numE
 lements
-ForPercentage
 ) %7B%0A    
@@ -1178,24 +1178,18 @@
 ist.add(
-original
+in
 List.get
@@ -1200,16 +1200,10 @@
 exOf
-Original
+In
 List

Refactor with great confidence