Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * CircularBuffer.java
  3. * $Id$
  4. * Created: Tue Nov 6 10:19:03 2001
  5. *
  6. *
  7. *
  8. * Copyright 1999-2003 The Apache Software Foundation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. *
  23. *
  24. * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  25. * @version $Revision$ $Name$
  26. */
  27. package org.apache.fop.datastructs;
  28. import java.util.NoSuchElementException;
  29. /**
  30. * A general circular buffer class. It stores and returns <tt>Object</tt>
  31. * references. The buffer operations are <b><i>not</i></b> synchronized.
  32. */
  33. public class CircularBuffer {
  34. private final static int DEFAULTBUFSIZE = 32;
  35. private Object[] buf;
  36. private int size = 0;
  37. private int getptr = 0;
  38. private int putptr = 0;
  39. /**
  40. * Constructor taking a single argument; the size of the buffer.
  41. */
  42. public CircularBuffer(int size) {
  43. buf = new Object[size];
  44. this.size = size;
  45. }
  46. /**
  47. * No-argument constructor sets up a buffer with the default number of
  48. * elements.
  49. */
  50. public CircularBuffer()
  51. throws IllegalArgumentException {
  52. this(DEFAULTBUFSIZE);
  53. }
  54. public boolean isFull() {
  55. return ((putptr + 1) % size) == getptr;
  56. }
  57. public boolean isEmpty() {
  58. return putptr == getptr;
  59. }
  60. /**
  61. * <tt>get</tt> returns the next object from the buffer.
  62. * Throws a NoSuchElementException if the buffer is empty.
  63. */
  64. public Object get()
  65. throws NoSuchElementException {
  66. if (isEmpty()) {
  67. throw new NoSuchElementException(
  68. "CircularBuffer is empty.");
  69. }
  70. int tmpptr = getptr++;
  71. if (getptr == size) getptr = 0;
  72. return buf[tmpptr];
  73. }
  74. /**
  75. * <tt>put</tt> adds an object to the buffer.
  76. * Throws a NoSuchElementException if the buffer is full.
  77. */
  78. public void put(Object thing) throws NoSuchElementException {
  79. if (isFull()) {
  80. throw new NoSuchElementException(
  81. "CircularBuffer is full.");
  82. }
  83. buf[putptr++] = thing;
  84. if (putptr == size) putptr = 0;
  85. }
  86. /**
  87. * <tt>oldest</tt> returns the next object that will be overwritten
  88. * by a put. If the buffer is full, returns null.
  89. */
  90. public Object oldest() {
  91. if (isFull()) {
  92. return null;
  93. }
  94. return buf[putptr];
  95. }
  96. }