1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* The Class Lock.
*/
public class Lock {
/** The list file in use. */
static private Map<String,List<String>> listFileInUse = new Hashtable<String,List<String>>();
/**
* Adds the file
*
* @param file the file
* @param clientName the client name
*/
public synchronized static void addFile(String file, String clientName) {
if(!listFileInUse.containsKey(file))
{
List<String> l = new ArrayList<String>();
l.add(clientName);
listFileInUse.put(file, l);
}
else
{
List<String> clientList = listFileInUse.get(file);
clientList.add(clientName);
listFileInUse.put(file, clientList);
}
}
/**
* Checks if is exist.
*
* @param file the file
* @return true, if checks if is exist
*/
public static synchronized Boolean isExist(String file) {
List<String> clientList = listFileInUse.get(file);
if (clientList == null)
return false;
else
return true;
}
/**
* Delete existing file.
*
* @param file the file
* @param clientName the client name
* @return the boolean
*/
public static synchronized Boolean deleteExistingFile(
String file, String clientName) {
Boolean flag = false;
List<String> clientList = listFileInUse.get(file);
for (Iterator<String> it = clientList.iterator(); it.hasNext();)
{
if(it.next().equals(clientName) )
{
it.remove();
flag = true;
break;
}
}
if(listFileInUse.get(file).size() == 0)
listFileInUse.remove(file);
return flag;
}
/**
* Gets the next client in wait.
*
* @param file the file
* @return the next client in wait
*/
public static synchronized String getNextClientInWait(String file)
{
List<String> clientList = listFileInUse.get(file);
return clientList.get(0);
}
} |
Partager