📄 mux.java
字号:
package lolo;/** A <tt>Mux</tt> mulitplexes two or more <tt>Scan</tt> instances. * * @author <a href="http://www.inf.uos.de/bernd" target="_blank">Bernd Kühl</a> (<a href="mailto:bernd@informatik.uni-osnabrueck.de">bernd@informatik.uni-osnabrueck.de</a>) * @see Scan */class Mux extends Scan { /** A State object which is used for <tt>nextChar()</tt>. */ protected final State stateObject = new State(); /** Collects all managed <tt>Scan</tt> instances. */ protected Scan [] scans = new Scan[4]; /** Indexes into <tt>scans</tt>. */ protected int max = scans.length, next; /** Creates a <tt>Mux</tt> object. * * @param s1 a <tt>Scan</tt> object. * @param s2 a <tt>Scan</tt> object. * @throws IllegalArgumentException if one argument is <tt>null</tt>. */ public Mux(Scan s1, Scan s2) throws IllegalArgumentException { if (s1 == s2) throw new IllegalArgumentException("s1 == s2"); add(s1); add(s2); reset(); } /** Adds a <tt>Scan</tt> object. * * @param scan a <tt>Scan</tt> object. * @throws IllegalArgumentException if <tt>scan</tt> argument is <tt>null</tt>. */ public void add(Scan scan) throws IllegalArgumentException { if (scan == null) throw new IllegalArgumentException("scan == null"); if (max == next) { Scan [] help = new Scan[max*=2]; System.arraycopy(scans, 0, help, 0, max/2); scans = help; } scans[next++] = scan; done = new boolean[next]; } /** Resets all managed <tt>Scan</tt> instance. */ public void reset() { for (int i = 0; i < next; i++) { scans[i].reset(); done[i] = false; } } /** At the end of one round <tt>winner</tt> holds the winning <tt>Scan</tt> object. */ protected transient Scan winner; /** Array displaying active <tt>Scan</tt> objects. */ protected boolean done []; public State nextChar(char ch) { boolean more = false, found = false; for (int i = 0; i < next; i++) { if (done[i]) continue; Scan scan = scans[i]; State state = scan.nextChar(ch); if (!found && state.found) { found = true; winner = scan; } if (!state.more) done[i] = true; else more = true; } return stateObject.set(more, found); } /** Calls <tt>action()</tt> for the winning <tt>Scan</tt> object. */ public void action(char [] buffer, int off, int len) { winner.action(buffer, off, len); } /** Returns the winning <tt>Scan</tt> object. * * @return the winning <tt>Scan</tt> object. */ public Scan winner() { return winner; } /** Returns a string representation. * * @return a string representation. */ public String toString() { StringBuffer buf = new StringBuffer("Mux["); for (int i = 0; i < next; i++) buf.append(scans[i].toString()).append(','); buf.setLength(buf.length()-1); return buf.append("]").toString(); } /** Returns a hash code value for the object. * * @return the sum of the hash codes of the encapsulated objects. */ public int hashCode() { int hashCode = 0; for (int i = 0; i < next; i++) hashCode += scans[i].hashCode(); return hashCode; } /** Indicate whether some other object is "equal to" this one. * * @param obj the reference object with which to compare. * @return <tt>true</tt> if <tt>obj</tt> is a <tt>Mux</tt> object and contains * the same objects (by identity) in the same order, <tt>false</tt> otherwise. */ public boolean equals(Object obj) { if (obj == null || !(obj instanceof Mux)) return false; if (this == obj) return true; Mux mux = (Mux) obj; if (next != mux.next) return false; for (int i = 0; i < next; i++) if (scans[i] != mux.scans[i]) // check identity, Scan order is always the same... return false; return true; } /** This method just throws a <tt>RuntimeException</tt> because no action for a <tt>Mux</tt> is allowed. * * @throws RuntimeException no action for a <tt>Mux</tt> is allowed. */ public Scan setAction(Action action) { throw new RuntimeException("not allowed"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -