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

me.legrange.panstamp.MemoryStore Maven / Gradle / Ivy

package me.legrange.panstamp;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * An implementation of DeviceStateStore that keeps whatever is learned in
 * memory. Used as default implementation.
 *
 * @since 1.0
 * @author Gideon le Grange https://github.com/GideonLeGrange *
 */
final class MemoryStore implements DeviceStateStore {

    MemoryStore() {
    }

    @Override
    public boolean hasRegisterValue(Register reg) {
        return mapForAddress(reg.getDevice().getAddress()).get(reg.getId()) != null;
    }

    @Override
    public byte[] getRegisterValue(Register reg) {
        return mapForAddress(reg.getDevice().getAddress()).get(reg.getId());
    }

    @Override
    public void setRegisterValue(Register reg, byte value[]) {
         mapForAddress(reg.getDevice().getAddress()).put(reg.getId(), value);
    }
    
    private Map mapForAddress(int address) {
        Map map = cache.get(address);
        if (map == null) {
            map = new ConcurrentHashMap<>();
            cache.put(address, map);
        }
        return map;
    }

    private final Map> cache = new ConcurrentHashMap<>();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy