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

com.rbmhtechnology.vind.api.query.update.Update Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.rbmhtechnology.vind.api.query.update;

import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.*;

import java.util.*;

/**
 * Class to define the updates to be perform on a document.
 *
 * @author Thomas Kurz ([email protected])
 * @since 12.07.16.
 */
public class Update {

    private String id;
    private HashMap, HashMap>> contextualizedOptions = new HashMap<>();
    private String context;

    /**
     * Creates a new instance of {@link Update} object for an specific Document.
     * @param id String unique identification value of the document to be updated.
     */
    public Update(String id) {
        this.id = id;
    }

    /**
     *  Prepares a field to be removed from the document.
     * @param descriptor {@link FieldDescriptor} indicates the field to ve removed.
     * @return {@link Update} object set to remove the specified field.
     */
    public Update remove(FieldDescriptor descriptor) {
        return this.remove(this.context, descriptor);
    }

    /**
     *  Prepares a field to be removed from the document. Deprecated: may cause ambigous method call
     * @param descriptor {@link FieldDescriptor} indicates the field to ve removed.
     * @param context String name of the context where the value will be updated.
     * @return {@link Update} object set to remove the specified field.
     */
    @Deprecated
    public Update remove(FieldDescriptor descriptor, String context) {
        return remove(context, descriptor);
    }
    /**
     *  Prepares a field to be removed from the document.
     * @param descriptor {@link FieldDescriptor} indicates the field to ve removed.
     * @param context String name of the context where the value will be updated.
     * @return {@link Update} object set to remove the specified field.
     */
    public Update remove(String context, FieldDescriptor descriptor) {
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.set, null));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares a group of values to be removed from a specific field of the document. If no values are provided (null or
     * empty list) the field is removed.
     * @param descriptor {@link FieldDescriptor} indicates the field  where the values are removed from.
     * @param t values to be deleted from the document field.
     * @param  Type of content the field can store.
     * @return {@link Update} object set to remove the specified values from the field.
     */
    public  Update remove(FieldDescriptor descriptor, T... t) {
        return this.remove(this.context, descriptor, t);
    }

    /**
     * Prepares a group of values to be removed from a specific field of the document. If no values are provided (null or
     * empty list) the field is removed. Deprecated: may cause ambigous method call
     * @param descriptor {@link FieldDescriptor} indicates the field  where the values are removed from.
     * @param context String name of the context where the value will be updated.
     * @param t values to be deleted from the document field.
     * @param  Type of content the field can store.
     * @return {@link Update} object set to remove the specified values from the field.
     */
    @Deprecated
    public  Update remove(FieldDescriptor descriptor, String context, T... t) {
        return remove(context, descriptor, t);
    }

    /**
     * Prepares a group of values to be removed from a specific field of the document. If no values are provided (null or
     * empty list) the field is removed.
     * @param descriptor {@link FieldDescriptor} indicates the field  where the values are removed from.
     * @param context String name of the context where the value will be updated.
     * @param t values to be deleted from the document field.
     * @param  Type of content the field can store.
     * @return {@link Update} object set to remove the specified values from the field.
     */
    public  Update remove(String context, FieldDescriptor descriptor, T... t) {
        if(Objects.isNull(t) || t.length<=0) {
            return remove(context, descriptor);
        }
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.remove, Arrays.asList(t)));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares to remove those values, from a multivalued field, which matches with the given regex.
     * @param descriptor {@link MultiValueFieldDescriptor} indicates the field  where the values are removed from.
     * @param s String regex matching the values to be deleted.
     * @return {@link Update} object set to remove the specified regex matching values from the field.
     */
    public Update removeRegex(MultiValueFieldDescriptor descriptor, String s) {
        return this.removeRegex(this.context, descriptor,s);
    }

    /**
     * Prepares to remove those values, from a multivalued field, which matches with the given regex. Deprecated: may cause ambigous method call
     * @param descriptor {@link MultiValueFieldDescriptor} indicates the field  where the values are removed from.
     * @param context String name of the context where the value will be updated.
     * @param s String regex matching the values to be deleted.
     * @return {@link Update} object set to remove the specified regex matching values from the field.
     */
    @Deprecated
    public Update removeRegex(MultiValueFieldDescriptor descriptor,String context, String s) {
        return removeRegex(context,descriptor,s);
    }
    /**
     * Prepares to remove those values, from a multivalued field, which matches with the given regex.
     * @param descriptor {@link MultiValueFieldDescriptor} indicates the field  where the values are removed from.
     * @param context String name of the context where the value will be updated.
     * @param s String regex matching the values to be deleted.
     * @return {@link Update} object set to remove the specified regex matching values from the field.
     */
    public Update removeRegex(String context, MultiValueFieldDescriptor descriptor, String s) {
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.removeregex, s));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);

        return this;
    }

    /**
     * Prepares the {@link Update} object to increment a numeric field in an specified quantity.
     * @param descriptor {@link com.rbmhtechnology.vind.model.SingleValueFieldDescriptor.NumericFieldDescriptor} indicating the field to be incremented.
     * @param t Quantity to increase the field.
     * @param  Type of the field. T must extend Number.
     * @return {@link Update} object set to increment in the specified amount the field.
     */
    public  Update increment(SingleValueFieldDescriptor.NumericFieldDescriptor descriptor, T t) {
        return this.increment(descriptor, this.context, t);
    }

    /**
     * Prepares the {@link Update} object to increment a numeric field in an specified quantity. Deprecated: may cause ambigous method call
     * @param descriptor {@link com.rbmhtechnology.vind.model.SingleValueFieldDescriptor.NumericFieldDescriptor} indicating the field to be incremented.
     * @param context String name of the context where the value will be updated.
     * @param t Quantity to increase the field.
     * @param  Type of the field. T must extend Number.
     * @return {@link Update} object set to increment in the specified amount the field.
     */
    @Deprecated
    public  Update increment(SingleValueFieldDescriptor.NumericFieldDescriptor descriptor, String context, T t) {
        return increment(context, descriptor, t);
    }
    /**
     * Prepares the {@link Update} object to increment a numeric field in an specified quantity.
     * @param descriptor {@link com.rbmhtechnology.vind.model.SingleValueFieldDescriptor.NumericFieldDescriptor} indicating the field to be incremented.
     * @param context String name of the context where the value will be updated.
     * @param t Quantity to increase the field.
     * @param  Type of the field. T must extend Number.
     * @return {@link Update} object set to increment in the specified amount the field.
     */
    public  Update increment(String context, SingleValueFieldDescriptor.NumericFieldDescriptor descriptor, T t) {
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.inc, t));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);

        return this;
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed.
     * @param descriptor {@link SingleValueFieldDescriptor} indicating the field to be set.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    public  Update set(SingleValueFieldDescriptor descriptor, T t) {
        return this.set(this.context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed. Deprecated: may cause ambigous method call
     * @param descriptor {@link SingleValueFieldDescriptor} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    @Deprecated
    public  Update set(SingleValueFieldDescriptor descriptor, String context, T t) {
        return set(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed.
     * @param descriptor {@link SingleValueFieldDescriptor} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    public  Update set( String context, SingleValueFieldDescriptor descriptor, T t) {
        if(Objects.isNull(t)) {
            return remove(context, descriptor);
        }
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.set, t));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);

        return this;
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed.
     * @param descriptor {@link SingleValuedComplexField} indicating the field to be set.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    public  Update set(SingleValuedComplexField descriptor, T t) {
        return this.set(descriptor, this.context, t);
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed. Deprecated: may cause ambigous method call
     * @param descriptor {@link SingleValuedComplexField} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    @Deprecated
    public  Update set(SingleValuedComplexField descriptor, String context, T t) {
        return set(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a value on a single valued field. The old value, if existing, is
     * over-written. If no no value is given (null) the field is removed.
     * @param descriptor {@link SingleValuedComplexField} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Value to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the value on the field.
     */
    public  Update set(String context, SingleValuedComplexField descriptor, T t) {
        if(Objects.isNull(t)) {
            return remove(context, descriptor);
        }
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.set, t));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed.
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be set.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    public  Update set(MultiValueFieldDescriptor descriptor, T ... t) {
        return this.set(this.context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed. Deprecated: may cause ambigous method call
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    @Deprecated
    public  Update set(MultiValueFieldDescriptor descriptor, String context, T ... t) {
        return set(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed.
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    public  Update set(String context, MultiValueFieldDescriptor descriptor, T ... t) {
        if(Objects.isNull(t) || t.length<=0) {
            return remove(context, descriptor);
        }
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.set, Arrays.asList(t)));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed.
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be set.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    public  Update set(MultiValuedComplexField descriptor, T ... t) {
        return this.set(this.context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed. Deprecated: may cause ambigous method call
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    @Deprecated
    public  Update set(MultiValuedComplexField descriptor, String context, T ... t) {
        return set(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to set a group of values on a multivalued field. The old values, if existing,
     * are over-written. If no no values are given (null or empty) the field is removed.
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be set.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be set.
     * @param  Type of the field.
     * @return {@link Update} object set to set the values on the field.
     */
    public  Update set(String context, MultiValuedComplexField descriptor, T ... t) {
        if(Objects.isNull(t) || t.length<=0) {
            return remove(context, descriptor);
        }
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.set, Arrays.asList(t)));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field.
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be added to.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    public  Update add(MultiValueFieldDescriptor descriptor, T ... t) {
        return this.add(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field. Deprecated: may cause ambigous method call
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be added to.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    @Deprecated
    public  Update add(MultiValueFieldDescriptor descriptor, String context, T ... t) {
        return add(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field.
     * @param descriptor {@link MultiValueFieldDescriptor} indicating the field to be added to.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    public  Update add(String context, MultiValueFieldDescriptor descriptor, T ... t) {
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.add, Arrays.asList(t)));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field.
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be added to.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    public  Update add(MultiValuedComplexField descriptor, T ... t) {
        return this.add(this.context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field. Deprecated: may cause ambigous method call
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be added to.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    @Deprecated
    public  Update add(MultiValuedComplexField descriptor,String context,T ... t) {
        return add(context, descriptor, t);
    }

    /**
     * Prepares the {@link Update} object to add a group of values to a multivalued field.
     * @param descriptor {@link MultiValuedComplexField} indicating the field to be added to.
     * @param context String name of the context where the value will be updated.
     * @param t Values to be added.
     * @param  Type of the field.
     * @return {@link Update} object set to add the values on the field.
     */
    public  Update add(String context,MultiValuedComplexField descriptor,T ... t) {
        final HashMap> fieldOperations =
                Objects.isNull(contextualizedOptions.get(descriptor)) ? new HashMap<>() : contextualizedOptions.get(descriptor);

        final SortedSet updateOperations =
                Objects.isNull(fieldOperations.get(context)) ? new TreeSet<>() : fieldOperations.get(context);

        updateOperations.add(new UpdateOperation(UpdateOperations.add, Arrays.asList(t)));
        fieldOperations.put(context,updateOperations);
        contextualizedOptions.put(descriptor,fieldOperations);
        return this;
    }

    /**
     * Gets the document identification string.
     * @return String document id.
     */
    public String getId() {
        return id;
    }

    /**
     * Get the updates prepared in the {@link Update} instance.
     * @return  {@code HashMap, HashMap>} A map of {@link FieldDescriptor} as key and as value a map with key {@link UpdateOperations} and value
     * the modifier values.
     */
    public HashMap, HashMap>> getOptions() {
        return contextualizedOptions;
    }

    /**
     *  Sets the update context.
     * @param context String context to be searched in.
     * @return This {@link Update} instance with the new context.
     */
    public Update context(String context) {
        this.context = context;
        return this;
    }

    /**
     * Gets the context of the update.
     * @return String containing the context target.
     */
    public String getUpdateContext() {
        return this.context;
    }

    public enum UpdateOperations {
        add, inc, remove, removeregex, set
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy