package com.wizzardo.tools.collections;

import java.util.*;

public class Range implements Iterable<Integer>, List<Integer> {
    private int from, to;

    /**
     * @param from - inclusive
     * @param to   - exclusive
     */
    public Range(int from, int to) {
        if (from > to) {
            throw new IllegalArgumentException("from must be lower or equal then to. " + from + " - " + to);
        }
        this.from = from;
        this.to = to;
    }

    @Override
    public int size() {
        return to - from;
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public Integer get(int index) {
        if (from + index >= to || index < 0)
            throw new IndexOutOfBoundsException("Index " + index + " is out of this range [" + from + ", " + to + ")");

        return from + index;
    }

    @Override
    public boolean contains(Object o) {
        if (o instanceof Integer) {
            Integer i = (Integer) o;
            return i >= from && i < to;
        }
        return false;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int cur = from;

            @Override
            public boolean hasNext() {
                return cur < to;
            }

            @Override
            public Integer next() {
                if (!hasNext())
                    throw new NoSuchElementException("iteration has no more elements");

                return cur++;
            }

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

    @Override
    public Object[] toArray() {
        Integer[] arr = new Integer[size()];
        for (int i = from; i < to; i++) {
            arr[i - from] = i;
        }
        return arr;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        if (a.getClass().getName().equals("[Ljava.lang.Integer;")) {
            Integer[] arr;
            if (a.length == 0)
                arr = new Integer[size()];
            else
                arr = (Integer[]) a;
            for (int i = from; i < to; i++) {
                arr[i - from] = i;
            }
            return (T[]) arr;
        }
        return a;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        boolean b = true;
        for (Object i : c) {
            b &= contains(i);
            if (!b) {
                return false;
            }
        }
        return b;
    }

    @Override
    public List<Integer> subList(int fromIndex, int toIndex) {
        return new Range(from + fromIndex, fromIndex + toIndex);
    }

    @Override
    public int indexOf(Object o) {
        if (o.getClass() == Integer.class) {
            int i = (Integer) o;
            if (i >= from && i < to) {
                return i - from;
            }
        }
        return -1;
    }

    public String toString() {
        return from + ".." + to;
    }


    @Override
    public int lastIndexOf(Object o) {
        return indexOf(o);
    }

    @Override
    public boolean add(Integer integer) {
        throw new UnsupportedOperationException();
    }

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

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

    @Override
    public boolean addAll(int index, Collection<? extends Integer> 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 Integer set(int index, Integer element) {
        throw new UnsupportedOperationException();
    }

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

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

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

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

@@ -1932,81 +1932,8 @@
 ) %7B%0A
-        if (a.getClass().getName().equals(%22%5BLjava.lang.Integer;%22)) %7B%0A    
     
@@ -1947,28 +1947,24 @@
 eger%5B%5D arr;%0A
-    
         if (
@@ -1976,17 +1976,18 @@
 gth 
-== 0)%0A   
+%3C size())%0A
     
@@ -1990,25 +1990,24 @@
             
- 
 arr = new In
@@ -2033,21 +2033,13 @@
     
-    
 else%0A
-    
     
@@ -2067,20 +2067,17 @@
 r%5B%5D) a;%0A
-    
+%0A
         
@@ -2102,36 +2102,32 @@
  i %3C to; i++) %7B%0A
-    
             arr%5B
@@ -2145,34 +2145,26 @@
  i;%0A        
-    %7D%0A    
+%7D%0A
         retu
@@ -2181,36 +2181,8 @@
 rr;%0A
-        %7D%0A        return a;%0A
     
@@ -2503,32 +2503,148 @@
  int toIndex) %7B%0A
+        if (fromIndex %3C 0 %7C%7C toIndex %3C 0 %7C%7C from + toIndex %3E to)%0A            throw new IndexOutOfBoundsException();%0A
         return n
@@ -2674,21 +2674,16 @@
 ex, from
-Index
  + toInd

fix tests