001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.util;
019
020import com.google.common.collect.Iterators;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.util.Iterator;
024import java.util.concurrent.locks.Lock;
025import java.util.concurrent.locks.ReentrantLock;
026
027/**
028 * A thread safe implementation of a Circular array
029 *
030 * <p>A simple data structure that will overwrite the oldest value when a new one is added, keeping it at a
031 * fixed length</p>
032 */
033@ThreadSafe
034public class ConcurrentCircularArray<E> implements Iterable<E> {
035    private final Object[] list;
036    private final int size;
037
038    private final Lock lock = new ReentrantLock();
039    private int index = 0;
040
041    public ConcurrentCircularArray(int size) {
042        this.list = new Object[size];
043        this.size = size;
044    }
045
046    public void add(E item) {
047        lock.lock();
048        try {
049            if (index == size) index = 0;
050            list[index] = item;
051            index++;
052        } finally {
053            lock.unlock();
054        }
055    }
056
057    @Override
058    public Iterator<E> iterator() {
059        lock.lock();
060        try {
061            return Iterators.transform(Iterators.forArray(list), e -> (E) e);
062        } finally {
063            lock.unlock();
064        }
065    }
066}