PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/util/SimpleBoundedQueue.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 110 lines | 77 code | 13 blank | 20 comment | 13 complexity | d8d84c0782bd6bbc724db65a5fb21c5c MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  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
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 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.
 15 */
 16
 17package com.hazelcast.util;
 18
 19import java.util.AbstractQueue;
 20import java.util.Iterator;
 21
 22/**
 23 * Not Thread-Safe
 24 *
 25 * @param <E>
 26 */
 27public final class SimpleBoundedQueue<E> extends AbstractQueue<E> {
 28    final int maxSize;
 29    final E[] objects;
 30    int add = 0;
 31    int remove = 0;
 32    int size = 0;
 33
 34    public SimpleBoundedQueue(int maxSize) {
 35        this.maxSize = maxSize;
 36        objects = (E[]) new Object[maxSize];
 37    }
 38
 39    @Override
 40    public boolean add(E obj) {
 41        if (size == maxSize) {
 42            return false;
 43        }
 44        objects[add] = obj;
 45        add++;
 46        size++;
 47        if (add == maxSize) {
 48            add = 0;
 49        }
 50        return true;
 51    }
 52
 53    public E pop() {
 54        if (size == 0)
 55            return null;
 56        int last = add - 1;
 57        E value = objects[last];
 58        objects[last] = null;
 59        size--;
 60        add--;
 61        return value;
 62    }
 63
 64    @Override
 65    public int size() {
 66        return size;
 67    }
 68
 69    public int remainingCapacity() {
 70        return maxSize - size;
 71    }
 72
 73    public boolean offer(E o) {
 74        return add(o);
 75    }
 76
 77    public E peek() {
 78        if (size == 0)
 79            return null;
 80        return objects[remove];
 81    }
 82
 83    public E poll() {
 84        if (size == 0)
 85            return null;
 86        E value = objects[remove];
 87        objects[remove] = null;
 88        remove++;
 89        size--;
 90        if (remove == maxSize) {
 91            remove = 0;
 92        }
 93        return value;
 94    }
 95
 96    @Override
 97    public void clear() {
 98        for (int i = 0; i < maxSize; i++) {
 99            objects[0] = null;
100        }
101        add = 0;
102        remove = 0;
103        size = 0;
104    }
105
106    @Override
107    public Iterator<E> iterator() {
108        throw new UnsupportedOperationException();
109    }
110}