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

com.flash3388.flashlib.io.devices.AbstractDeviceProvider Maven / Gradle / Ivy

The newest version!
package com.flash3388.flashlib.io.devices;

import com.castle.reflect.exceptions.TypeException;
import com.flash3388.flashlib.util.logging.Logging;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

public abstract class AbstractDeviceProvider implements DeviceProvider {

    protected static final Logger LOGGER = Logging.getMainLogger();

    private final Map> mKnownTypes;

    protected AbstractDeviceProvider() {
        mKnownTypes = new HashMap<>();
    }

    @Override
    public  Class findDevice(int id, Class type) {
        Class storedType = mKnownTypes.get(id);
        if (storedType == null) {
            throw new NoSuchElementException(String.valueOf(id));
        }

        if (storedType.isAssignableFrom(type)) {
            throw new TypeException("stored type is not assignable from wanted type");
        }

        //noinspection unchecked
        return (Class) storedType;
    }

    protected void registerDevice(int id, Class type) {
        type = mKnownTypes.put(id, type);
        if (type != null) {
            LOGGER.warn("Device of id {} override", id);
        }
    }

    protected  void registerDevice(DeviceId id, Class type) {
        registerDevice(id.id(), type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy