OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 177
Line 28... Line 28...
28
import org.openconcerto.utils.cc.IClosure;
28
import org.openconcerto.utils.cc.IClosure;
29
import org.openconcerto.utils.cc.ITransformer;
29
import org.openconcerto.utils.cc.ITransformer;
30
 
30
 
31
import java.util.Collection;
31
import java.util.Collection;
32
import java.util.Deque;
32
import java.util.Deque;
33
import java.util.concurrent.FutureTask;
33
import java.util.concurrent.RunnableFuture;
34
 
34
 
35
import javax.swing.SwingUtilities;
35
import javax.swing.SwingUtilities;
36
 
36
 
37
public final class SearchQueue extends SleepingQueue {
37
public final class SearchQueue extends SleepingQueue {
38
 
38
 
Line 43... Line 43...
43
     * Whether the passed future performs a search.
43
     * Whether the passed future performs a search.
44
     * 
44
     * 
45
     * @param f a task in this queue, can be <code>null</code>.
45
     * @param f a task in this queue, can be <code>null</code>.
46
     * @return <code>true</code> if <code>f</code> searches.
46
     * @return <code>true</code> if <code>f</code> searches.
47
     */
47
     */
48
    public static boolean isSearch(final FutureTask<?> f) {
48
    public static boolean isSearch(final RunnableFuture<?> f) {
49
        final Runnable r = getRunnable(f);
49
        final Runnable r = getRunnable(f);
50
        return r instanceof SearchRunnable && ((SearchRunnable) r).performsSearch();
50
        return r instanceof SearchRunnable && ((SearchRunnable) r).performsSearch();
51
    }
51
    }
52
 
52
 
53
    public static Runnable getRunnable(final FutureTask<?> f) {
53
    public static Runnable getRunnable(final RunnableFuture<?> f) {
54
        if (f instanceof IFutureTask)
54
        if (f instanceof IFutureTask)
55
            return ((IFutureTask<?>) f).getRunnable();
55
            return ((IFutureTask<?>) f).getRunnable();
56
        else
56
        else
57
            return null;
57
            return null;
58
    }
58
    }
Line 73... Line 73...
73
    private final ITableModel model;
73
    private final ITableModel model;
74
    // only accessed within this queue
74
    // only accessed within this queue
75
    SearchSpec search;
75
    SearchSpec search;
76
    private final ListAccess listAccess;
76
    private final ListAccess listAccess;
77
    // thread-safe
77
    // thread-safe
78
    private final IClosure<Deque<FutureTask<?>>> cancelClosure;
78
    private final IClosure<Deque<RunnableFuture<?>>> cancelClosure;
79
 
79
 
80
    public SearchQueue(final ListAccess la) {
80
    public SearchQueue(final ListAccess la) {
81
        super(SearchQueue.class.getName() + " on " + la.getModel());
81
        super(SearchQueue.class.getName() + " on " + la.getModel());
82
        this.listAccess = la;
82
        this.listAccess = la;
83
        this.model = la.getModel();
83
        this.model = la.getModel();
84
        this.search = null;
84
        this.search = null;
85
        this.cancelClosure = UpdateQueue.createCancelClosure(this, new ITransformer<FutureTask<?>, TaskType>() {
85
        this.cancelClosure = UpdateQueue.createCancelClosure(this, new ITransformer<RunnableFuture<?>, TaskType>() {
86
            @Override
86
            @Override
87
            public TaskType transformChecked(FutureTask<?> input) {
87
            public TaskType transformChecked(RunnableFuture<?> input) {
88
                final Runnable r = getRunnable(input);
88
                final Runnable r = getRunnable(input);
89
                if (r instanceof SearchRunnable)
89
                if (r instanceof SearchRunnable)
90
                    return TaskType.COMPUTE;
90
                    return TaskType.COMPUTE;
91
                else if (r instanceof SetStateRunnable)
91
                else if (r instanceof SetStateRunnable)
92
                    return TaskType.SET_STATE;
92
                    return TaskType.SET_STATE;
Line 135... Line 135...
135
        // is updated : the queue would naively contain setSearch, searchAll, searchAll and thus we
135
        // is updated : the queue would naively contain setSearch, searchAll, searchAll and thus we
136
        // can cancel one searchAll. Whereas if the setSearch was contained in searchAll, we
136
        // can cancel one searchAll. Whereas if the setSearch was contained in searchAll, we
137
        // couldn't cancel it.
137
        // couldn't cancel it.
138
        // use tasksDo() so that no other runnable can come between setSearch and searchAll.
138
        // use tasksDo() so that no other runnable can come between setSearch and searchAll.
139
        // Otherwise a runnable might the new search query but not the new filtered list.
139
        // Otherwise a runnable might the new search query but not the new filtered list.
140
        this.tasksDo(new IClosure<Deque<FutureTask<?>>>() {
140
        this.tasksDo(new IClosure<Deque<RunnableFuture<?>>>() {
141
            @Override
141
            @Override
142
            public void executeChecked(Deque<FutureTask<?>> input) {
142
            public void executeChecked(Deque<RunnableFuture<?>> input) {
143
                put(new SetStateRunnable() {
143
                put(new SetStateRunnable() {
144
                    @Override
144
                    @Override
145
                    public void run() {
145
                    public void run() {
146
                        SearchQueue.this.search = s;
146
                        SearchQueue.this.search = s;
147
                    }
147
                    }
Line 162... Line 162...
162
    private void fullDataChange() {
162
    private void fullDataChange() {
163
        this.put(new SearchAll(this));
163
        this.put(new SearchAll(this));
164
    }
164
    }
165
 
165
 
166
    @Override
166
    @Override
167
    protected void willPut(final FutureTask<?> qr) throws InterruptedException {
167
    protected void willPut(final RunnableFuture<?> qr) throws InterruptedException {
168
        if (getRunnable(qr) instanceof SearchAll) {
168
        if (getRunnable(qr) instanceof SearchAll) {
169
            // si on recherche tout, ne sert à rien de garder les recherches précédentes.
169
            // si on recherche tout, ne sert à rien de garder les recherches précédentes.
170
            this.tasksDo(this.cancelClosure);
170
            this.tasksDo(this.cancelClosure);
171
        }
171
        }
172
    }
172
    }