KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > util > IntQueue


1 package com.thoughtworks.xstream.core.util;
2
3 public final class IntQueue {
4
5     private final int[] data;
6     private int writePointer = 0;
7     private int readPointer = 0;
8     private boolean empty = true;
9
10     public IntQueue(int size) {
11         data = new int[size];
12     }
13
14     public void write(int value) {
15         if (!empty && writePointer == readPointer) {
16             throw new OverflowException();
17         }
18         data[writePointer++] = value;
19         if (writePointer == data.length) {
20             writePointer = 0;
21         }
22         empty = false;
23     }
24
25     public int read() {
26         if (empty) {
27             throw new NothingToReadException();
28         }
29         int result = data[readPointer++];
30         if (readPointer == data.length) {
31             readPointer = 0;
32         }
33         if (readPointer == writePointer) {
34             empty = true;
35         }
36         return result;
37     }
38
39     public boolean isEmpty() {
40         return empty;
41     }
42
43     public static class OverflowException extends RuntimeException JavaDoc {
44     }
45
46     public static class NothingToReadException extends RuntimeException JavaDoc {
47     }
48
49 }
50
Popular Tags