1/*
2 * Copyright (C) 2012 The Android Open Source Project
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.android.inputmethod.research;
18
19import java.util.LinkedList;
20
21/**
22 * Maintain a FIFO queue of LogUnits.
23 *
24 * This class provides an unbounded queue.  This is useful when the user is aware that their actions
25 * are being recorded, such as when they are trying to reproduce a bug.  In this case, there should
26 * not be artificial restrictions on how many events that can be saved.
27 */
28public class LogBuffer {
29    // TODO: Gracefully handle situations in which this LogBuffer is consuming too much memory.
30    // This may happen, for example, if the user has forgotten that data is being logged.
31    private final LinkedList<LogUnit> mLogUnits;
32
33    public LogBuffer() {
34        mLogUnits = new LinkedList<LogUnit>();
35    }
36
37    protected LinkedList<LogUnit> getLogUnits() {
38        return mLogUnits;
39    }
40
41    public void clear() {
42        mLogUnits.clear();
43    }
44
45    public void shiftIn(final LogUnit logUnit) {
46        mLogUnits.add(logUnit);
47    }
48
49    public LogUnit unshiftIn() {
50        if (mLogUnits.isEmpty()) {
51            return null;
52        }
53        return mLogUnits.removeLast();
54    }
55
56    public LogUnit peekLastLogUnit() {
57        if (mLogUnits.isEmpty()) {
58            return null;
59        }
60        return mLogUnits.peekLast();
61    }
62
63    public boolean isEmpty() {
64        return mLogUnits.isEmpty();
65    }
66
67    public LogUnit shiftOut() {
68        if (isEmpty()) {
69            return null;
70        }
71        return mLogUnits.removeFirst();
72    }
73}
74