modules/json/src/main/java/net/multiphasicapps/jsr353/ImplObjectBuilder.java
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the Mozilla Public License Version 2.0.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package net.multiphasicapps.jsr353;
import com.oracle.json.JsonArray;
import com.oracle.json.JsonArrayBuilder;
import com.oracle.json.JsonNumber;
import com.oracle.json.JsonObject;
import com.oracle.json.JsonObjectBuilder;
import com.oracle.json.JsonString;
import com.oracle.json.JsonValue;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import net.multiphasicapps.collections.UnmodifiableMap;
/**
* This class builds objects.
*
* @since 2014/08/02
*/
public class ImplObjectBuilder
extends SomeBuilder
implements JsonObjectBuilder
{
/** Object builder. */
private Map<String, JsonValue> _order;
/**
* Initializes a new object builder.
*
* @since 2014/08/05
*/
public ImplObjectBuilder()
{
// Order init
this._order = new LinkedHashMap<String, JsonValue>();
}
/**
* Adds either {@link JsonValue#TRUE} or {@link JsonValue#FALSE} and
* associates it with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} is {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, boolean __v)
{
synchronized (this.lock)
{
return this.add(__n, (__v ? JsonValue.TRUE : JsonValue.FALSE));
}
}
/**
* Adds a {@link JsonNumber} with the specified value and associates it
* with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} is {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, double __v)
{
synchronized (this.lock)
{
return this.add(__n, new ImplValueNumber(__v));
}
}
/**
* Adds a {@link JsonNumber} with the specified value and associates it
* with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} is {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, int __v)
{
synchronized (this.lock)
{
return this.add(__n, new ImplValueNumber(__v));
}
}
/**
* Adds a {@link JsonArray} with an array which would be generated by the
* builder.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} or {@code __v} are
* {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, JsonArrayBuilder __v)
{
// Cannot be null
if (__v == null)
throw new NullPointerException("No array builder specified.");
// Build
synchronized (this.lock)
{
return this.add(__n, __v.build());
}
}
/**
* Adds a {@link JsonObject} with an object which would be generated by the
* builder.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} or {@code __v} are
* {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, JsonObjectBuilder __v)
{
// Cannot be null
if (__v == null)
throw new NullPointerException("No object builder specified.");
// Build
synchronized (this.lock)
{
return this.add(__n, __v.build());
}
}
/**
* Adds a {@link JsonValue} and associates it with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} or {@code __v} are
* {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, JsonValue __v)
{
// Cannot be null
if (__n == null || __v == null)
throw new NullPointerException(String.format(
"Null arguments specified: " +
"%1$s %2$s.", (__n == null ? "__n" : ""), (__v == null ?
"__v" : "")));
// Locked
synchronized (this.lock)
{
// Put into the map and return self.
this._order.put(__n, __v);
return this;
}
}
/**
* Adds a {@link JsonNumber} with the specified value and associates it
* with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} is {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, long __v)
{
synchronized (this.lock)
{
return this.add(__n, new ImplValueNumber(__v));
}
}
/**
* Adds a {@link JsonString} with the specified value and associates it
* with the given key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @param __v Value of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} or {@code __v} are
* {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder add(String __n, String __v)
{
synchronized (this.lock)
{
return this.add(__n, new ImplValueString(__v));
}
}
/**
* Adds {@link JsonValue#NULL} to the object with the specified key.
*
* If the key is already set with a value, it is replaced.
*
* @param __n Name of the key pair to add.
* @return {@code this}.
* @throws NullPointerException If {@code __n} is {@code null}.
* @since 2014/08/02
*/
@Override
public JsonObjectBuilder addNull(String __n)
{
synchronized (this.lock)
{
return this.add(__n, JsonValue.NULL);
}
}
/**
* Returns the Json object associated with this builder, the iteration of
* the order matches the order keys were added in.
*
* @return The built Json object.
* @since 2014/08/02
*/
@Override
public JsonObject build()
{
synchronized (this.lock)
{
// As a map
return new ImplValueObject(
UnmodifiableMap.of(new LinkedHashMap<String, JsonValue>(
this._order)));
}
}
}