<commit_before>package railo.runtime.lock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import railo.commons.collections.HashTable;


/**
 * Lock mnager to make a log by a string name
 */
public final class LockManagerImpl implements LockManager {

	private static List<LockManagerImpl> managers=new ArrayList<LockManagerImpl>();
    private Map<String,LockToken> locks=new HashTable();
	
    private LockManagerImpl() {
    	
    }
	
    public static LockManager getInstance() {
    	LockManagerImpl lmi = new LockManagerImpl();
    	managers.add(lmi);
    	return lmi;
    }
	
	/**
     * @see railo.runtime.lock.LockManager#lock(int, java.lang.String, int, int)
     */
	public LockData lock(int type, String name, int timeout, int pageContextId) throws LockTimeoutException, InterruptedException {
		LockData data = new LockDataImpl(type,name,pageContextId);
        LockToken token=touchLookToken(data);
        long start=System.currentTimeMillis();
        if(timeout<=0)timeout=1;
        synchronized(token) {
        	//print.out("count:"+token.getDataCount()+":"+token.canEnter(data));

        	while(!token.canEnter(data)) { 
            	//print.out("wait:"+timeout);
                token.wait(timeout);
            	//print.out("count:"+timeout+"+"+(start+timeout<=System.currentTimeMillis()));
                if(timeout!=0 && start+timeout<=System.currentTimeMillis()) {
                    //SystemOut.printDate("lock ["+name+"] timeout after "+(System.currentTimeMillis()-start)+" millis");
    				throw new LockTimeoutException(type,name,timeout);
    			}
    		}
            token.addLockData(data);
            if(data.isReadOnly())token.notify();
        }
        return data;
	}

    /**
     * @see railo.runtime.lock.LockManager#unlock(railo.runtime.lock.LockManager.LockData)
     */
	public void unlock(LockData data) {
        if(data==null)return;
        LockToken token=(LockToken)locks.get(data.getName());
        
        if(token!=null) {
           if(token.removeLockData(data))
               locks.remove(data.getName());
        }
	}
    
    /**
     * touch a LockToken, create if needed
     * @param data
     * @return token
     */
    private LockToken touchLookToken(LockData data) {
    	LockToken token=null;
    	synchronized(locks) {
	    	token=(LockToken)locks.get(data.getName());
	        if(token == null){
	            token=new LockToken();
	            locks.put(data.getName(),token);
	        }
    	}
        return token;
    }
    
    /**
     * a running lock token
     */
    private class LockToken {
        
        //private LockData data;
        //private int count;
        private Set<LockData> datas=new HashSet<LockData>();

        public int getDataCount() {
        	return datas.size();
        }
        
        /**
         * remove lock data
         * @param data 
         * @return has removed or not
         */
        private synchronized boolean removeLockData(LockData data) {
        	if(datas.remove(data))notify();
        	return datas.isEmpty();
        }

        /**
         * adds a data to token
         * @param data
         */
        private void addLockData(LockData data) {
        	//print.out("b:"+datas.size()+":"+this);
        	datas.add(data);
        	//print.out("a:"+datas.size());
        }

        /**
         * can the data entry in the lock with this token
         * @param data
         * @return can entry
         */
        private boolean canEnter(LockData data) {
        	LockData d=null;
        	synchronized(datas){ 
	        	Iterator it = datas.iterator();
	        	while(it.hasNext()) {
	        		d=(LockData) it.next();
	        		if(d!=data && d.getId()!=data.getId() && !data.isReadOnly()) return false;
	        	}
        	}
        	return true;
        }
        /*private boolean hasId(int id) {
        	LockData d=null;
        	synchronized(datas){ 
	        	Iterator it = datas.iterator();
	        	while(it.hasNext()) {
	        		d=(LockData) it.next();
	        		if(d.getId()==id) return true;
	        	}
        	}
        	return false;
        }*/
    }

	/**
	 *
	 * @see railo.runtime.lock.LockManager#getOpenLockNames()
	 */
	public String[] getOpenLockNames() {
	/*	return getOpenLockNames(-1);
	}
	
	private String[] getOpenLockNames(int pageContextId) {*/
		Iterator it = locks.entrySet().iterator();
		ArrayList rtn=new ArrayList();
		LockToken token;
		Map.Entry entry;
		while(it.hasNext()) {
			entry=(Entry) it.next();
			token=(LockToken) entry.getValue();
			
			if(token.getDataCount()>0) {
				rtn.add(entry.getKey());
			}
		}
		return (String[]) rtn.toArray(new String[rtn.size()]);
	}

	
	
	private LockData[] getLockDatas(int pageContextId) {
		Iterator it = locks.entrySet().iterator(),itt;
		ArrayList rtn=new ArrayList();
		LockToken token;
		LockData data;
		
		while(it.hasNext()) {
			token=(LockToken) ((Entry) it.next()).getValue();
			itt=token.datas.iterator();
			while(itt.hasNext()) {
				data=(LockData) itt.next();
				if(data.getId()==pageContextId)rtn.add(data);
			}
		}
		return (LockData[]) rtn.toArray(new LockData[rtn.size()]);
	}

	/**
	 *
	 * @see railo.runtime.lock.LockManager#unlock(int)
	 */
	public void unlock(int pageContextId) {
		LockData[] datas = getLockDatas(pageContextId);
		for(int i=0;i<datas.length;i++) {
			unlock(datas[i]);
		}
	}
	
	public void clear() {
		//locks.clear();
	}
	
	public static void unlockAll(int pageContextId) {
		Iterator it = managers.iterator();
		LockManagerImpl lmi;
		while(it.hasNext()) {
			lmi=(LockManagerImpl) it.next();
			lmi.unlock(pageContextId);
		}
	}
	
	
}<commit_msg>add generic types to collection<commit_after>package railo.runtime.lock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import railo.commons.collections.HashTable;


/**
 * Lock mnager to make a log by a string name
 */
public final class LockManagerImpl implements LockManager {

	private static List<LockManagerImpl> managers=new ArrayList<LockManagerImpl>();
    private Map<String,LockToken> locks=new HashTable();
	
    private LockManagerImpl() {
    	
    }
	
    public static LockManager getInstance() {
    	LockManagerImpl lmi = new LockManagerImpl();
    	managers.add(lmi);
    	return lmi;
    }
	
	/**
     * @see railo.runtime.lock.LockManager#lock(int, java.lang.String, int, int)
     */
	public LockData lock(int type, String name, int timeout, int pageContextId) throws LockTimeoutException, InterruptedException {
		LockData data = new LockDataImpl(type,name,pageContextId);
        LockToken token=touchLookToken(data);
        long start=System.currentTimeMillis();
        if(timeout<=0)timeout=1;
        synchronized(token) {
        	//print.out("count:"+token.getDataCount()+":"+token.canEnter(data));

        	while(!token.canEnter(data)) { 
            	//print.out("wait:"+timeout);
                token.wait(timeout);
            	//print.out("count:"+timeout+"+"+(start+timeout<=System.currentTimeMillis()));
                if(timeout!=0 && start+timeout<=System.currentTimeMillis()) {
                    //SystemOut.printDate("lock ["+name+"] timeout after "+(System.currentTimeMillis()-start)+" millis");
    				throw new LockTimeoutException(type,name,timeout);
    			}
    		}
            token.addLockData(data);
            if(data.isReadOnly())token.notify();
        }
        return data;
	}

    /**
     * @see railo.runtime.lock.LockManager#unlock(railo.runtime.lock.LockManager.LockData)
     */
	public void unlock(LockData data) {
        if(data==null)return;
        LockToken token=locks.get(data.getName());
        
        if(token!=null) {
           if(token.removeLockData(data))
               locks.remove(data.getName());
        }
	}
    
    /**
     * touch a LockToken, create if needed
     * @param data
     * @return token
     */
    private LockToken touchLookToken(LockData data) {
    	LockToken token=null;
    	synchronized(locks) {
	    	token=locks.get(data.getName());
	        if(token == null){
	            token=new LockToken();
	            locks.put(data.getName(),token);
	        }
    	}
        return token;
    }
    
    /**
     * a running lock token
     */
    private class LockToken {
        
        //private LockData data;
        //private int count;
        private Set<LockData> datas=new HashSet<LockData>();

        public int getDataCount() {
        	return datas.size();
        }
        
        /**
         * remove lock data
         * @param data 
         * @return has removed or not
         */
        private synchronized boolean removeLockData(LockData data) {
        	if(datas.remove(data))notify();
        	return datas.isEmpty();
        }

        /**
         * adds a data to token
         * @param data
         */
        private void addLockData(LockData data) {
        	//print.out("b:"+datas.size()+":"+this);
        	datas.add(data);
        	//print.out("a:"+datas.size());
        }

        /**
         * can the data entry in the lock with this token
         * @param data
         * @return can entry
         */
        private boolean canEnter(LockData data) {
        	LockData d=null;
        	synchronized(datas){ 
	        	Iterator<LockData> it = datas.iterator();
	        	while(it.hasNext()) {
	        		d=it.next();
	        		if(d!=data && d.getId()!=data.getId() && !data.isReadOnly()) return false;
	        	}
        	}
        	return true;
        }
        /*private boolean hasId(int id) {
        	LockData d=null;
        	synchronized(datas){ 
	        	Iterator it = datas.iterator();
	        	while(it.hasNext()) {
	        		d=(LockData) it.next();
	        		if(d.getId()==id) return true;
	        	}
        	}
        	return false;
        }*/
    }

	/**
	 *
	 * @see railo.runtime.lock.LockManager#getOpenLockNames()
	 */
	public String[] getOpenLockNames() {
	/*	return getOpenLockNames(-1);
	}
	
	private String[] getOpenLockNames(int pageContextId) {*/
		Iterator<Entry<String, LockToken>> it = locks.entrySet().iterator();
		ArrayList<String> rtn=new ArrayList<String>();
		LockToken token;
		Entry<String, LockToken> entry;
		while(it.hasNext()) {
			entry = it.next();
			token=entry.getValue();
			
			if(token.getDataCount()>0) {
				rtn.add(entry.getKey());
			}
		}
		return rtn.toArray(new String[rtn.size()]);
	}

	
	
	private LockData[] getLockDatas(int pageContextId) {
		Iterator<Entry<String, LockToken>> it = locks.entrySet().iterator();
		Iterator<LockData> itt;
		ArrayList<LockData> rtn=new ArrayList<LockData>();
		LockToken token;
		LockData data;
		
		while(it.hasNext()) {
			token=it.next().getValue();
			itt=token.datas.iterator();
			while(itt.hasNext()) {
				data= itt.next();
				if(data.getId()==pageContextId)rtn.add(data);
			}
		}
		return rtn.toArray(new LockData[rtn.size()]);
	}

	/**
	 *
	 * @see railo.runtime.lock.LockManager#unlock(int)
	 */
	public void unlock(int pageContextId) {
		LockData[] datas = getLockDatas(pageContextId);
		for(int i=0;i<datas.length;i++) {
			unlock(datas[i]);
		}
	}
	
	public void clear() {
		//locks.clear();
	}
	
	public static void unlockAll(int pageContextId) {
		Iterator<LockManagerImpl> it = managers.iterator();
		LockManagerImpl lmi;
		while(it.hasNext()) {
			lmi=it.next();
			lmi.unlock(pageContextId);
		}
	}
	
	
}
{'fraction_non_alphanumeric': 0.14099577185261886, 'fraction_numerical': 0.0010354646647683149, 'mean_word_length': 2.3819667347534286, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890016', 'n_tokens_mistral': 3829, 'n_tokens_neox': 3582, 'n_words': 676}