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

net.vvakame.util.jsonpullparser.builder.JsonModelBuilder Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
package net.vvakame.util.jsonpullparser.builder;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * JSON model builder.
 * @author vvakame
 * @param 
 */
public abstract class JsonModelBuilder {

	protected Class baseClass;

	protected boolean treatUnknownKeyAsError;

	protected Map> map =
			new LinkedHashMap>();


	/**
	 * the constructor.
	 * @param baseClass 
	 * @param treatUnknownKeyAsError
	 * @category constructor
	 */
	public JsonModelBuilder(Class baseClass, boolean treatUnknownKeyAsError) {
		this.baseClass = baseClass;
		this.treatUnknownKeyAsError = treatUnknownKeyAsError;
	}

	/**
	 * Attaches property builder for the all types.
	 * @return this
	 * @author vvakame
	 */
	public abstract JsonModelBuilder addAll();

	/**
	 * Attaches property builder for the given types.
	 * @param creators
	 * @return this
	 * @author vvakame
	 */
	public JsonModelBuilder add(JsonPropertyBuilderCreator... creators) {
		for (JsonPropertyBuilderCreator creator : creators) {
			addSub(creator. get());
		}
		return this;
	}

	protected void addSub(JsonPropertyBuilder builder) {
		map.put(builder.name, builder);
	}

	/**
	 * Detaches property builder for the given types.
	 * @param creators
	 * @return this
	 * @author vvakame
	 */
	public JsonModelBuilder rm(JsonPropertyBuilderCreator... creators) {
		for (JsonPropertyBuilderCreator creator : creators) {
			rmSub(creator. get());
		}
		return this;
	}

	protected void rmSub(JsonPropertyBuilder builder) {
		map.remove(builder.name);
	}

	/**
	 * Detaches property builder for the given names.
	 * @param names
	 * @return this
	 * @author vvakame
	 */
	public JsonModelBuilder rm(String... names) {
		for (String name : names) {
			rmSub(name);
		}
		return this;
	}

	protected void rmSub(String name) {
		map.remove(name);
	}

	/**
	 * Sets if the exception should be thrown upon deserializing unknown keys.
	 * @param treatUnknownKeyAsError
	 * @return this
	 * @author vvakame
	 */
	public JsonModelBuilder treatUnknownKeyAsError(boolean treatUnknownKeyAsError) {
		this.treatUnknownKeyAsError = treatUnknownKeyAsError;
		return this;
	}

	/**
	 * Fixiates the current state for coding.
	 * @return A JsonModelCoder instance for the actual coding.
	 * @author vvakame
	 */
	public abstract JsonModelCoder fix();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy