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

org.snapscript.tree.collection.PrimitiveBooleanList Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.collection;

import org.snapscript.core.InternalArgumentException;

public class PrimitiveBooleanList extends ArrayWrapper {

   private final boolean[] array;
   private final int length;

   public PrimitiveBooleanList(boolean[] array) {
      this.length = array.length;
      this.array = array;
   }

   @Override
   public int size() {
      return length;
   }
   
   @Override
   public Boolean get(int index) {
      return array[index];
   }

   @Override
   public Boolean set(int index, Boolean value) {
      Boolean result = array[index];
      array[index] = value;
      return result;
   }
   
   @Override
   public Object[] toArray() {
      Object[] copy = new Boolean[length];
      
      for(int i = 0; i < length; i++) {
         copy[i] = array[i];
      }
      return copy;
   }

   @Override
   public  T[] toArray(T[] copy) {
      Class type = copy.getClass();
      int require = copy.length;
     
      for(int i = 0; i < length && i < require; i++) {
         Boolean flag = array[i];
         Object value = flag;
         
         if(type == String[].class) {
            value = value.toString();
         } else if(type == Boolean[].class) {
            value = flag;
         } else if(type == Object[].class) {
            value = flag;
         } else {
            throw new InternalArgumentException("Incompatible array type");
         }
         copy[i] = (T)value;
      }
      return copy;
   }

   @Override
   public int indexOf(Object object) {
      for (int i = 0; i < length; i++) {
         Object value = array[i];

         if (object.equals(value)) {
            return i;
         }
      }
      return -1;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy