Skip to content

Instantly share code, notes, and snippets.

@kimburgess
Created January 10, 2013 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimburgess/4506054 to your computer and use it in GitHub Desktop.
Save kimburgess/4506054 to your computer and use it in GitHub Desktop.
Little utility class for handling Duet module state.
package com.amxaustralia.duet.util;
import com.amx.duet.devicesdk.base.Module;
import com.amx.duet.devicesdk.base.ModuleComponentEvent;
import java.util.HashMap;
import java.util.Map;
public class ModuleStateHandler {
private static final Map instances = new HashMap();
private final Module mod;
private boolean isOnline;
private Object onlineLock;
private boolean isInitialized;
private Object initLock;
private ModuleStateHandler(Module mod) {
this.mod = mod;
this.onlineLock = new Object();
this.initLock = new Object();
}
public static ModuleStateHandler getInstance(Module mod) {
synchronized (instances) {
ModuleStateHandler instance = (ModuleStateHandler) instances
.get(mod);
if (instance == null) {
instance = new ModuleStateHandler(mod);
instances.put(mod, instance);
}
return instance;
}
}
public void dispose() {
synchronized (instances) {
instances.remove(this);
}
}
public void finalize() {
dispose();
}
public boolean isOnLine() {
synchronized (this.onlineLock) {
return this.isOnline;
}
}
public boolean isInitialized() {
synchronized (this.initLock) {
return this.isInitialized;
}
}
public void setOnline(boolean state) {
synchronized (this.onlineLock) {
if (this.isOnline != state) {
this.isOnline = state;
this.mod.processDeviceOnLineEvent(new ModuleComponentEvent(
this, state, 1));
}
}
}
public void setInitialized(boolean state) {
synchronized (this.initLock) {
if (this.isInitialized != state) {
this.isInitialized = state;
this.mod.processDataInitializedEvent(new ModuleComponentEvent(
this, state, 1));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment