package com.hypirion.beckon;

import sun.misc.Signal;
import sun.misc.SignalHandler;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class SignalRegistererHelper {

    /**
     * A mapping of the signal name to the original signal handler.
     */
    private final static Map<String, SignalHandler> originalHandlers =
        new HashMap<String, SignalHandler>();

    /**
     * Registers the new list of functions to the signal name, and returns the
     * old SignalHandler.
     */
    private static SignalHandler reset_BANG_(String signame, List fns) {
        Signal sig = new Signal(signame);
        SignalFolder folder = new SignalFolder(fns);
        SignalHandler oldHandler = Signal.handle(sig, folder);
        return oldHandler;
    }

    /**
     * Registers the signal name to a List of Callables, where each callable
     * returns an Object. The signal handling is performed as follows: The first
     * callable is called, and if it returns a value equal to <code>false</code>
     * or <code>null</code> it will stop. Otherwise it will repeat on the next
     * callable, until there are no more left.
     *
     * @param signame the signal name to register this list of callables on.
     * @param fns the list of Callables to (potentially) call.
     */
    static synchronized void register(String signame, List fns) {
        SignalHandler old = reset_BANG_(signame, fns);
        if (!originalHandlers.containsKey(signame)) {
            originalHandlers.put(signame, old);
        }
    }

    /**
     * Resets/reinits the signal back to its original signal handler, discarding
     * all possible changes.
     *
     * @param signame the name of the signal to reinit.
     */
    static synchronized void reinit_signal_handler_BANG_(String signame) {
        if (originalHandlers.containsKey(signame)) {
            SignalHandler original = originalHandlers.get(signame);
            Signal sig = new Signal(signame);
            Signal.handle(sig, original);
        }
    }

    /**
     * Resets/reinits all the signals back to their original signal handlers,
     * discarding all possible changes done to them.
     */
    static synchronized void reinit_all_BANG_() {
        for (String signame : originalHandlers.keySet()) {
            reinit_signal_handler_BANG_(signame);
        }
    }
}

@@ -119,19 +119,47 @@
 va.util.
-Map
+ArrayList;%0Aimport java.util.Set
 ;%0Aimport
@@ -173,19 +173,19 @@
 til.Hash
-Map
+Set
 ;%0A%0Apubli
@@ -239,50 +239,23 @@
 * A 
-mapping of the signal name to the original
+set of modified
  sig
@@ -261,24 +261,25 @@
 gnal handler
+s
 .%0A     */%0A  
@@ -305,43 +305,28 @@
 tic 
-Map
+Set
 %3CString
-, SignalHandler%3E original
+%3E modified
 Hand
@@ -331,24 +331,16 @@
 ndlers =
-%0A       
  new Has
@@ -344,33 +344,18 @@
 Hash
-Map
+Set
 %3CString
-, SignalHandler
 %3E();
@@ -1403,111 +1403,38 @@
     
-if (!originalHandlers.containsKey(signame)) %7B%0A            originalHandlers.put(signame, old);%0A        %7D
+modifiedHandlers.add(signame);
 %0A   
@@ -1705,65 +1705,8 @@
 ) %7B%0A
-        if (originalHandlers.containsKey(signame)) %7B%0A    
     
@@ -1734,21 +1734,19 @@
 ginal = 
-or
+S
 ig
-i
 nalHandl
@@ -1751,28 +1751,18 @@
 dler
-s.get(signame);%0A    
+.SIG_DFL;%0A
     
@@ -1803,28 +1803,24 @@
 e);%0A        
-    
 Signal.handl
@@ -1841,25 +1841,53 @@
 l);%0A        
-%7D
+modifiedHandlers.remove(sig);
 %0A    %7D%0A%0A    
@@ -2091,27 +2091,140 @@
     
-for (
+// To get around the fact that we cannot remove elements from a set%0A        // while iterating over it.%0A        List%3C
 String
+%3E
  signame
  : o
@@ -2223,36 +2223,83 @@
 name
- : originalHandlers.keySet()
+s = new ArrayList(modifiedHandlers);%0A        for (String signame : signames
 ) %7B%0A

Use sets and native default signalhandlers.