com.memority.citadel.shared.api.im.HasMutableAttributeValues Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citadel-api Show documentation
Show all versions of citadel-api Show documentation
This artifact provides the API classes that are necessary to implement general configuration Rules on the Memority IM platform.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Citadel 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.citadel.shared.api.im;
import org.apache.commons.lang3.Validate;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A mutable version of {@link HasAttributeValues}, where the object attributes can be modified.
*/
public interface HasMutableAttributeValues extends HasAttributeValues {
/**
* Override of the immutable {@link HasAttributeValuesMapLike#attributeValueMap()}. Subclasses must ensure
* that the returned map is mutable as all operations are performed directly on it. If custom
* behaviour must be implemented on mutation, it must be intercepted on the Map's mutating methods
* (put, remove, clear).
*
* @return the backing, mutable Map
*/
@Override
Map> attributeValueMap();
/**
* Set the attribute values held by this mutable object. Existing values are all replaced.
*
* @param attributeValues the new attribute values
*/
default void setAttributeValues(Collection extends AttributeValue>> attributeValues) {
Validate.notNull(attributeValues);
this.clearAttributeValues();
attributeValues.forEach(this::setAttributeValue);
}
/**
* Set the given attribute value in this mutable object. If an existing value already exists
* for the same attribute id, it is replaced.
*
* @param attributeValue the new attribute value
* @return the given {@code attributeValue}
* @param the attribute value type
*/
default AttributeValue setAttributeValue(AttributeValue attributeValue) {
Validate.notNull(attributeValue);
this.attributeValueMap().put(attributeValue.getId(), attributeValue);
return attributeValue;
}
/**
* Set the given single value
for the given attribute id. This results
* in a mono-valued attribute (unless a List is passed, in which case we infer a
* multi-valued attribute).
*
* @param attributeIdValue the attribute id, as a String
* @param value the new value (may be null
)
* @return the newly set attribute value
* @param the attribute value type
*/
default AttributeValue setAttributeValue(String attributeIdValue, T value) {
Validate.notNull(attributeIdValue);
AttributeValue av = AttributeValue.from(attributeIdValue, value);
setAttributeValue(av);
return av;
}
/**
* Shortcut to {@link #addAttributeValue(String, Object, boolean, boolean)} with {@code distinct = false} and {@code sorted = false}
*
* @param attributeId the attribute id
* @param additionalValue the value to add
* @param the attribute value type
* @return the updated attribute value
*/
@SuppressWarnings("unused") //API method
default AttributeValue addAttributeValue(String attributeId, T additionalValue) {
return this.addAttributeValue(attributeId, additionalValue, false, false);
}
/**
* Add a single value to an {@code AttributeValue}
*
* @param attributeId the attribute id
* @param additionalValue the value to add
* @param distinct whether values should be distinct
* @param sorted whether values should be distinct
* @param the attribute value type
* @return the updated attribute value
*/
@SuppressWarnings("unused") //API method
default AttributeValue addAttributeValue(String attributeId, T additionalValue, boolean distinct, boolean sorted) {
return this.addAttributeValues(attributeId, Collections.singletonList(additionalValue), distinct, sorted);
}
/**
* Shortcut to {@link #addAttributeValues(String, List, boolean, boolean)} with {@code distinct = false} and {@code sorted = false}
*
* @param attributeId the attribute id
* @param additionalValues the values to add
* @param the attribute value type
* @return the updated attribute value
*/
@SuppressWarnings("unused") //API method
default AttributeValue addAttributeValues(String attributeId, List additionalValues) {
return this.addAttributeValues(attributeId, additionalValues, false, false);
}
/**
* Add values to an {@code AttributeValue}
*
* @param attributeId the attribute id
* @param additionalValues the values to add
* @param distinct whether values should be distinct
* @param sorted whether values should be distinct
* @param the attribute value type
* @return the updated attribute value
*/
default AttributeValue addAttributeValues(String attributeId, List additionalValues, boolean distinct, boolean sorted) {
Validate.notNull(attributeId);
@SuppressWarnings("unchecked")
AttributeValue existingAttributeValue = attributeValue(attributeId);
List vals = existingAttributeValue != null ? new ArrayList<>(existingAttributeValue.getValues()) : new ArrayList<>();
vals.addAll(additionalValues);
Stream valStream = vals.stream();
if (distinct) {
valStream = valStream.distinct();
}
if (sorted) {
valStream = valStream.sorted();
}
vals = valStream.collect(Collectors.toList());
AttributeValue newAttributeValue = AttributeValue.multi(attributeId, vals);
setAttributeValue(newAttributeValue);
return newAttributeValue;
}
default AttributeValue removeAttributeValue(String attributeIdValue, T valueToRemove) {
return this.removeAttributeValues(attributeIdValue, Collections.singletonList(valueToRemove));
}
default AttributeValue removeAttributeValues(String attributeIdValue, List valuesToRemove) {
Validate.notNull(attributeIdValue);
@SuppressWarnings("unchecked")
AttributeValue existingAttributeValue = (AttributeValue) attributeValue(attributeIdValue);
List vals = existingAttributeValue != null ? new ArrayList<>(existingAttributeValue.getValues()) : new ArrayList<>();
vals.removeAll(valuesToRemove);
AttributeValue newAttributeValue = AttributeValue.multi(attributeIdValue, vals);
setAttributeValue(newAttributeValue);
return newAttributeValue;
}
/**
* Shortcut to {@link #setAttributeValues(String, Collection, boolean, boolean)} with {@code distinct = false} and {@code sorted = false}
*
* @param attributeId the attribute id
* @param values the values to set
* @param the attribute value type
* @return the updated attribute value
*/
default AttributeValue setAttributeValues(String attributeId, Collection values) {
return this.setAttributeValues(attributeId, values, false, false);
}
/**
* Set the given multiple values
for the given attribute id. This results
* in a multi-valued attribute. If keepDistinctAndSorted
then the list of values is first processed
* to ensure that there is no duplicate values and that they are sorted in their natural order.
*
* @param attributeIdValue the attribute id, as a String
* @param values the new value
* @param distinct true to keep new values distinct
* @param sorted true to keep new values ordered
* @param the attribute value type
* @return the updated attribute value
*/
default AttributeValue setAttributeValues(String attributeIdValue, Collection values, boolean distinct, boolean sorted) {
Validate.notNull(attributeIdValue);
Stream valStream = values.stream();
if (distinct) {
valStream = valStream.distinct();
}
if (sorted) {
valStream = valStream.sorted();
}
values = valStream.collect(Collectors.toList());
AttributeValue av = AttributeValue.from(attributeIdValue, values);
setAttributeValue(av);
return av;
}
/**
* Clear this object's values / also an override of {@link Map#clear()}
*/
default void clearAttributeValues() {
this.attributeValueMap().clear();
}
}