package jp.unsf.ics.soft.jig;

import java.util.HashMap;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

public class MainApplication extends Application {
	public Object sync_ = new Object(); // 同期用。
	private HashMap<String, Integer> dictUnique = new HashMap<String, Integer>(); // concurrentにはしない。

	public boolean getFirstTicket(Activity act){
		return getFirstTicket(act.getClass().getName());
	}

	// singleton実装用
	public boolean getFirstTicket(String key){
		String ticketSolt = "t@";
		key = ticketSolt + key;
		synchronized(sync_){
			if(!dictUnique.containsKey(key)){
				dictUnique.put(key, 1);
				Log.d("MainApplication.getFirstTicket", "1");
				return true;
			}else{
				int count = dictUnique.get(key);
				dictUnique.put(key, count + 1);
				Log.d("MainApplication.getFirstTicket", ""+count +"->"+(count+1));
				return (count == 0);
			}
		}
	}
	
	public void disposeTicket(Activity act){
		disposeTicket(act.getClass().getName());
	}

	public void disposeTicket(String key){
		String ticketSolt = "t@";
		key = ticketSolt + key;
		synchronized(sync_){
			if(dictUnique.containsKey(key)){
				dictUnique.put(key, dictUnique.get(key) - 1);
				Log.d("MainApplication.disposeTicket", "->"+dictUnique.get(key));
			}else{
				Log.d("MainApplication.disposeTicket", "ticket is already 0.");
			}
		}
	}

	public boolean getOneShotTicket(String key){
		String oneshotSolt = "o@";
		key = oneshotSolt + key;
		if(!dictUnique.containsKey(key)){
			dictUnique.put(key, 1);
			Log.w("MainApplication.getOneShotTicket", "1");
			return true;
		}else{
			return false;
		}
	}

}
