source: trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/IncompleteMemory.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.0 KB
Line 
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
15package de.ugoe.cs.autoquest.usageprofiles;
16
17import java.util.LinkedList;
18import java.util.List;
19
20/**
21 * <p>
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.
25 * </p>
26 *
27 * @author Steffen Herbold
28 * @version 1.0
29 *
30 * @param <T>
31 *            Type which is memorized.
32 */
33public class IncompleteMemory<T> implements IMemory<T> {
34
35    /**
36     * <p>
37     * Maximum length of the memory.
38     * </p>
39     */
40    private int length;
41
42    /**
43     * <p>
44     * Internal storage of the history.
45     * </p>
46     */
47    private List<T> history;
48
49    /**
50     * <p>
51     * Constructor. Creates a new IncompleteMemory.
52     * </p>
53     *
54     * @param length
55     *            number of recent events that are remembered
56     * @throws IllegalArgumentException
57     *             This exception is thrown if the length is smaller than 1
58     */
59    public IncompleteMemory(int length) {
60        if (length < 1) {
61            throw new IllegalArgumentException("Length of IncompleteMemory must be at least 1.");
62        }
63        this.length = length;
64        history = new LinkedList<T>();
65    }
66
67    /*
68     * (non-Javadoc)
69     *
70     * @see de.ugoe.cs.autoquest.usageprofiles.IMemory#add(java.lang.Object)
71     */
72    @Override
73    public void add(T state) {
74        if (history.size() == length) {
75            history.remove(0);
76        }
77        history.add(state);
78    }
79
80    /*
81     * (non-Javadoc)
82     *
83     * @see de.ugoe.cs.autoquest.usageprofiles.IMemory#getLast(int)
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    }
95
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    }
107}
Note: See TracBrowser for help on using the repository browser.