View Javadoc
1   package de.fhdw.wtf.dsl.scanner.test;
2   
3   import de.fhdw.wtf.common.stream.TokenStream;
4   import de.fhdw.wtf.common.token.Token;
5   
6   /**
7    * TokenStream-Implementation for test cases. Prints detailed information about token, when fetched from this stream to
8    * the command line.
9    * 
10   */
11  public class VerboseTokenStream implements TokenStream {
12  	
13  	/**
14  	 * Wrapped TokenStream that implements all features.
15  	 */
16  	private final TokenStream stream;
17  	
18  	/**
19  	 * Creates a verbose token stream, that adds detailed log information when accessing the wrapped stream.
20  	 * 
21  	 * @param wrappedStream
22  	 *            stream that implements necessary operations
23  	 */
24  	public VerboseTokenStream(final TokenStream wrappedStream) {
25  		this.stream = wrappedStream;
26  	}
27  	
28  	@Override
29  	public void add(final Token t) {
30  		this.getStream().add(t);
31  		System.out.println("Added -> " + t.toString());
32  	}
33  	
34  	@Override
35  	public Token next() {
36  		final Token next = this.getStream().next();
37  		System.out.println("Next -> " + next.toString());
38  		this.assertEndTokenIsLastToken(next);
39  		return next;
40  	}
41  	
42  	/**
43  	 * Asserts that (a possible) EndToken is the last Token of this stream.
44  	 * 
45  	 * @param next
46  	 *            token, which is tested for EndToken
47  	 * @throws RuntimeException
48  	 *             if this rule is violated. Definitely denotes a bug - don't try catching it!
49  	 */
50  	private void assertEndTokenIsLastToken(final Token next) {
51  		if (next.isEndToken() && this.getStream().hasNext()) {
52  			throw new RuntimeException(
53  					"After an EndToken NEVER is a next token. This exception shows a violation of this rule.");
54  		}
55  	}
56  	
57  	@Override
58  	public boolean hasNext() {
59  		return this.getStream().hasNext();
60  	}
61  	
62  	/**
63  	 * @return Returns the wrapped stream.
64  	 */
65  	public TokenStream getStream() {
66  		return this.stream;
67  	}
68  	
69  	@Override
70  	public boolean removeFirst() {
71  		return this.stream.removeFirst();
72  	}
73  	
74  	@Override
75  	public Token peek() {
76  		final Token t = this.getStream().peek();
77  		System.out.println("Peek --> " + t.toString());
78  		return t;
79  	}
80  	
81  	public void printStreamContent() {
82  		while (this.hasNext()) {
83  			System.out.print(this.getStream().next().stringRepresentation());
84  		}
85  	}
86  	
87  	@Override
88  	public TokenStream copy() {
89  		return new VerboseTokenStream(this.stream.copy());
90  	}
91  }