OnJava8-Examples/validating/CircularQueue.java

92 lines
2.6 KiB
Java
Raw Normal View History

2016-11-26 10:20:48 -08:00
// validating/CircularQueue.java
2020-10-07 13:35:40 -06:00
// (c)2020 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Demonstration of Design by Contract (DbC)
2016-08-29 07:27:53 -06:00
package validating;
2015-06-15 17:47:35 -07:00
import java.util.*;
2016-11-26 10:20:48 -08:00
public class CircularQueue {
2015-06-15 17:47:35 -07:00
private Object[] data;
private int
in = 0, // Next available storage space
out = 0; // Next gettable object
// Has it wrapped around the circular queue?
private boolean wrapped = false;
2016-11-26 10:20:48 -08:00
public CircularQueue(int size) {
2015-06-15 17:47:35 -07:00
data = new Object[size];
2016-01-25 18:05:55 -08:00
// Must be true after construction:
assert invariant();
2015-06-15 17:47:35 -07:00
}
public boolean empty() {
return !wrapped && in == out;
}
public boolean full() {
return wrapped && in == out;
}
public boolean isWrapped() { return wrapped; }
2015-06-15 17:47:35 -07:00
public void put(Object item) {
precondition(item != null, "put() null item");
2016-11-26 10:20:48 -08:00
precondition(!full(),
"put() into full CircularQueue");
2015-06-15 17:47:35 -07:00
assert invariant();
data[in++] = item;
if(in >= data.length) {
in = 0;
wrapped = true;
}
assert invariant();
}
public Object get() {
2016-11-26 10:20:48 -08:00
precondition(!empty(),
"get() from empty CircularQueue");
2015-06-15 17:47:35 -07:00
assert invariant();
Object returnVal = data[out];
data[out] = null;
out++;
if(out >= data.length) {
out = 0;
wrapped = false;
}
assert postcondition(
2016-11-26 10:20:48 -08:00
returnVal != null,
"Null item in CircularQueue");
2015-06-15 17:47:35 -07:00
assert invariant();
return returnVal;
}
// Design-by-contract support methods:
private static void
precondition(boolean cond, String msg) {
2016-11-26 10:20:48 -08:00
if(!cond) throw new CircularQueueException(msg);
2015-06-15 17:47:35 -07:00
}
private static boolean
postcondition(boolean cond, String msg) {
2016-11-26 10:20:48 -08:00
if(!cond) throw new CircularQueueException(msg);
2015-06-15 17:47:35 -07:00
return true;
}
private boolean invariant() {
// Guarantee that no null values are in the
// region of 'data' that holds objects:
for(int i = out; i != in; i = (i + 1) % data.length)
if(data[i] == null)
2016-11-26 10:20:48 -08:00
throw new CircularQueueException(
"null in CircularQueue");
2015-06-15 17:47:35 -07:00
// Guarantee that only null values are outside the
// region of 'data' that holds objects:
if(full()) return true;
for(int i = in; i != out; i = (i + 1) % data.length)
if(data[i] != null)
2016-11-26 10:20:48 -08:00
throw new CircularQueueException(
"non-null outside of CircularQueue range: "
+ dump());
2015-06-15 17:47:35 -07:00
return true;
}
public String dump() {
2015-06-15 17:47:35 -07:00
return "in = " + in +
", out = " + out +
", full() = " + full() +
", empty() = " + empty() +
2016-11-26 10:20:48 -08:00
", CircularQueue = " + Arrays.asList(data);
2015-06-15 17:47:35 -07:00
}
2016-08-23 16:48:02 -06:00
}