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

com.memority.citadel.shared.api.im.HasAttributeValuesMapLike Maven / Gradle / Ivy

Go to download

This artifact provides the API classes that are necessary to implement general 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 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.function.Function;
import java.util.stream.Collectors;

/**
 * {@link HasAttributeValues} extension also implementing {@code Map Map}, so that SpEL syntactic sugar
 * is available ({@code OBJECT.attributes['firstName']} instead of {@code OBJECT.attributes.value('firstName')})/
 */
@SuppressWarnings("NullableProblems")
public interface HasAttributeValuesMapLike extends HasAttributeValues, Map {

    /*
     * Map implementation for SpEL syntactic sugar.
     */
    @Override
    default int size() {
        return attributeValueMap().size();
    }

    @Override
    default boolean isEmpty() {
        return attributeValueMap().isEmpty();
    }

    @Override
    default boolean containsKey(Object key) {
        return has((String) key);
    }

    @Override
    default boolean containsValue(Object value) {
        return attributeValueMap().keySet().stream().anyMatch(attrId -> Objects.equals(HasAttributeValueHelper.doGet(this, attrId), value));
    }

    @Override
    default Object get(Object key) {
        return HasAttributeValueHelper.doGet(this, (String)key);
    }

    @Override
    default Object put(String key, Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    default Object remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    default void putAll(Map m) {
        throw new UnsupportedOperationException();
    }

    @Override
    default void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    default Set keySet() {
        return attributeValueMap().keySet();
    }

    @Override
    default Collection values() {
        return attributeValueMap().keySet().stream().map(this::value).collect(Collectors.toList());
    }

    @Override
    default Set> entrySet() {
        return keySet().stream()
                .map(key -> new AbstractMap.SimpleEntry<>(key, get(key)))
                .collect(Collectors.toSet());
    }

    /**
     * Static constructor initialized from a given list of {@code AttributeValue}.
     *
     * @param attributeValues not null (may be empty)
     * @return an implementation of {@code HasAttributeValues}
     */
    static HasAttributeValuesMapLike from(List> attributeValues) {
        Validate.notNull(attributeValues);
        return () -> attributeValues.stream().collect(Collectors.toMap(AttributeValue::getId, Function.identity()));
    }

}