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