package at.gv.egiz.pdfas.placeholder;

/**
 * Store and retrieve {@link SignaturePlaceholderData} in/from a thread local context.
 * 
 * @author exthex
 *
 */
public class SignaturePlaceholderContext {
   
   private ThreadLocal sigHolder = new ThreadLocal();
   
   private static SignaturePlaceholderContext instance = new SignaturePlaceholderContext();
   
   /**
    * Constructor. Private because this is a singleton.
    */
   private SignaturePlaceholderContext() {
      
   }

   /**
    * Get the {@link SignaturePlaceholderData} which is currently bound to this thread.
    * Might be null.
    * 
    * @return
    */
   public static SignaturePlaceholderData getSignaturePlaceholderData(){
      return (SignaturePlaceholderData)instance.sigHolder.get();
   }

   /**
    * 
    * @return true if there is currently a {@link SignaturePlaceholderData} bound to this thread, false otherwise.
    */
   public static boolean isSignaturePlaceholderDataSet() {
      return instance.sigHolder.get() != null;
   }

   /**
    * Bind a {@link SignaturePlaceholderData} to this thread.
    * If the given data is null, the context will be cleared.
    * 
    * @param data if null, clears the ThreadLocal, else binds the data to the current thread.
    */
   public static void setSignaturePlaceholderData(SignaturePlaceholderData data) {
      instance.sigHolder.set(data);
   }
}