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

org.nlab.xml.stream.context.UserContext Maven / Gradle / Ivy

The newest version!
package org.nlab.xml.stream.context;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;


public class UserContext {

    private final Deque> hierarchicalProperties = new LinkedList<>();

    public  Optional findProperty(String name) {
        return (Optional)hierarchicalProperties.stream().filter(m -> m.containsKey(name))
                .map(m -> m.get(name))
                .findFirst();
    }

    public  Optional findProperty(String name, Class clazz) {
        return hierarchicalProperties.stream().filter(m -> m.containsKey(name)).findFirst()
                .map(m -> m.get(name))
                .map(clazz::cast);
    }

    public UserContext setProperty(String name, Object value) {
        hierarchicalProperties.getFirst().put(name , value);
        return this;
    }

    public UserContext setRootProperty(String name, Object value) {
        hierarchicalProperties.getLast().put(name , value);
        return this;
    }

    public Map flatten(){
        return hierarchicalProperties.stream().reduce((l, r) -> {
            Map result = new HashMap<>(r);
            result.putAll(l);
            return result;
        }).get();
    }



    public void push() {
        hierarchicalProperties.addFirst(new HashMap<>());
    }

    public void pop() {
        hierarchicalProperties.removeFirst();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy