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