com.memority.citadel.shared.api.im.HasAttributeValues Maven / Gradle / Ivy
Show all versions of citadel-api Show documentation
/*
* 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 java.util.*;
/**
* This represents a set of {@link AttributeValue} with convenient methods to access those values
* in mono- and multi-valued mode. The type of the internal values may or may not correspond
* to the nominal type, as {@link AttributeValue} instances are used to carry those values at
* various times in their processing cycle.
*
* Note: 'empty' AttributeValues ({@link AttributeValue} objects that have no value, as returned
* by {@link AttributeValue#hasValue()}) are valid and denote that the corresponding object
* does NOT have a value for the considered attribute (which in effect indicates the attribute is not present).
*
*
* Default methods are provided to simplify/unify the implementation of the interface's contract.
*
* @see HasAttributeValuesMapLike
*/
public interface HasAttributeValues extends HasValues {
/**
* The sole method to implement. Implementors must make sure that the returned Map is immutable.
*
* @return a Map whose keys are the attribute names, may be empty but never null
*/
Map> attributeValueMap();
/**
* @return the list of attribute values held by this object, may be empty but never null
*/
default List> attributeValues() {
return Collections.unmodifiableList(new ArrayList<>(attributeValueMap().values()));
}
/**
* Fetch the AttributeValue corresponding to the corresponding attribute Id.
*
* @param attributeId the id of the looked up attribute value, as a String
* @param the attribute value type
* @return the corresponding AttributeValue, maybe null
*/
@SuppressWarnings("unchecked") // expected
default AttributeValue attributeValue(String attributeId) {
return (AttributeValue) this.attributeValueMap().get(attributeId);
}
@SuppressWarnings("unchecked")
default AttributeValue attributeValue(String attributeIdValue, Class valueType) {
AttributeValue> av = attributeValue(attributeIdValue);
if (av == null) {
return null;
}
if (! av.hasNonNullValue()) {
return (AttributeValue) av;
}
Object value = av.getValues().get(0);
if (!valueType.isAssignableFrom(value.getClass())) {
throw new ClassCastException(String.format("Actual value type of attribute '%s' is '%s' (incompatible with '%s')",
attributeIdValue, value.getClass(), valueType));
}
return (AttributeValue) av;
}
@Override
default List> values(String attributeId) {
AttributeValue> av = attributeValue(attributeId);
if (av == null) {
return Collections.emptyList();
} else {
return av.getValues();
}
}
/**
* Return the attribute value (in mono-value context) or the first attribute value (in multi-value
* context. Returns a null
value in the following cases:
*
* - if the attribute is not found
* - if the attribute does not have any value
*
*
* @param attributeId the id of the looked up attribute value, as a String
* @return the attribute value or first value, maybe null
*/
@Override
default Object value(String attributeId) {
List> values = values(attributeId);
if (values.isEmpty()) {
return null;
} else {
return values.get(0);
}
}
/**
* Returns true if and only if this object contains an AttributeValue with the given id. Note that this AttributeValue
* may or may not have a value itself.
*
* @param attributeId the attribute identifier
* @return true
if this object has a value for the given attribute, false
otherwise
*/
default boolean hasAttributeValue(String attributeId) {
return attributeValueMap().containsKey(attributeId);
}
@Override
default boolean hasValue(String attributeId) {
return hasAttributeValue(attributeId) && attributeValue(attributeId).hasValue();
}
@Override
default boolean has(String attributeId) {
return attributeValueMap().containsKey(attributeId);
}
@Override
default Set names() {
return this.attributeValueMap().keySet();
}
}