Advertisement
tomdodd4598

Untitled

Oct 3rd, 2023
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. public class InventoryStackList extends NonNullList<ItemStack> {
  2.    
  3.     public InventoryStackList() {
  4.         super();
  5.     }
  6.    
  7.     public InventoryStackList(List<ItemStack> other) {
  8.         super(other, null);
  9.     }
  10.    
  11.     @Override
  12.     public List<ItemStack> subList(int fromIndex, int toIndex) {
  13.         return new SubList<>(this, fromIndex, toIndex);
  14.     }
  15.    
  16.     class SubList<E> extends NonNullList<E> {
  17.        
  18.         private final AbstractList<E> internal;
  19.         private final int offset;
  20.         private int size;
  21.        
  22.         SubList(AbstractList<E> list, int fromIndex, int toIndex) {
  23.             if (fromIndex < 0) {
  24.                 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
  25.             }
  26.             if (toIndex > list.size()) {
  27.                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
  28.             }
  29.             if (fromIndex > toIndex) {
  30.                 throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
  31.             }
  32.             internal = list;
  33.             offset = fromIndex;
  34.             size = toIndex - fromIndex;
  35.             // this.modCount = internal.modCount;
  36.         }
  37.        
  38.         @Override
  39.         public E set(int index, E element) {
  40.             rangeCheck(index);
  41.             checkForComodification();
  42.             return internal.set(index + offset, element);
  43.         }
  44.        
  45.         @Override
  46.         public E get(int index) {
  47.             rangeCheck(index);
  48.             checkForComodification();
  49.             return internal.get(index + offset);
  50.         }
  51.        
  52.         @Override
  53.         public int size() {
  54.             checkForComodification();
  55.             return size;
  56.         }
  57.        
  58.         @Override
  59.         public void add(int index, E element) {
  60.             rangeCheckForAdd(index);
  61.             checkForComodification();
  62.             internal.add(index + offset, element);
  63.             // this.modCount = internal.modCount;
  64.             ++size;
  65.         }
  66.        
  67.         @Override
  68.         public E remove(int index) {
  69.             rangeCheck(index);
  70.             checkForComodification();
  71.             E result = internal.remove(index + offset);
  72.             // this.modCount = internal.modCount;
  73.             --size;
  74.             return result;
  75.         }
  76.        
  77.         private Method removeRangeMethod = null;
  78.        
  79.         @Override
  80.         protected void removeRange(int fromIndex, int toIndex) {
  81.             checkForComodification();
  82.             if (removeRangeMethod == null) {
  83.                 try {
  84.                     removeRangeMethod = NonNullList.class.getMethod("removeRange", int.class, int.class);
  85.                 }
  86.                 catch (NoSuchMethodException | SecurityException e) {
  87.                     throw new UnsupportedOperationException();
  88.                 }
  89.             }
  90.             try {
  91.                 removeRangeMethod.invoke(internal, fromIndex + offset, toIndex + offset);
  92.             }
  93.             catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  94.                 throw new UnsupportedOperationException();
  95.             }
  96.             // this.modCount = internal.modCount;
  97.             size -= (toIndex - fromIndex);
  98.         }
  99.        
  100.         @Override
  101.         public boolean addAll(Collection<? extends E> c) {
  102.             return addAll(size, c);
  103.         }
  104.        
  105.         @Override
  106.         public boolean addAll(int index, Collection<? extends E> c) {
  107.             rangeCheckForAdd(index);
  108.             int cSize = c.size();
  109.             if (cSize == 0) {
  110.                 return false;
  111.             }
  112.            
  113.             checkForComodification();
  114.             internal.addAll(offset + index, c);
  115.             // this.modCount = internal.modCount;
  116.             size += cSize;
  117.             return true;
  118.         }
  119.        
  120.         @Override
  121.         public Iterator<E> iterator() {
  122.             return listIterator();
  123.         }
  124.        
  125.         @Override
  126.         public ListIterator<E> listIterator(final int index) {
  127.             checkForComodification();
  128.             rangeCheckForAdd(index);
  129.            
  130.             return new ListIterator<E>() {
  131.                
  132.                 private final ListIterator<E> internalIter = internal.listIterator(index + offset);
  133.                
  134.                 @Override
  135.                 public boolean hasNext() {
  136.                     return nextIndex() < size;
  137.                 }
  138.                
  139.                 @Override
  140.                 public E next() {
  141.                     if (hasNext()) {
  142.                         return internalIter.next();
  143.                     }
  144.                     else {
  145.                         throw new NoSuchElementException();
  146.                     }
  147.                 }
  148.                
  149.                 @Override
  150.                 public boolean hasPrevious() {
  151.                     return previousIndex() >= 0;
  152.                 }
  153.                
  154.                 @Override
  155.                 public E previous() {
  156.                     if (hasPrevious()) {
  157.                         return internalIter.previous();
  158.                     }
  159.                     else {
  160.                         throw new NoSuchElementException();
  161.                     }
  162.                 }
  163.                
  164.                 @Override
  165.                 public int nextIndex() {
  166.                     return internalIter.nextIndex() - offset;
  167.                 }
  168.                
  169.                 @Override
  170.                 public int previousIndex() {
  171.                     return internalIter.previousIndex() - offset;
  172.                 }
  173.                
  174.                 @Override
  175.                 public void remove() {
  176.                     internalIter.remove();
  177.                     // SubList.this.modCount = internal.modCount;
  178.                     --size;
  179.                 }
  180.                
  181.                 @Override
  182.                 public void set(E e) {
  183.                     internalIter.set(e);
  184.                 }
  185.                
  186.                 @Override
  187.                 public void add(E e) {
  188.                     internalIter.add(e);
  189.                     // SubList.this.modCount = internal.modCount;
  190.                     ++size;
  191.                 }
  192.             };
  193.         }
  194.        
  195.         @Override
  196.         public List<E> subList(int fromIndex, int toIndex) {
  197.             return new SubList<>(this, fromIndex, toIndex);
  198.         }
  199.        
  200.         private void rangeCheck(int index) {
  201.             if (index < 0 || index >= size) {
  202.                 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  203.             }
  204.         }
  205.        
  206.         private void rangeCheckForAdd(int index) {
  207.             if (index < 0 || index > size) {
  208.                 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  209.             }
  210.         }
  211.        
  212.         private String outOfBoundsMsg(int index) {
  213.             return "Index: " + index + ", Size: " + size;
  214.         }
  215.        
  216.         private void checkForComodification() {
  217.             /*if (this.modCount != internal.modCount) {
  218.                 throw new ConcurrentModificationException();
  219.             }*/
  220.         }
  221.     }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement