[19] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[253] | 3 | import java.security.InvalidParameterException;
|
---|
[1] | 4 | import java.util.LinkedList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
[106] | 7 | /**
|
---|
| 8 | * <p>
|
---|
| 9 | * Implements a round-trip buffered memory of a specified length that can be
|
---|
| 10 | * used to remember the recent history. Every event that happend longer ago than
|
---|
| 11 | * the length of the memory is forgotten, hence the memory is incomplete.
|
---|
| 12 | * </p>
|
---|
| 13 | *
|
---|
| 14 | * @author Steffen Herbold
|
---|
| 15 | * @version 1.0
|
---|
| 16 | *
|
---|
| 17 | * @param <T>
|
---|
| 18 | * Type which is memorized.
|
---|
| 19 | */
|
---|
[1] | 20 | public class IncompleteMemory<T> implements IMemory<T> {
|
---|
| 21 |
|
---|
[106] | 22 | /**
|
---|
| 23 | * <p>
|
---|
| 24 | * Maximum length of the memory.
|
---|
| 25 | * </p>
|
---|
| 26 | */
|
---|
[1] | 27 | private int length;
|
---|
[106] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * <p>
|
---|
| 31 | * Internal storage of the history.
|
---|
| 32 | * </p>
|
---|
| 33 | */
|
---|
[1] | 34 | private List<T> history;
|
---|
[106] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * <p>
|
---|
| 38 | * Constructor. Creates a new IncompleteMemory.
|
---|
| 39 | * </p>
|
---|
| 40 | *
|
---|
| 41 | * @param length
|
---|
| 42 | * number of recent events that are remembered
|
---|
[253] | 43 | * @throws InvalidParameterException
|
---|
| 44 | * This exception is thrown if the length is smaller than 1
|
---|
[106] | 45 | */
|
---|
[1] | 46 | public IncompleteMemory(int length) {
|
---|
[253] | 47 | if (length < 1) {
|
---|
| 48 | throw new InvalidParameterException(
|
---|
| 49 | "Length of IncompleteMemory must be at least 1.");
|
---|
| 50 | }
|
---|
[1] | 51 | this.length = length;
|
---|
| 52 | history = new LinkedList<T>();
|
---|
| 53 | }
|
---|
[106] | 54 |
|
---|
| 55 | /*
|
---|
| 56 | * (non-Javadoc)
|
---|
| 57 | *
|
---|
| 58 | * @see de.ugoe.cs.eventbench.models.IMemory#add(java.lang.Object)
|
---|
| 59 | */
|
---|
[1] | 60 | @Override
|
---|
| 61 | public void add(T state) {
|
---|
[106] | 62 | if (history.size() == length) {
|
---|
[1] | 63 | history.remove(0);
|
---|
| 64 | }
|
---|
| 65 | history.add(state);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[106] | 68 | /*
|
---|
| 69 | * (non-Javadoc)
|
---|
| 70 | *
|
---|
| 71 | * @see de.ugoe.cs.eventbench.models.IMemory#getLast(int)
|
---|
| 72 | */
|
---|
[1] | 73 | @Override
|
---|
| 74 | public List<T> getLast(int num) {
|
---|
[253] | 75 | if( num<1 ) {
|
---|
| 76 | return new LinkedList<T>();
|
---|
| 77 | } else {
|
---|
| 78 | return new LinkedList<T>(history.subList(
|
---|
| 79 | Math.max(0, history.size() - num),
|
---|
| 80 | history.size())); // defensive copy
|
---|
| 81 | }
|
---|
[1] | 82 | }
|
---|
| 83 |
|
---|
[106] | 84 | /**
|
---|
| 85 | * <p>
|
---|
| 86 | * Returns the current length of the memory. This can be less than
|
---|
| 87 | * {@link #length}, if the overall history is less than {@link #length}.
|
---|
| 88 | * </p>
|
---|
| 89 | *
|
---|
| 90 | * @return length of the current memory
|
---|
| 91 | */
|
---|
[1] | 92 | public int getLength() {
|
---|
| 93 | return history.size();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|