package de.ugoe.cs.quest.usageprofiles; import java.security.InvalidParameterException; import java.util.LinkedList; import java.util.List; /** *

* Implements a round-trip buffered memory of a specified length that can be * used to remember the recent history. Every event that happend longer ago than * the length of the memory is forgotten, hence the memory is incomplete. *

* * @author Steffen Herbold * @version 1.0 * * @param * Type which is memorized. */ public class IncompleteMemory implements IMemory { /** *

* Maximum length of the memory. *

*/ private int length; /** *

* Internal storage of the history. *

*/ private List history; /** *

* Constructor. Creates a new IncompleteMemory. *

* * @param length * number of recent events that are remembered * @throws InvalidParameterException * This exception is thrown if the length is smaller than 1 */ public IncompleteMemory(int length) { if (length < 1) { throw new InvalidParameterException( "Length of IncompleteMemory must be at least 1."); } this.length = length; history = new LinkedList(); } /* * (non-Javadoc) * * @see de.ugoe.cs.quest.usageprofiles.IMemory#add(java.lang.Object) */ @Override public void add(T state) { if (history.size() == length) { history.remove(0); } history.add(state); } /* * (non-Javadoc) * * @see de.ugoe.cs.quest.usageprofiles.IMemory#getLast(int) */ @Override public List getLast(int num) { if( num<1 ) { return new LinkedList(); } else { return new LinkedList(history.subList( Math.max(0, history.size() - num), history.size())); // defensive copy } } /** *

* Returns the current length of the memory. This can be less than * {@link #length}, if the overall history is less than {@link #length}. *

* * @return length of the current memory */ public int getLength() { return history.size(); } }