~people-project/people-core/deep-refactoring

« back to all changes in this revision

Viewing changes to libs/utils/async-search.vala

  • Committer: Ali Sabil
  • Date: 2009-01-29 22:35:23 UTC
  • Revision ID: ali.sabil@gmail.com-20090129223523-f834u01bufze5r0e
- Removed the libs/ folder, depending now on libcore to provide these features

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* async-search.vala
2
 
 *
3
 
 * Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
4
 
 * Copyright (C) 2008 Ali Sabil <ali.sabil@gmail.com>
5
 
 *
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation; either
9
 
 * version 2.1 of the License, or (at your option) any later version.
10
 
 *
11
 
 * This library is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * Lesser General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this library; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 
 *
20
 
 */
21
 
 
22
 
using Gee;
23
 
 
24
 
 
25
 
namespace People.Utils {
26
 
 
27
 
        /**
28
 
         * Class representing an asynchronous search, an instance of this class
29
 
         * is returned by asynchronous query methods.
30
 
         */
31
 
        public class AsyncSearch<T> : Object {
32
 
                private bool started = false;
33
 
                private bool finished = false;
34
 
 
35
 
                private Gee.List<T> buffered_hits;
36
 
 
37
 
                /**
38
 
                 * Signal emitted when a new hit is found.
39
 
                 *
40
 
                 * @param hit the new hit found
41
 
                 */
42
 
                public signal void hit_found(T hit);
43
 
 
44
 
                /**
45
 
                 * Signal emitted when most of the hits for this AsyncSearch have been
46
 
                 * found.
47
 
                 */
48
 
                public signal void done();
49
 
 
50
 
                /**
51
 
                 * Makes this AsyncSearch start emitting signals.
52
 
                 * <p>
53
 
                 * Buffered hits found before this method is called are now propagated
54
 
                 * through several {@link AsyncSearch#hit_found} emissions.
55
 
                 * <p>
56
 
                 * If the AsyncSearch was marked as done before this method is called,
57
 
                 * then {@link AsyncSearch#done} is emitted.
58
 
                 */
59
 
                public void start() {
60
 
                        return_if_fail (started != true);
61
 
                        started = true;
62
 
 
63
 
                        if (buffered_hits != null) {
64
 
                                foreach(T hit in buffered_hits)
65
 
                                        hit_found(hit);
66
 
                                buffered_hits.clear();
67
 
                        }
68
 
 
69
 
                        if (finished)
70
 
                                done();
71
 
                }
72
 
 
73
 
                /**
74
 
                 * Makes this AsyncSearch stop emitting signals.
75
 
                 *
76
 
                 * FIXME: for now, only suspends the signal emissions, as the hits
77
 
                 *        are buffered again. If this is the wanted behavior, we
78
 
                 *        should change the name of that method. If the wanted
79
 
                 *        behavior is to actually stop the research for new hits,
80
 
                 *        then we need to find a way to do so.
81
 
                 */
82
 
                public void stop() {
83
 
                        started = false;
84
 
                }
85
 
 
86
 
                /**
87
 
                 * Marks this AsyncSearch as finished and emits
88
 
                 * {@link AsyncSearch#done}.
89
 
                 * <p>
90
 
                 * This method should only be used from the entity populating this
91
 
                 * AsyncSearch.
92
 
                 */
93
 
                public void finish() {
94
 
                        finished = true;
95
 
                        if (started)
96
 
                                done();
97
 
                }
98
 
 
99
 
                /**
100
 
                 * Adds a hit to this AsyncSearch.
101
 
                 * <p>
102
 
                 * If this AsyncSearch is started, then the hit is propagated through
103
 
                 * a {@link AsyncSearch#hit_found} emission. Else, the hit is buffered.
104
 
                 * <p>
105
 
                 * This method should only be used from the entity populating this
106
 
                 * AsyncSearch.
107
 
                 */
108
 
                public void add_hit(T hit) {
109
 
                        if (started)
110
 
                                hit_found(hit);
111
 
                        else {
112
 
                                if (buffered_hits == null)
113
 
                                        buffered_hits = new Gee.ArrayList<T>();
114
 
                                buffered_hits.add(hit);
115
 
                        }
116
 
                }
117
 
        }
118
 
 
119
 
}