Created
January 10, 2013 21:43
-
-
Save kimburgess/4506054 to your computer and use it in GitHub Desktop.
Little utility class for handling Duet module state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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