/******************************************************************************* * Copyright 2010-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. ******************************************************************************/ package com.amazonaws.services.cloudtrail.processinglibrary.utils; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * Provides a buffer-like store for AWS CloudTrail events. */ public class EventBuffer { private List bufferedEvents; private int bufferSize; /** * Initialize a new EventBuffer. * * @param bufferSize the number of events that can be held in the buffer. */ public EventBuffer(final int bufferSize) { LibraryUtils.checkCondition(bufferSize < 1, "Event Buffer size cannot be " + bufferSize + ", must be at lease 1."); bufferedEvents = new LinkedList<>(); this.bufferSize = bufferSize; } /** * Indicates whether the buffer has reached the number of events configured in the constructor. * * @return true if the current buffer is full; false otherwise. */ public boolean isBufferFull() { return bufferedEvents.size() >= bufferSize; } /** * Add a event to the buffer. * * @param event An object of the type configured for this buffer. */ public void addEvent(T event) { bufferedEvents.add(event); } /** * Get a list of objects held by the buffer. *

* The number of returned objects will be from zero to the configured buffer size. * * @return a List containing the buffered * objects. */ public List getEvents() { List returnEvents = new ArrayList(); if (bufferedEvents.isEmpty()) { return returnEvents; } int returnSize = isBufferFull() ? bufferSize : bufferedEvents.size(); for (int i = 0 ; i < returnSize ; i++) { returnEvents.add(bufferedEvents.remove(0)); } return returnEvents; } }