[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.usageprofiles;
|
---|
[518] | 16 |
|
---|
| 17 | import java.util.LinkedList;
|
---|
| 18 | import java.util.List;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * <p>
|
---|
[559] | 22 | * Implements a round-trip buffered memory of a specified length that can be used to remember the
|
---|
| 23 | * recent history. Every event that happend longer ago than the length of the memory is forgotten,
|
---|
| 24 | * hence the memory is incomplete.
|
---|
[518] | 25 | * </p>
|
---|
| 26 | *
|
---|
| 27 | * @author Steffen Herbold
|
---|
| 28 | * @version 1.0
|
---|
| 29 | *
|
---|
| 30 | * @param <T>
|
---|
| 31 | * Type which is memorized.
|
---|
| 32 | */
|
---|
| 33 | public class IncompleteMemory<T> implements IMemory<T> {
|
---|
| 34 |
|
---|
[559] | 35 | /**
|
---|
| 36 | * <p>
|
---|
| 37 | * Maximum length of the memory.
|
---|
| 38 | * </p>
|
---|
| 39 | */
|
---|
| 40 | private int length;
|
---|
[518] | 41 |
|
---|
[559] | 42 | /**
|
---|
| 43 | * <p>
|
---|
| 44 | * Internal storage of the history.
|
---|
| 45 | * </p>
|
---|
| 46 | */
|
---|
| 47 | private List<T> history;
|
---|
[518] | 48 |
|
---|
[559] | 49 | /**
|
---|
| 50 | * <p>
|
---|
| 51 | * Constructor. Creates a new IncompleteMemory.
|
---|
| 52 | * </p>
|
---|
| 53 | *
|
---|
| 54 | * @param length
|
---|
| 55 | * number of recent events that are remembered
|
---|
[766] | 56 | * @throws IllegalArgumentException
|
---|
[559] | 57 | * This exception is thrown if the length is smaller than 1
|
---|
| 58 | */
|
---|
| 59 | public IncompleteMemory(int length) {
|
---|
| 60 | if (length < 1) {
|
---|
[766] | 61 | throw new IllegalArgumentException("Length of IncompleteMemory must be at least 1.");
|
---|
[559] | 62 | }
|
---|
| 63 | this.length = length;
|
---|
| 64 | history = new LinkedList<T>();
|
---|
| 65 | }
|
---|
[518] | 66 |
|
---|
[559] | 67 | /*
|
---|
| 68 | * (non-Javadoc)
|
---|
| 69 | *
|
---|
[922] | 70 | * @see de.ugoe.cs.autoquest.usageprofiles.IMemory#add(java.lang.Object)
|
---|
[559] | 71 | */
|
---|
| 72 | @Override
|
---|
| 73 | public void add(T state) {
|
---|
| 74 | if (history.size() == length) {
|
---|
| 75 | history.remove(0);
|
---|
| 76 | }
|
---|
| 77 | history.add(state);
|
---|
| 78 | }
|
---|
[518] | 79 |
|
---|
[559] | 80 | /*
|
---|
| 81 | * (non-Javadoc)
|
---|
| 82 | *
|
---|
[922] | 83 | * @see de.ugoe.cs.autoquest.usageprofiles.IMemory#getLast(int)
|
---|
[559] | 84 | */
|
---|
| 85 | @Override
|
---|
| 86 | public List<T> getLast(int num) {
|
---|
| 87 | if (num < 1) {
|
---|
| 88 | return new LinkedList<T>();
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | return new LinkedList<T>(history.subList(Math.max(0, history.size() - num),
|
---|
| 92 | history.size())); // defensive copy
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[518] | 95 |
|
---|
[559] | 96 | /**
|
---|
| 97 | * <p>
|
---|
| 98 | * Returns the current length of the memory. This can be less than {@link #length}, if the
|
---|
| 99 | * overall history is less than {@link #length}.
|
---|
| 100 | * </p>
|
---|
| 101 | *
|
---|
| 102 | * @return length of the current memory
|
---|
| 103 | */
|
---|
| 104 | public int getLength() {
|
---|
| 105 | return history.size();
|
---|
| 106 | }
|
---|
[518] | 107 | }
|
---|