/** * Returns a new {@code ImmutableJsonObject} instance which contains the given fields. * * @param fields the fields of the new JSON object. * @return a new JSON object containing the {@code fields}. * @throws NullPointerException if {@code fields} is {@code null}. */ public static ImmutableJsonObject of(final Map<String, JsonField> fields) { return new ImmutableJsonObject(fields); }
/** * Returns a new empty JSON object. * * @return a new empty JSON object. */ public static ImmutableJsonObject empty() { return new ImmutableJsonObject(Collections.emptyMap()); }
/** * Returns an empty JSON object. * * @return an empty JSON object. */ public static ImmutableJsonObject empty() { ImmutableJsonObject result = emptyInstance; if (null == result) { result = new ImmutableJsonObject(SoftReferencedFieldMap.empty()); emptyInstance = result; } return result; }
/** * Returns a new {@code ImmutableJsonObject} instance which contains the given fields. * * @param fields the fields of the new JSON object. * @param stringRepresentation the already known string representation of the returned object or {@code null}. * @return a new JSON object containing the {@code fields}. * @throws NullPointerException if {@code fields} is {@code null}. */ public static ImmutableJsonObject of(final Map<String, JsonField> fields, @Nullable final String stringRepresentation) { requireNonNull(fields, "The fields of JSON object must not be null!"); return new ImmutableJsonObject(SoftReferencedFieldMap.of(fields, stringRepresentation)); }
private JsonObject removeValueForKey(final CharSequence key) { JsonObject result = this; if (containsKey(key)) { result = new ImmutableJsonObject(fieldMap.remove(key.toString())); } return result; }
@Override public ImmutableJsonObject set(final JsonField field) { requireNonNull(field, "The JSON field to be set must not be null!"); ImmutableJsonObject result = this; final JsonField existingField = fields.get(field.getKeyName()); if (!field.equals(existingField)) { final Map<String, JsonField> fieldsCopy = copyFields(); fieldsCopy.put(field.getKeyName(), field); result = new ImmutableJsonObject(fieldsCopy); } return result; }
private JsonObject removeValueForKey(final CharSequence key) { JsonObject result = this; if (containsKey(key)) { final Map<String, JsonField> fieldsCopy = copyFields(); fieldsCopy.remove(key.toString()); result = new ImmutableJsonObject(fieldsCopy); } return result; }
@Override public JsonObject setAll(final Iterable<JsonField> fields) { requireNonNull(fields, "The JSON fields to add must not be null!"); final JsonObject result; if (isEmpty(fields)) { result = this; } else { final Map<String, JsonField> fieldsCopy = copyFields(); fields.forEach(jsonField -> fieldsCopy.put(jsonField.getKeyName(), jsonField)); result = new ImmutableJsonObject(fieldsCopy); } return result; }
@Override public JsonObject setAll(final Iterable<JsonField> fields) { requireNonNull(fields, "The JSON fields to add must not be null!"); final JsonObject result; if (isEmpty(fields)) { result = this; } else { result = new ImmutableJsonObject(fieldMap.putAll(fields)); } return result; }
@Override public ImmutableJsonObject set(final JsonField field) { requireNonNull(field, "The JSON field to be set must not be null!"); ImmutableJsonObject result = this; final JsonField existingField = fieldMap.getOrNull(field.getKeyName()); if (!field.equals(existingField)) { result = new ImmutableJsonObject(fieldMap.put(field.getKeyName(), field)); } return result; }