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