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

org.freedesktop.dbus.connections.FallbackContainer Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).

There is a newer version: 5.1.0
Show newest version
package org.freedesktop.dbus.connections;

import org.freedesktop.dbus.messages.ExportedObject;
import org.freedesktop.dbus.utils.LoggingHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FallbackContainer {

    private final Logger                        logger    = LoggerFactory.getLogger(getClass());
    private final Map fallbacks = new HashMap<>();

    FallbackContainer() {
    }

    public synchronized void add(String _path, ExportedObject _eo) {
        logger.debug("Adding fallback on {} of {}", _path, _eo);
        fallbacks.put(_path.split("/"), _eo);
    }

    public synchronized void remove(String _path) {
        logger.debug("Removing fallback on {}", _path);
        fallbacks.remove(_path.split("/"));
    }

    public synchronized ExportedObject get(String _path) {
        int best = 0;
        ExportedObject bestobject = null;
        String[] pathel = _path.split("/");
        for (Map.Entry entry : fallbacks.entrySet()) {
            String[] fbpath = entry.getKey();

            LoggingHelper.logIf(logger.isTraceEnabled(), () ->
                logger.trace("Trying fallback path {} to match {}",
                        Arrays.deepToString(fbpath),
                        Arrays.deepToString(pathel))
            );

            int i;
            for (i = 0; i < pathel.length && i < fbpath.length; i++) {
                if (!pathel[i].equals(fbpath[i])) {
                    break;
                }
            }
            if (i > 0 && i == fbpath.length && i > best) {
                bestobject = entry.getValue();
            }
            logger.trace("Matches {} bestobject now {}", i, bestobject);
        }

        logger.debug("Found fallback for {} of {}", _path, bestobject);
        return bestobject;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy