~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/Queue.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 *    Queue.java
 
19
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 *    Modified March-May 2004 by Mark Utting to add JML specs
 
22
 *    (this was done as the example solution of an assignment for a
 
23
 *     software engineering course, so the specifications are more precise
 
24
 *     and complete than one would normally do).
 
25
 *    Passed a static analysis using ESC/Java-2.0a6 with no warnings.
 
26
 */
 
27
 
 
28
package weka.core;
 
29
 
 
30
import java.io.Serializable;
 
31
 
 
32
/** 
 
33
 * Class representing a FIFO queue.
 
34
 *
 
35
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
 
36
 * @version $Revision: 1.9 $
 
37
 */
 
38
public class Queue
 
39
  extends Object
 
40
  implements Serializable {
 
41
 
 
42
  /** for serialization */
 
43
  private static final long serialVersionUID = -1141282001146389780L;
 
44
 
 
45
  /**
 
46
   * Represents one node in the queue.
 
47
   */
 
48
  protected class QueueNode
 
49
    implements Serializable {
 
50
 
 
51
    /** for serialization */
 
52
    private static final long serialVersionUID = -5119358279412097455L;
 
53
 
 
54
    /** The next node in the queue */
 
55
    protected /*@ spec_public @*/ QueueNode m_Next;
 
56
 
 
57
    /** The nodes contents
 
58
     */
 
59
    protected /*@ non_null spec_public @*/ Object m_Contents;
 
60
 
 
61
    /** 
 
62
     * Creates a queue node with the given contents 
 
63
     */
 
64
    //@ requires contents != null;
 
65
    //@ assignable m_Contents, m_Next;
 
66
    //@ ensures m_Contents == contents;
 
67
    //@ ensures m_Next == null;
 
68
    public QueueNode(Object contents) {
 
69
      m_Contents = contents;
 
70
      next(null);
 
71
    }
 
72
 
 
73
    /**
 
74
     * Sets the next node in the queue, and returns it.
 
75
     */
 
76
    //@ requires next != this ;
 
77
    //@ assignable m_Next;
 
78
    //@ ensures m_Next==next && \result==next;
 
79
    public QueueNode next(QueueNode next) {
 
80
      return m_Next = next;
 
81
    } //@ nowarn Invariant; // Because it stupidly checks the Queue invariant!
 
82
 
 
83
    /**
 
84
     * Gets the next node in the queue. 
 
85
     */
 
86
    //@ ensures \result == m_Next;
 
87
    public /*@ pure @*/ QueueNode next() {
 
88
      return m_Next;
 
89
    }
 
90
 
 
91
    /**
 
92
     * Sets the contents of the node.
 
93
     */
 
94
    //@ requires contents != null;
 
95
    //@ assignable m_Contents;
 
96
    //@ ensures  m_Contents == contents && \result == contents;
 
97
    public Object contents(Object contents) {
 
98
      return m_Contents = contents;
 
99
    }
 
100
 
 
101
    /**
 
102
     * Returns the contents in the node.
 
103
     */
 
104
      //@ ensures \result == m_Contents;
 
105
    public /*@ pure @*/ Object contents() {
 
106
      return m_Contents;
 
107
    }
 
108
  }
 
109
 
 
110
  /** Store a reference to the head of the queue */
 
111
  protected /*@ spec_public @*/ QueueNode m_Head = null;
 
112
 
 
113
  /** Store a reference to the tail of the queue */
 
114
  protected /*@ spec_public @*/ QueueNode m_Tail = null;
 
115
 
 
116
  /** Store the c m_Tail.m_Nexturrent number of elements in the queue */
 
117
  protected /*@ spec_public @*/ int m_Size = 0;
 
118
 
 
119
  //@ public invariant m_Head == null <==> m_Tail == null;
 
120
  //@public invariant m_Tail != null ==> m_Tail.m_Next == null;
 
121
  //@ public invariant m_Size >= 0;
 
122
  //@ public invariant m_Size == 0 <==> m_Head == null;
 
123
  //@ public invariant m_Size == 1 <==> m_Head != null && m_Head == m_Tail;
 
124
  //@ public invariant m_Size > 1 ==> m_Head != m_Tail;
 
125
  //@ public invariant m_Size > 1 <== m_Head != m_Tail;
 
126
 
 
127
 
 
128
 
 
129
  /**
 
130
   * Removes all objects from the queue m_Tail.m_Next.
 
131
   */
 
132
  //@ assignable m_Size, m_Head, m_Tail;
 
133
  //@ ensures m_Size == 0;
 
134
  //@ ensures m_Head == null;
 
135
  //@ ensures m_Tail == null;
 
136
  public final synchronized void removeAllElements() {
 
137
    m_Size = 0;
 
138
    m_Head = null;
 
139
    m_Tail = null;
 
140
  }
 
141
 
 
142
  /**
 
143
   * Appends an object to the back of the queue.
 
144
   *
 
145
   * @param item the object to be appended
 
146
   * @return the object appended
 
147
   */
 
148
  //@ requires item != null;
 
149
  //@ assignable m_Head, m_Tail, m_Tail.m_Next, m_Head.m_Next, m_Size;
 
150
  //@ ensures m_Head != null;
 
151
  //@ ensures m_Tail != \old(m_Tail);
 
152
  //@ ensures m_Size == \old(m_Size) + 1;
 
153
  //@ ensures \old(m_Size) == 0 ==> m_Head == m_Tail; 
 
154
  //@ ensures \old(m_Size) != 0 ==> m_Head == \old(m_Head);
 
155
  //@ ensures m_Tail.contents() == \old(item);
 
156
  //@ ensures \result == item;
 
157
  public synchronized Object push(Object item) {
 
158
    QueueNode newNode = new QueueNode(item);
 
159
    
 
160
    if (m_Head == null) {
 
161
      m_Head = m_Tail = newNode;
 
162
    } else {
 
163
      m_Tail = m_Tail.next(newNode);
 
164
    }
 
165
    m_Size++;
 
166
    return item;
 
167
  }
 
168
 
 
169
  /**
 
170
   * Pops an object from the front of the queue.
 
171
   *
 
172
   * @return the object at the front of the queue
 
173
   * @exception RuntimeException if the queue is empty
 
174
   */
 
175
  //@ assignable m_Head, m_Tail, m_Size;
 
176
  //@ ensures m_Size == \old(m_Size) - 1;
 
177
  //@ ensures m_Head == \old(m_Head.m_Next);
 
178
  //@ ensures m_Head != null ==> m_Tail == \old(m_Tail);
 
179
  //@ ensures \result == \old(m_Head.m_Contents);
 
180
  //@ signals (RuntimeException) \old(m_Head) == null;
 
181
  public synchronized Object pop() 
 
182
      throws RuntimeException   // REDUNDANT, BUT ESCJAVA REQUIRES THIS
 
183
  {
 
184
    if (m_Head == null) {
 
185
        throw new RuntimeException("Queue is empty");
 
186
    }
 
187
    Object retval = m_Head.contents();
 
188
    m_Size--;
 
189
    m_Head = m_Head.next();
 
190
    // Here we need to either tell ESC/Java some facts about
 
191
    // the contents of the list after popping off the head,
 
192
    // or turn off the 'invariant' warnings.
 
193
    //
 
194
    //@ assume m_Size == 0 <==> m_Head == null;
 
195
    //@ assume m_Size == 1 <==> m_Head == m_Tail;
 
196
    if (m_Head == null) {
 
197
      m_Tail = null;
 
198
    }
 
199
    return retval;
 
200
  }
 
201
 
 
202
  /**
 
203
   * Gets object from the front of the queue.
 
204
   *
 
205
   * @return the object at the front of the queue
 
206
   * @exception RuntimeException if the queue is empty
 
207
   */
 
208
  //@ ensures \result == \old(m_Head.m_Contents);
 
209
  //@ signals (RuntimeException) \old(m_Head) == null;
 
210
  public /*@ pure @*/ synchronized Object peek() 
 
211
    throws RuntimeException
 
212
  { 
 
213
    if (m_Head == null) {
 
214
      throw new RuntimeException("Queue is empty");
 
215
    }
 
216
    return m_Head.contents();
 
217
  }
 
218
 
 
219
  /**
 
220
   * Checks if queue is empty.
 
221
   * 
 
222
   * @return true if queue is empty
 
223
   */
 
224
  //@ ensures \result <==> m_Head == null;
 
225
  public /*@ pure @*/ boolean empty() {
 
226
    return m_Head == null;
 
227
  }
 
228
 
 
229
  /**
 
230
   * Gets queue's size.
 
231
   *
 
232
   * @return size of queue
 
233
   */
 
234
  //@ ensures \result == m_Size;
 
235
  public /*@ pure @*/ int size() {
 
236
    return m_Size;
 
237
  }
 
238
 
 
239
  /**
 
240
   * Produces textual description of queue.
 
241
   *
 
242
   * @return textual description of queue
 
243
   */
 
244
  //@ also
 
245
  //@ ensures \result != null;
 
246
  //@ ensures (* \result == textual description of the queue *);
 
247
  public  /*@ pure @*/ String toString() {
 
248
 
 
249
    String retval = "Queue Contents "+m_Size+" elements\n";
 
250
    QueueNode current = m_Head;
 
251
    if (current == null) {
 
252
      return retval + "Empty\n";
 
253
    } else {
 
254
      while (current != null) {
 
255
        retval += current.contents().toString()+"\n"; //@nowarn Modifies;
 
256
        current = current.next();
 
257
      }
 
258
    }
 
259
    return retval;
 
260
  } //@ nowarn Post;
 
261
 
 
262
  /**
 
263
   * Main method for testing this class.
 
264
   *
 
265
   * @param argv a set of strings that are pushed on a test queue
 
266
   */
 
267
  //@ requires argv.length >= 0;
 
268
  //@ requires argv != null;
 
269
  //@ requires (\forall int i; 0 <= i && i < argv.length; argv[i] != null);
 
270
  public static void main(String [] argv) {
 
271
 
 
272
    try {
 
273
      Queue queue = new Queue();
 
274
      for(int i = 0; i < argv.length; i++) {
 
275
        queue.push(argv[i]);
 
276
      }
 
277
      System.out.println("After pushing command line arguments");
 
278
      System.out.println(queue.toString());
 
279
      while (!queue.empty()) {
 
280
        System.out.println("Pop: " + queue.pop().toString());
 
281
      }
 
282
      // try one more pop, to make sure we get an exception
 
283
      try 
 
284
        {
 
285
          queue.pop();
 
286
          System.out.println("ERROR: pop did not throw exception!");
 
287
        }
 
288
      catch (RuntimeException ex)
 
289
        {
 
290
          System.out.println("Pop on empty queue correctly gave exception.");
 
291
        }
 
292
    } catch (Exception ex) {
 
293
      System.out.println(ex.getMessage());
 
294
    }
 
295
  }
 
296
}