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