Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / bookmarksandhistory / impl / BaseHistory.java @ 1990

History | View | Annotate | Download (1.77 KB)

1
package org.gvsig.tools.bookmarksandhistory.impl;
2

    
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Iterator;
6
import java.util.List;
7
import org.gvsig.tools.bookmarksandhistory.History;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

    
11
/**
12
 *
13
 * @author jjdelcerro
14
 * @param <T>
15
 */
16
public class BaseHistory<T>
17
        implements History<T> {
18
    
19
    private static final Logger LOG = LoggerFactory.getLogger(BaseHistory.class);
20

    
21
    protected int maxsize;
22
    protected List<T> history;
23

    
24
    public BaseHistory(int size) {
25
        this.maxsize = size;
26
        this.history = new ArrayList<>();
27
    }
28

    
29
    /**
30
     *
31
     * @param element
32
     * @return
33
     */
34
    @Override
35
    public boolean add(T element) {
36
        if (element == null) {
37
            LOG.warn("Element not been add to history because it's null.");
38
            return false;
39
        }
40
        if (!this.history.isEmpty()) {
41
            T last = this.history.get(0);
42
                if (element.equals(last)) {
43
                    return true;
44
                }
45
        }
46
        this.history.add(0, element);
47
        if (this.history.size() > this.maxsize) {
48
            this.history.remove(this.history.size() - 1);
49
        }
50
        return true;
51
    }
52

    
53
    @Override
54
    public List<T> toList() {
55
        return Collections.unmodifiableList(this.history);
56
    }
57

    
58
    @Override
59
    public void clear() {
60
        this.history.clear();
61
    }
62

    
63
    @Override
64
    public boolean isEmpty() {
65
        return this.history.isEmpty();
66
    }
67

    
68
    @Override
69
    public int size() {
70
        return this.history.size();
71
    }
72

    
73
    @Override
74
    public Iterator<T> iterator() {
75
        return this.history.iterator();
76
    }
77

    
78
    @Override
79
    public T get(int position) {
80
        return this.history.get(position);
81
    }
82

    
83
}