/*-
 * ========================LICENSE_START========================
 * Morphix MongoDB
 * %%
 * Copyright (C) 2017 - 2018 Harry Fox
 * %%
 * This file is part of Morphix, licensed under the MIT License (MIT).
 *
 * Copyright 2018 Harry Fox <https://hfox.uk/>
 * Copyright 2018 contributors
 *
 * 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.
 * ========================LICENSE_END========================
 */
package uk.hfox.morphix.mongo.query;

import uk.hfox.morphix.query.FieldQueryBuilder;
import uk.hfox.morphix.query.QueryBuilder;

import java.util.HashMap;
import java.util.Map;

public class MongoFieldQueryBuilder<T> implements FieldQueryBuilder<T> {

    private final QueryBuilder<T> builder;

    private boolean locked;

    private boolean not = false;
    private Object result = null;

    public MongoFieldQueryBuilder(QueryBuilder<T> builder) {
        this.builder = builder;
    }

    @Override
    public QueryBuilder<T> matches(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("eq", object);

        return builder;
    }

    @Override
    public FieldQueryBuilder<T> not() {
        return not(!this.not);
    }

    @Override
    public FieldQueryBuilder<T> not(boolean not) {
        this.not = not;
        return this;
    }

    @Override
    public QueryBuilder<T> notEqual(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("ne", object);

        return this.builder;
    }

    @Override
    public QueryBuilder<T> greaterThan(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("gt", object);

        return this.builder;
    }

    @Override
    public QueryBuilder<T> greaterThanOrEqual(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("gte", object);

        return this.builder;
    }

    @Override
    public QueryBuilder<T> lessThan(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("lt", object);

        return this.builder;
    }

    @Override
    public QueryBuilder<T> lessThanOrEqual(Object object) {
        if (this.locked) {
            throw new IllegalStateException("field query is locked");
        }

        this.locked = true;
        setResult("lte", object);

        return this.builder;
    }

    private void setResult(String operation, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put("$" + operation, value);
        this.result = map;
    }

    public Object getBson() {
        if (not) {
            Map<String, Object> map = new HashMap<>();
            map.put("$not", result);
            return map;
        }

        return result;
    }

}

@@ -1892,66 +1892,31 @@
     
-@Override%0A    public QueryBuilder%3CT%3E matches(Object object
+private void checkLock(
 ) %7B%0A
@@ -2013,32 +2013,126 @@
 ked%22);%0A        %7D
+%0A    %7D%0A%0A    @Override%0A    public QueryBuilder%3CT%3E matches(Object object) %7B%0A        checkLock();
 %0A%0A        this.l
@@ -2498,106 +2498,20 @@
     
-if (this.locked) %7B%0A            throw new IllegalStateException(%22field query is locked%22);%0A        %7D
+checkLock();
 %0A%0A  
@@ -2688,106 +2688,20 @@
     
-if (this.locked) %7B%0A            throw new IllegalStateException(%22field query is locked%22);%0A        %7D
+checkLock();
 %0A%0A  
@@ -2885,106 +2885,20 @@
     
-if (this.locked) %7B%0A            throw new IllegalStateException(%22field query is locked%22);%0A        %7D
+checkLock();
 %0A%0A  
@@ -3073,106 +3073,20 @@
     
-if (this.locked) %7B%0A            throw new IllegalStateException(%22field query is locked%22);%0A        %7D
+checkLock();
 %0A%0A  
@@ -3267,106 +3267,20 @@
     
-if (this.locked) %7B%0A            throw new IllegalStateException(%22field query is locked%22);%0A        %7D
+checkLock();
 %0A%0A  

Replace 6 duplicates