2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.containers;
18 import org.jetbrains.annotations.NotNull;
21 * A single threaded, resizable, circular char queue backed by an array.
23 * @author Sergey Simonchik
25 public class CharArrayQueue {
27 private char[] myArray;
32 public CharArrayQueue(int initialCapacity) {
33 myArray = new char[initialCapacity];
39 public void add(char c) {
40 resizeIfNeeded(mySize + 1);
44 public void addAll(char[] buffer) {
45 resizeIfNeeded(mySize + buffer.length);
46 for (char c : buffer) {
51 public void add(@NotNull String str) {
52 resizeIfNeeded(mySize + str.length());
53 for (int i = 0; i < str.length(); i++) {
58 private void doAdd(char c) {
61 if (myTail >= myArray.length) {
71 char res = myArray[myHead];
73 if (myHead >= myArray.length) {
80 public boolean isEmpty() {
88 private void resizeIfNeeded(int newSize) {
89 int len = myArray.length;
91 char[] newArray = new char[Math.max(len << 1, newSize)];
92 if (myHead < myTail) {
93 System.arraycopy(myArray, myHead, newArray, 0, myTail - myHead);
96 System.arraycopy(myArray, myHead, newArray, 0, len - myHead);
97 System.arraycopy(myArray, 0, newArray, len - myHead, myTail);