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

com.coditory.quark.context.ConditionContext Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.coditory.quark.context;

import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.coditory.quark.context.BeanDescriptor.descriptor;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toSet;

class ConditionContext {
    static ConditionContext from(Set> holders, Map properties) {
        Set> descriptors = holders.stream()
                .map(BeanHolder::getDescriptor)
                .collect(toSet());
        return new ConditionContext(descriptors, properties);
    }

    static ConditionContext from(Map properties) {
        return new ConditionContext(Set.of(), properties);
    }

    private final Set> descriptors;
    private final Set names;
    private final Map properties;

    private ConditionContext(Set> descriptors, Map properties) {
        this.descriptors = requireNonNull(descriptors);
        this.properties = requireNonNull(properties);
        this.names = descriptors.stream()
                .map(BeanDescriptor::getName)
                .collect(toSet());
    }

    public boolean hasBean(Class type, String name) {
        return descriptors.contains(descriptor(type, name));
    }

    public boolean hasBean(String name) {
        return names.contains(name);
    }

    public boolean hasBean(Class type) {
        return descriptors.contains(descriptor(type));
    }

    @Nullable
    public Object getProperty(String name) {
        return properties.get(name);
    }

    public Object getPropertyOrDefault(String name, Object defaultValue) {
        return properties.getOrDefault(name, defaultValue);
    }

    ConditionContext with(BeanHolder holder) {
        Set> descriptors = new HashSet<>(this.descriptors);
        descriptors.addAll(holder.getBeanClassHierarchyDescriptors());
        return new ConditionContext(descriptors, properties);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy