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

org.somda.sdc.biceps.common.MdibEntityImpl Maven / Gradle / Ivy

Go to download

SDCri is a set of Java libraries that implements a network communication framework conforming with the IEEE 11073 SDC specifications. This project implements the functionality described in IEEE 11073-10207.

There is a newer version: 5.1.1
Show newest version
package org.somda.sdc.biceps.common;

import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.somda.sdc.biceps.common.access.CopyManager;
import org.somda.sdc.biceps.model.participant.AbstractDescriptor;
import org.somda.sdc.biceps.model.participant.AbstractMultiState;
import org.somda.sdc.biceps.model.participant.AbstractState;
import org.somda.sdc.biceps.model.participant.MdibVersion;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * Default implementation of {@link MdibEntity}.
 */
public class MdibEntityImpl implements MdibEntity {
    private final String parent;
    private final List children;
    private final AbstractDescriptor descriptor;
    private final List states;
    private final MdibVersion mdibVersion;
    private final CopyManager copyManager;
    private final Class stateClass;

    @AssistedInject
    MdibEntityImpl(@Assisted @Nullable String parent,
                   @Assisted("children") List children,
                   @Assisted AbstractDescriptor descriptor,
                   @Assisted("states") List states,
                   @Assisted MdibVersion mdibVersion,
                   CopyManager copyManager,
                   MdibTypeValidator typeValidator) {
        this.parent = parent;
        this.children = children;
        this.descriptor = descriptor;
        this.states = states;
        this.mdibVersion = mdibVersion;
        this.copyManager = copyManager;

        try {
            this.stateClass = typeValidator.resolveStateType(descriptor.getClass());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(String.format(
                    "Unexpected descriptor class with no matching state class found: %s",
                    descriptor.getClass()
            ));
        }
    }

    @Override
    public MdibVersion getLastChanged() {
        return mdibVersion;
    }

    @Override
    public List getChildren() {
        return children;
    }

    @Override
    public AbstractDescriptor getDescriptor() {
        return copyManager.processOutput(descriptor);
    }

    @Override
    public  Optional getDescriptor(Class theClass) {
        return theClass.isAssignableFrom(descriptor.getClass())
                ? Optional.of(theClass.cast(getDescriptor())) : Optional.empty();
    }

    @Override
    public String getHandle() {
        return descriptor.getHandle();
    }

    @Override
    public Optional getParent() {
        return Optional.ofNullable(parent);
    }

    @Override
    public List getStates() {
        return copyManager.processOutput(states);
    }

    @Override
    public  List getStates(Class theClass) {
        if (!states.isEmpty() && theClass.isAssignableFrom(states.get(0).getClass())) {
            return (List) copyManager.processOutput(states);
        }

        return new ArrayList<>();
    }

    @Override
    public  Optional getFirstState(Class theClass) {
        if (!states.isEmpty() && theClass.isAssignableFrom(states.get(0).getClass())) {
            return Optional.of(theClass.cast(copyManager.processOutput(states.get(0))));
        }
        return Optional.empty();
    }

    @Override
    public StateAlternative> doIfSingleState(Consumer consumer) {
        if (!getStates().isEmpty()) {
            // if AbstractMultiState is a superclass of the state, it's a multi-state
            if (!AbstractMultiState.class.isAssignableFrom(getStates().get(0).getClass())) {
                consumer.accept(getStates().get(0));
                return stateAlternativeConsumer -> {
                };
            }
        }

        return stateAlternativeConsumer -> stateAlternativeConsumer.accept((List) getStates());
    }

    @Override
    public StateAlternative doIfMultiState(Consumer> consumer) {
        if (getStates().isEmpty()) {
            consumer.accept(Collections.emptyList());
            return stateAlternativeConsumer -> {
            };
        } else {
            if (AbstractMultiState.class.isAssignableFrom(getStates().get(0).getClass())) {
                consumer.accept((List) getStates());
                return stateAlternativeConsumer -> {
                };
            }
        }
        return stateAlternativeConsumer -> stateAlternativeConsumer.accept(getStates().get(0));
    }

    @Override
    public Class getDescriptorClass() {
        return descriptor.getClass();
    }

    @Override
    public Class getStateClass() {
        return stateClass;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy