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

com.memority.domino.shared.api.AttributeDelta Maven / Gradle / Ivy

Go to download

This artifact provides the API classes that are necessary to implement synchronization configuration Rules on the Memority IM platform.

There is a newer version: 3.43.1
Show newest version
/*
 * Copyright (c) 2016-2023 Memority. All Rights Reserved.
 *
 * This file is part of Memority Domino API , a Memority project.
 *
 * This file is released under the Memority Public Artifacts End-User License Agreement,
 * see 
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 */
package com.memority.domino.shared.api;

import com.memority.citadel.shared.api.im.operation.AttributeOperation;
import com.memority.citadel.shared.api.im.operation.AttributePatch;

import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * @author Pierre Sion <[email protected]> Date: 2021/02/13
 */
public class AttributeDelta extends AttributePatch {

    /**
     * Attribute values to add
     */
    private final List valuesToAdd = new ArrayList<>();

    /**
     * Attribute values to remove
     */
    private final List valuesToRemove = new ArrayList<>();

    public AttributeDelta(AttributeOperation operation, String id, List values) {
        super(operation, id, values);
    }

    public AttributeDelta(AttributeOperation operation, String id, List values, MultiValuedState multiValuedState) {
        super(operation, id, values, multiValuedState);
    }

    public List valuesToAdd() {
        return Collections.unmodifiableList(this.valuesToAdd);
    }

    public AttributeDelta setValuesToAdd(List values) {
        this.valuesToAdd.addAll(values.stream().filter(Objects::nonNull).collect(toList()));
        return this;
    }

    public List valuesToRemove() {
        return Collections.unmodifiableList(this.valuesToRemove);
    }

    public AttributeDelta setValuesToRemove(List values) {
        this.valuesToRemove.addAll(values.stream().filter(Objects::nonNull).collect(toList()));
        return this;
    }

    public static  AttributeDelta setValues(String attributeId, List values) {
        return new AttributeDelta<>(AttributeOperation.SET, attributeId, values);
    }

    public static  AttributeDelta addValues(String attributeId, List values) {
        return new AttributeDelta<>(AttributeOperation.ADD, attributeId, values);
    }

    public static  AttributeDelta delete(String attributeId) {
        return new AttributeDelta<>(AttributeOperation.DELETE, attributeId, Collections.emptyList());
    }

    public static  AttributeDelta from(AttributePatch attributePatch) {
        return new AttributeDelta<>(attributePatch.getOperation(), attributePatch.getId(), attributePatch.getValues(),
                                    attributePatch.getMultiValuedState());
    }

    @Override
    public String toString() {
        String string = super.toString();
        if (isMultiValued()) {
            if (! valuesToAdd().isEmpty()) {
                string += " - valuesToAdd: " + valuesToAdd();
            }
            if (! valuesToRemove().isEmpty()) {
                string += " - valuesToRemove: " + valuesToRemove();
            }
        }
        return string;
    }
}