View Javadoc
1   package org.gnu.glpk;
2   
3   import java.util.LinkedList;
4   
5   /**
6    * This class manages terminal output.
7    * <p>GLPK will call method {@link #callback(String) callback} before producing
8    * terminal output. A listener can inhibit the terminal output by returning
9    * <code>false</code> in the {@link GlpkTerminalListener#output(String) output}
10   * routine.
11   * <p>The list of listeners is thread local. Each thread has to register its
12   * own listener.
13   * <p>If a {@link GlpkException GlpkExeption} has occured it is necessary to
14   * call <pre>
15   * GLPK.glp_term_hook(null, null);</pre>
16   * to reenable listening to terminal output.
17   * @see GlpkTerminalListener
18   * @see GlpkException
19   * @see GLPK#glp_term_hook(SWIGTYPE_p_f_p_void_p_q_const__char__int,
20   * SWIGTYPE_p_void)
21   */
22  public final class GlpkTerminal {
23      /**
24       * List of listeners.
25       */
26      private static ThreadLocal<LinkedList<GlpkTerminalListener>> listeners
27              = new ThreadLocal<LinkedList<GlpkTerminalListener>>() {
28                  @Override
29                  protected LinkedList<GlpkTerminalListener> initialValue() {
30                      return new LinkedList<GlpkTerminalListener>(); 
31                  }
32              };
33  
34      static {
35          GLPK.glp_term_hook(null, null);
36      }
37  
38      /**
39       * Constructor.
40       */
41      private GlpkTerminal() {
42      }
43  
44      /**
45       * Callback function called by native library.
46       * Output to the console is created if any of the listeners.get() requests it.
47       * @param str string to be written to console
48       * @return 0 if output is requested
49       */
50      public static int callback(final String str) {
51          boolean output = false;
52  
53          if (listeners.get().size() > 0) {
54              for (GlpkTerminalListener listener : listeners.get()) {
55                  output |= listener.output(str);
56              }
57              if (output) {
58                  return 0;
59              } else {
60                  return 1;
61              }
62          }
63          return 0;
64      }
65  
66      /**
67       * Add listener.
68       * @param listener listener for terminal output
69       */
70      public static void addListener(final GlpkTerminalListener listener) {
71          listeners.get().add(listener);
72      }
73  
74      /**
75       * Removes first occurance of listener.
76       * @param listener listener for terminal output
77       * @return true if listener was found
78       */
79      public static boolean removeListener(final GlpkTerminalListener listener) {
80          return listeners.get().remove(listener);
81      }
82  
83      /**
84       * Remove all listeners.get().
85       */
86      public static void removeAllListeners() {
87          while (listeners.get().size() > 0) {
88              listeners.get().removeLast();
89          }
90      }
91  }