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

com.mastfrog.asyncpromises.mongo.ModificationBuilderImpl Maven / Gradle / Ivy

There is a newer version: 2.9.7
Show newest version
/*
 * The MIT License
 *
 * Copyright 2015 Tim Boudreau.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mastfrog.asyncpromises.mongo;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bson.Document;

/**
 *
 * @author Tim Boudreau
 */
final class ModificationBuilderImpl implements ModificationBuilder {

    private final Map set = new HashMap<>();
    private final Map inc = new HashMap<>();
    private final Map push = new HashMap<>();
    private final Map pull = new HashMap<>();
    private final Map setOnInsert = new HashMap<>();
    private final Map rename = new HashMap<>();
    private final Set unset = new HashSet<>();
    final Factory factory;

    ModificationBuilderImpl(Factory factory) {
        this.factory = factory;
    }

    public boolean isEmpty() {
        return set.isEmpty() && inc.isEmpty() && push.isEmpty() && pull.isEmpty() && setOnInsert.isEmpty() && rename.isEmpty() && unset.isEmpty();
    }

    Document toDocument() {
        Document result = new Document();
        if (!set.isEmpty()) {
            result.append("$set", new Document(set));
        }
        if (!setOnInsert.isEmpty()) {
            result.append("$setOnInsert", new Document(setOnInsert));
        }
        if (!inc.isEmpty()) {
            result.append("$inc", new Document(inc));
        }
        if (!push.isEmpty()) {
            result.append("$push", new Document(push));
        }
        if (!pull.isEmpty()) {
            result.append("$pull", new Document(push));
        }
        if (!unset.isEmpty()) {
            Document un = new Document();
            for (String key : unset) {
                un.append(key, "");
            }
            result.append("$unset", un);
        }
        if (!rename.isEmpty()) {
            Document rn = new Document();
            for (Map.Entry e : rename.entrySet()) {
                rn.append(e.getKey(), e.getValue());
            }
            result.append("$rename", rn);
        }
        if (result.isEmpty()) {
            throw new IllegalStateException("Modification document is empty - update will fail");
        }
        return result;
    }

    static  ModificationBuilder> create(UpdateBuilderImpl impl) {
        return new ModificationBuilderImpl<>(new UpdateFactory<>(impl));
    }

    interface Factory {

        R build(Document document);
    }

    static final class UpdateFactory implements Factory> {

        private final UpdateBuilderImpl update;

        public UpdateFactory(UpdateBuilderImpl update) {
            this.update = update;
        }

        @Override
        public UpdateBuilder build(Document document) {
            return update.modification(document);
        }
    }

    public T build() {
        return factory.build(toDocument());
    }

    public ModificationBuilder rename(String old, String nue) {
        if (old.equals(nue)) {
            throw new IllegalArgumentException("Renaming " + old + " to " + nue);
        }
        rename.put(old, nue);
        return this;
    }

    public ModificationBuilder setOnInsert(String key, Object value) {
        setOnInsert.put(key, value);
        return this;
    }

    public ModificationBuilder unset(String name) {
        unset.add(name);
        return this;
    }

    public ModificationBuilder set(String name, Object value) {
        set.put(name, value);
        return this;
    }

    public ModificationBuilder decrement(String name) {
        return increment(name, -1);
    }

    public ModificationBuilder decrement(String name, int amount) {
        return increment(name, -amount);
    }

    public ModificationBuilder decrement(String name, long amount) {
        return increment(name, -amount);
    }

    public ModificationBuilder increment(String name) {
        return increment(name, 1);
    }

    public ModificationBuilder increment(String name, int amount) {
        inc.put(name, amount);
        return this;
    }

    public ModificationBuilder increment(String name, long amount) {
        inc.put(name, amount);
        return this;
    }

    public ModificationBuilder push(String name, Object... val) {
        Object value = val;
        if (val != null && val.length > 0) {
            if (val.length == 1 && val[0] != null && val[0].getClass().isArray()) {
                int size = Array.getLength(val[0]);
                List l = new ArrayList<>(size);
                for (int i = 0; i < size; i++) {
                    l.add(Array.get(val[0], i));
                }
                value = new Document("$each", l);
            } else if (val.length == 1 && val[0] instanceof Collection) {
                value = new Document("$each", val[0]);
            } else if (val.length == 1) {
                value = val[0];
            } else {
                value = new Document("$each", Arrays.asList(val));
            }
        }
        if (value != null && value.getClass().isArray()) {
            int size = Array.getLength(value);
            List l = new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                l.add(Array.get(value, i));
            }
            value = new Document("$each", l);
        }
        push.put(name, value);
        return this;
    }

    public ModificationBuilder pull(String name, Object val) {
        pull.put(name, val);
        return this;
    }
}