@Override public Value clone() { if (isArray()) return new ComparableValue(asArray().clone()); if (isDocument()) return new ComparableValue(asDocument().clone()); // All other values are immutable ... return this; }
static Array create(Object... values) { if (values == null || values.length == 0) { return create(); } Value[] vals = new Value[values.length]; for (int i = 0; i != values.length; ++i) { vals[i] = Value.create(values[i]); } return new BasicArray(vals); }
@Override public Path append(Path relPath) { if (relPath.isRoot()) return this; if (relPath.isSingle()) return new ChildPath(this, relPath.lastSegment().get()); String[] segments = new String[size() + relPath.size()]; int offset = this.copyInto(segments, 0); copyPathInto(relPath, segments, offset); return new MultiSegmentPath(segments); }
/** * Determine whether this object has an entry at the given index and the value is null. * * @param index the index * @return <code>true</code> if the entry exists but is null, or false otherwise * @see #isNullOrMissing(int) */ default boolean isNull(int index) { Value value = get(index); return value != null ? value.isNull() : false; }
/** * Get the big decimal value in this document for the given field name. * * @param fieldName The name of the field * @param defaultValue the default value to return if there is no such field or if the value is not a big decimal * @return The big decimal field value, if found, or null if there is no such field or if the value is not a big decimal */ default BigDecimal getBigDecimal(CharSequence fieldName, BigDecimal defaultValue) { Value value = get(fieldName); return value != null && value.isBigDecimal() ? value.asBigDecimal() : defaultValue; }
/** * Set the value for the field with the given name to be the supplied array. * * @param name The name of the field * @param values the (valid) values for the array * @return The array that was just set as the value for the named field; never null and may or may not be the same * instance as the supplied <code>array</code>. */ default Array setArray(CharSequence name, Object... values) { return setArray(name, Array.create(values)); }
/** * Set the value for the field with the given name to the supplied float value. * * @param index the index of the field; must be greater than or equal to 0 or less than or equal to {@link #size() size} * @param value the new value for the field * @return this array to allow for chaining methods */ default Array setNumber(int index, float value) { return setValue(index, Value.create(value)); }
/** * Set the value for the field with the given name to the supplied long value. * * @param name The name of the field * @param value the new value for the field * @return This document, to allow for chaining methods */ default Document setNumber(CharSequence name, long value) { setValue(name, Value.create(value)); return this; }
/** * Gets the field in this document with the given field name. * * @param fieldName The name of the field * @return The field, if found, or null otherwise */ default Field getField(CharSequence fieldName) { Value value = get(fieldName); return value != null ? new BasicField(fieldName, value) : null; }
static Array create(Value firstValue, Value secondValue, Value... additionalValues) { Value[] values = new Value[additionalValues.length + 2]; values[0] = Value.create(firstValue); values[1] = Value.create(secondValue); for (int i = 0; i != additionalValues.length; ++i) { values[i + 2] = Value.create(additionalValues[i]); } return new BasicArray(values); }
/** * If the current size of the array is smaller than the given size, expand it and use the supplied value for all new entries. * This method does nothing if the current size is larger than the supplied {@code desiredSize}. * * @param desiredSize the desired size of the array; may be negative * @param value the new value for any new entries * @return this array to allow for chaining methods */ default Array expand(int desiredSize, int value) { return expand(desiredSize,Value.create(value)); }
/** * Increment the numeric value at the given location by the designated amount. * @param index the index of the field; must be greater than or equal to 0 and less than or equal to {@link #size() size} * @param increment the amount to increment the existing value; may be negative to decrement * @return this array to allow for chaining methods * @throws IllegalArgumentException if the current value is not a number */ default Array increment( int index, double increment ) { return increment(index,Value.create(increment)); }
/** * Set the value for the field with the given name to the supplied big integer value. * * @param index the index of the field; must be greater than or equal to 0 or less than or equal to {@link #size() size} * @param value the new value for the field * @return this array to allow for chaining methods */ default Array setNumber(int index, BigInteger value) { return setValue(index, Value.create(value)); }
/** * Set the value for the field with the given name to the supplied double value. * * @param name The name of the field * @param value the new value for the field * @return This document, to allow for chaining methods */ default Document setNumber(CharSequence name, double value) { setValue(name, Value.create(value)); return this; }
/** * Set the value for the field with the given name to the supplied string value. * * @param index the index of the field; must be greater than or equal to 0 or less than or equal to {@link #size() size} * @param value the new value for the field * @return this array to allow for chaining methods */ default Array setString(int index, String value) { return setValue(index, Value.create(value)); }