/* * @(#)LockedPool.java * * $Id$ * * Copyright 2004-2005 satoshi okita. All rights reserved. */ // package jp.co.uknet.rfid.util; import java.util.HashMap; /** * Phidgets,Inc did not implement in native library to delect mechanizm * that a tag is removed from the range of the Phidgets RFID reader. * this class solution to probrem. *

* NOTE: USE Singleton Design Pattern
* TODO examin support of parsistance implementation.
* * @author satoshi okita * @version $Rev$ $Date$ */ public class LockedPool { private static final int UNLOCK = 0; /** default wait time milliseconds. */ private long defaultWaitTimeMillis = 10000; private HashMap pool = new HashMap(); /* singleton */ private static LockedPool lockedPoolSingletonObject = new LockedPool(); /** * instanciate interface by singleton design pattern. * @return LockedPool object. */ public static LockedPool getInstance() { return LockedPool.lockedPoolSingletonObject; } /** * no implement initialize and client developer can not call this method * for singleton design pattern. */ private LockedPool() { } /** * change default wait time milliseconds. * @param waitTimeMillis */ public void setDefaultLockTimeMillis(long waitTimeMillis) { defaultWaitTimeMillis = waitTimeMillis; } /** * return current wait time milliseconds. * @return current wait time milliseconds */ public long setDefaultLockTimeMillis() { return defaultWaitTimeMillis; } /** * return be locked objects size. * @return be locked objects size. */ public int length() { return pool.size(); } /** * this object is pooled in this class pool variable. */ private class LockObject { public String key; public long lockedTimeMillis; public int lockStatus; public LockObject(String key) { lockedTimeMillis = System.currentTimeMillis(); incrementLockStatus(); } public void updateTimeMillis() { lockedTimeMillis = System.currentTimeMillis(); } public void unlock() { lockStatus = LockedPool.UNLOCK; } public void incrementLockStatus() { lockStatus++; } public int getLockStatus() { return lockStatus; } } /** * check object is locked by primary key. * @param key object primary key * @return zero is unlocked other locked. */ public int getLockStatus(String key) { if ( pool.isEmpty() || pool.containsKey(key) == false ) { return -1; } LockObject tmp = (LockObject) pool.get(key); return tmp.getLockStatus(); } /** * execute lock object of specify key. * @param key object primary key * @return true is successfully other error. */ public boolean lock(String key) { pool.put(key, new LockObject(key)); return true; } /** * execute unlock object of specify key. * @param key object primary key * @return true is successfully other error. */ public boolean unlock(String key) { LockObject tmp = (LockObject) pool.get(key); if ( tmp == null ) { return false; } tmp.unlock(); return true; } /** * check object is locked by primary key. * @param key object primary key * @return true is locked other false. */ public boolean isLock(String key) { if ( pool.isEmpty() ) { return false; } if ( pool.containsKey(key) == false ) { return false; } else { LockObject tmp = (LockObject) pool.get(key); if ( tmp.getLockStatus() == 0 ) { return false; } } return true; } /** * check object be locked time milliseconds add default wait time * millisecondslittle then current time milliseconds. * * @param key object primary key * @return true is time over other false. */ public boolean isWaitTimeOver(String key) { if ( pool.isEmpty() || pool.containsKey(key) == false ) { return false; } LockObject tmp = (LockObject) pool.get(key); if (tmp.lockedTimeMillis + defaultWaitTimeMillis < System.currentTimeMillis()) { return true; } return false; } /** * lock status not manage boolean type. * if object did not be locked, status is zero, and system do lock is one, * other lock status is increments. * @param key object primary key */ public void incrementLockStatus(String key) { LockObject tmp = (LockObject) pool.get(key); if ( tmp == null ) { return; } tmp.incrementLockStatus(); } }