Exemple #1
0
 /**
  * Removes the value at the specified key
  *
  * @param construct
  * @return
  */
 public Construct remove(Construct construct) {
   String c = normalizeConstruct(construct);
   Construct ret;
   if (!associative_mode) {
     try {
       ret = array.remove(Integer.parseInt(c));
       next_index--;
     } catch (NumberFormatException e) {
       throw ConfigRuntimeException.BuildException(
           "Expecting an integer, but received \""
               + c
               + "\" (were you expecting an associative array? This array is a normal array.)",
           ExceptionType.CastException,
           construct.getTarget());
     } catch (IndexOutOfBoundsException e) {
       throw ConfigRuntimeException.BuildException(
           "Cannot remove the value at '" + c + "', as no such index exists in the array",
           ExceptionType.RangeException,
           construct.getTarget());
     }
   } else {
     ret = associative_array.remove(c);
   }
   regenValue(new HashSet<CArray>());
   return ret;
 }
Exemple #2
0
 @Override
 public Construct get(Construct index, Target t) {
   if (!associative_mode) {
     try {
       return array.get(Static.getInt32(index, t));
     } catch (IndexOutOfBoundsException e) {
       throw ConfigRuntimeException.BuildException(
           "The element at index \"" + index.val() + "\" does not exist",
           ExceptionType.IndexOverflowException,
           t,
           e);
     }
   } else {
     if (associative_array.containsKey(normalizeConstruct(index))) {
       Construct val = associative_array.get(normalizeConstruct(index));
       if (val instanceof CEntry) {
         return ((CEntry) val).construct();
       }
       return val;
     } else {
       // Create this so we can at least attach a stacktrace.
       @SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
       IndexOutOfBoundsException ioobe = new IndexOutOfBoundsException();
       throw ConfigRuntimeException.BuildException(
           "The element at index \"" + index.val() + "\" does not exist",
           ExceptionType.IndexOverflowException,
           t,
           ioobe);
     }
   }
 }
Exemple #3
0
 /**
  * @param index
  * @param c
  */
 public void set(Construct index, Construct c, Target t) {
   if (!associative_mode) {
     try {
       int indx = Static.getInt32(index, t);
       if (indx > next_index || indx < 0) {
         throw ConfigRuntimeException.BuildException(
             "", ExceptionType.IndexOverflowException, Target.UNKNOWN);
       } else if (indx == next_index) {
         this.push(c, t);
       } else {
         array.set(indx, c);
       }
     } catch (ConfigRuntimeException e) {
       // Not a number. Convert to associative.
       associative_array = new TreeMap<String, Construct>(comparator);
       for (int i = 0; i < array.size(); i++) {
         associative_array.put(Integer.toString(i), array.get(i));
       }
       associative_mode = true;
       array = null; // null out the original array container so it can be GC'd
     }
   }
   if (associative_mode) {
     associative_array.put(normalizeConstruct(index), c);
   }
   if (c instanceof CArray) {
     ((CArray) c).parent = this;
   }
   regenValue(new HashSet<CArray>());
 }
Exemple #4
0
 /**
  * Reverses the array in place, if it is a normal array, otherwise, if associative, it throws an
  * exception.
  */
 public void reverse() {
   if (!associative_mode) {
     Collections.reverse(array);
     regenValue(new HashSet<CArray>());
   } else {
     throw ConfigRuntimeException.BuildException(
         "Cannot reverse an associative array.", ExceptionType.CastException, getTarget());
   }
 }
Exemple #5
0
 public CDouble(String value, Target t) {
   super(value, ConstructType.INT, t);
   try {
     val = Double.parseDouble(value);
   } catch (NumberFormatException e) {
     throw ConfigRuntimeException.BuildException(
         "Could not cast " + value + " to double", ExceptionType.FormatException, t);
   }
 }
Exemple #6
0
 private String normalizeConstruct(Construct c) {
   if (c instanceof CArray) {
     throw ConfigRuntimeException.BuildException(
         "Arrays cannot be used as the key in an associative array",
         ExceptionType.CastException,
         c.getTarget());
   } else if (c instanceof CString || c instanceof CInt) {
     return c.val();
   } else if (c instanceof CNull) {
     return "";
   } else if (c instanceof CBoolean) {
     if (((CBoolean) c).getBoolean()) {
       return "1";
     } else {
       return "0";
     }
   } else if (c instanceof CLabel) {
     return normalizeConstruct(((CLabel) c).cVal());
   } else {
     return c.val();
   }
 }