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

hu.blackbelt.epsilon.runtime.execution.impl.CompositeURIHandlerImpl Maven / Gradle / Ivy

package hu.blackbelt.epsilon.runtime.execution.impl;

import lombok.Builder;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.ToString;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Builder
@ToString
public class CompositeURIHandlerImpl  extends URIHandlerImpl  {

    @NonNull
    List uriHandlerList;

    private URIHandler chooseUriHandler(URI uri) throws IOException {
        Optional uriHandler = uriHandlerList.stream().filter(u -> u.canHandle(uri)).findFirst();
        if (!uriHandler.isPresent()) {
            throw new IOException("URI cannot be resolved: " + uri.toString());
        } else {
            return uriHandler.get();
        }
    }

    @Override
    public boolean canHandle(URI uri) {
        return uriHandlerList.stream().filter(u -> u.canHandle(uri)).findFirst().isPresent();
    }

    @Override
    public OutputStream createOutputStream(URI uri, Map options) throws IOException {
        return chooseUriHandler(uri).createOutputStream(uri, options);
    }

    @Override
    public InputStream createInputStream(URI uri, Map options) throws IOException {
        return chooseUriHandler(uri).createInputStream(uri, options);
    }

    @Override
    public void delete(URI uri, Map options) throws IOException {
        chooseUriHandler(uri).delete(uri, options);
    }

    @Override
    public boolean exists(URI uri, Map options) {
        try {
            return chooseUriHandler(uri).exists(uri, options);
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    @SneakyThrows(IOException.class)
    public Map getAttributes(URI uri, Map options) {
        return chooseUriHandler(uri).getAttributes(uri, options);
    }

    @Override
    public void setAttributes(URI uri, Map attributes, Map options) throws IOException {
        chooseUriHandler(uri).setAttributes(uri, attributes, options);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy