KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > util > WorkSet


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  * Copyright 2007 Raymond Audet <raymond.audet@gmail.com>
5  * Copyright 2007 Patrick Pelletier <pp.pelletier@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.sablecc.sablecc.util;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.sablecc.sablecc.exception.InternalException;
27
28 /**
29  * A work set is a set of element to work on. This class provides various
30  * methods to maniplutate work sets.
31  */

32 public class WorkSet<T> {
33
34     /** The list of the elements already treated. */
35     private final Set JavaDoc<T> done = new HashSet JavaDoc<T>();
36
37     /** The list of the elements left to be treated. */
38     private final LinkedHashSet JavaDoc<T> toDo = new LinkedHashSet JavaDoc<T>();
39
40     /**
41      * Indicates if an element has a next one.
42      *
43      * @return a <code>boolean</code> value indicating whether the element has
44      * a next one or not.
45      */

46     public boolean hasNext() {
47
48         return !this.toDo.isEmpty();
49     }
50
51     /**
52      * Treats a new element, adding it to the done list.
53      *
54      * @return the treated element.
55      */

56     public T next() {
57
58         T next = this.toDo.iterator().next();
59
60         this.toDo.remove(next);
61         this.done.add(next);
62
63         return next;
64     }
65
66     /**
67      * Adds a new element to be treated.
68      *
69      * @param element
70      * the new element to be treated.
71      * @throws InternalException
72      * if the element is <code>null</code>.
73      */

74     public void add(
75             T element) {
76
77         if (element == null) {
78             throw new InternalException("element may not be null");
79         }
80
81         if (!this.done.contains(element)) {
82             this.toDo.add(element);
83         }
84     }
85 }
86
Popular Tags