package fr.wseduc.mongodb;

import org.vertx.java.core.json.JsonObject;

public class MongoUpdateBuilder {

	private static final String SET = "$set";
	private static final String INC = "$inc";
	private static final String UNSET = "$unset";
	private static final String RENAME = "$rename";
	private static final String PUSH = "$push";
	private static final String PULL = "$pull";

	private final JsonObject query;

	public MongoUpdateBuilder() {
		this.query = new JsonObject();
	}

	public MongoUpdateBuilder set(final String key, final Object value) {
		addToQuery(SET, key, value);
		return this;
	}

	public MongoUpdateBuilder push(final String key, final Object value) {
		addToQuery(PUSH, key, value);
		return this;
	}

	public MongoUpdateBuilder pull(final String key, final Object value) {
		addToQuery(PULL, key, value);
		return this;
	}

	public MongoUpdateBuilder rename(final String oldKey, final String newKey) {
		addToQuery(RENAME, oldKey, newKey);
		return this;
	}

	public MongoUpdateBuilder inc(final String key, final int value) {
		addToQuery(INC, key, value);
		return this;
	}

	public MongoUpdateBuilder unset(final String key) {
		addToQuery(UNSET, key, 1);
		return this;
	}

	public boolean isEmpty() {
		return query.size() == 0;
	}

	public JsonObject build() {
		return query;
	}

	private void addToQuery(String operator, String key, Object value) {
		JsonObject subquery = query.getObject(operator);
		if (subquery == null) {
			subquery = new JsonObject();
			query.putObject(operator, subquery);
		}
		subquery.putValue(key, value);
	}

}

@@ -372,16 +372,71 @@
 %22$pull%22;
+%0A%09private static final String ADD_TO_SET = %22$addToSet%22;
 %0A%0A%09priva
@@ -644,32 +644,165 @@
 eturn this;%0A%09%7D%0A%0A
+%09public MongoUpdateBuilder addToSet(final String key, final Object value) %7B%0A%09%09addToQuery(ADD_TO_SET, key, value);%0A%09%09return this;%0A%09%7D%0A%0A
 %09public MongoUpd

add in MongoUpdateBuilder