View Javadoc

1   /*
2    * LinkedListToken.java
3    * 
4    * Copyright (c) 2006 David Holroyd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package uk.co.badgersinfoil.metaas.impl.antlr;
20  
21  import org.antlr.runtime.ClassicToken;
22  
23  public class LinkedListToken extends ClassicToken {
24  	private LinkedListToken prev = null;
25  	private LinkedListToken next = null;
26  
27  	public LinkedListToken(int type, String text) {
28  		super(type, text);
29  	}
30  
31  	public LinkedListToken getNext() {
32  		return next;
33  	}
34  
35  	public void setNext(LinkedListToken next) {
36  		if (this == next) {
37  			throw new IllegalArgumentException("Token stream loop detected ("+toString()+")");
38  		}
39  		this.next = next;
40  		if (next != null) {
41  			next.prev = this;
42  		}
43  	}
44  
45  	public LinkedListToken getPrev() {
46  		return prev;
47  	}
48  
49  	public void setPrev(LinkedListToken prev) {
50  		if (this == prev) {
51  			throw new IllegalArgumentException("Token stream loop detected");
52  		}
53  		this.prev = prev;
54  		if (prev != null) {
55  			prev.next = this;
56  		}
57  	}
58  
59  	public void afterInsert(LinkedListToken insert) {
60  		if (insert.getPrev() != null) {
61  			throw new IllegalArgumentException("afterInsert("+insert+") : prev was not null");
62  		}
63  		if (insert.getNext() != null) {
64  			throw new IllegalArgumentException("afterInsert("+insert+") : next was not null");
65  		}
66  		insert.next = next;
67  		insert.prev = this;
68  		if (next != null) {
69  			next.prev = insert;
70  		}
71  		next = insert;
72  	}
73  
74  	public void beforeInsert(LinkedListToken insert) {
75  		if (insert.getPrev() != null) {
76  			throw new IllegalArgumentException("beforeInsert("+insert+") : prev was not null");
77  		}
78  		if (insert.getNext() != null) {
79  			throw new IllegalArgumentException("beforeInsert("+insert+") : next was not null");
80  		}
81  		insert.prev = prev;
82  		insert.next = this;
83  		if (prev != null) {
84  			prev.next = insert;
85  		}
86  		prev = insert;
87  	}
88  
89  	public void delete() {
90  		if (prev != null) {
91  			prev.next = next;
92  		}
93  		if (next != null) {
94  			next.prev = prev;
95  		}
96  		next = prev = null;
97  	}
98  }