aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/history/OrderedQueryResultSet.java
blob: 23802f764f5a348ce76295d76cadd203e8c256f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package net.java.sip.communicator.impl.history;

import java.util.*;

import net.java.sip.communicator.service.history.*;
import net.java.sip.communicator.service.history.records.*;

/**
 * This implementation is the same as DefaultQueryResultSet but the
 * container holding the records is LinkedList - so guarantees that values are ordered
 *
 * @author Damian Minkov
 */
public class OrderedQueryResultSet
    implements QueryResultSet
{
    private LinkedList records = null;

    private int currentPos = -1;

    public OrderedQueryResultSet(Set records)
    {
        this.records = new LinkedList(records);
    }

    /**
     * Returns <tt>true</tt> if the iteration has more elements.
     *
     * @return <tt>true</tt> if the iterator has more elements.
     */
    public boolean hasNext()
    {
        return this.currentPos + 1 < this.records.size();
    }

    /**
     * Returns true if the iteration has elements preceeding the current one.
     *
     * @return true if the iterator has preceeding elements.
     */
    public boolean hasPrev()
    {
        return this.currentPos - 1 >= 0;
    }

    /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration.
     */
    public Object next()
    {
        this.currentPos++;

        if (this.currentPos >= this.records.size())
        {
            throw new NoSuchElementException();
        }

        return records.get(this.currentPos);
    }

    /**
     * A strongly-typed variant of <tt>next()</tt>.
     *
     * @return the next history record.
     * @throws NoSuchElementException iteration has no more elements.
     */
    public HistoryRecord nextRecord() throws NoSuchElementException
    {
        return (HistoryRecord) this.next();
    }

    /**
     * Returns the previous element in the iteration.
     *
     * @return the previous element in the iteration.
     * @throws NoSuchElementException iteration has no more elements.
     */
    public Object prev() throws NoSuchElementException
    {
        this.currentPos--;

        if (this.currentPos < 0)
        {
            throw new NoSuchElementException();
        }

        return records.get(this.currentPos);
    }

    /**
     * A strongly-typed variant of <tt>prev()</tt>.
     *
     * @return the previous history record.
     * @throws NoSuchElementException iteration has no more elements.
     */
    public HistoryRecord prevRecord() throws NoSuchElementException
    {
        return (HistoryRecord) this.prev();
    }

    /**
     * Removes from the underlying collection the last element returned by
     * the iterator (optional operation).
     */
    public void remove()
    {
        throw new UnsupportedOperationException("Cannot remove elements "
                + "from underlaying collection.");
    }
}