OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 180
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
16
import org.openconcerto.utils.cc.IClosure;
16
import org.openconcerto.utils.cc.IClosure;
17
 
17
 
18
import java.util.Collection;
-
 
19
import java.util.Deque;
18
import java.util.Deque;
20
import java.util.LinkedList;
19
import java.util.LinkedList;
21
import java.util.concurrent.locks.Condition;
20
import java.util.concurrent.locks.Condition;
22
import java.util.concurrent.locks.Lock;
21
import java.util.concurrent.locks.Lock;
23
import java.util.concurrent.locks.ReentrantLock;
22
import java.util.concurrent.locks.ReentrantLock;
-
 
23
import java.util.function.Function;
24
 
24
 
25
import net.jcip.annotations.GuardedBy;
25
import net.jcip.annotations.GuardedBy;
26
import net.jcip.annotations.ThreadSafe;
26
import net.jcip.annotations.ThreadSafe;
27
 
27
 
28
/**
28
/**
Line 221... Line 221...
221
            this.itemsLock.unlock();
221
            this.itemsLock.unlock();
222
        }
222
        }
223
    }
223
    }
224
 
224
 
225
    public final void eachItemDo(final IClosure<T> c) {
225
    public final void eachItemDo(final IClosure<T> c) {
226
        this.itemsDo(new IClosure<Collection<T>>() {
226
        this.itemsDo((items) -> {
227
            @Override
-
 
228
            public void executeChecked(Collection<T> items) {
-
 
229
                for (final T t : items) {
227
            for (final T t : items) {
230
                    c.executeChecked(t);
228
                c.executeChecked(t);
231
                }
-
 
232
            }
229
            }
-
 
230
            return null;
233
        });
231
        });
234
    }
232
    }
235
 
233
 
236
    /**
234
    /**
237
     * Allows <code>c</code> to arbitrarily modify our queue as it is locked during this method.
235
     * Allows <code>c</code> to arbitrarily modify our queue as it is locked during this method.
238
     * I.e. no items will be removed (passed to the closure) nor added.
236
     * I.e. no items will be removed (passed to the closure) nor added.
239
     * 
237
     * 
240
     * @param c what to do with our queue.
238
     * @param c what to do with our queue.
241
     */
239
     */
242
    public final void itemsDo(IClosure<? super Deque<T>> c) {
240
    public final void itemsDo(IClosure<? super Deque<T>> c) {
-
 
241
        this.itemsDo((q) -> {
-
 
242
            c.executeChecked(q);
-
 
243
            return null;
-
 
244
        });
-
 
245
    }
-
 
246
 
-
 
247
    public final <R> R itemsDo(Function<? super Deque<T>, R> c) {
243
        this.itemsLock.lock();
248
        this.itemsLock.lock();
244
        try {
249
        try {
245
            c.executeChecked(this.items);
250
            final R res = c.apply(this.items);
246
            if (!this.items.isEmpty())
251
            if (!this.items.isEmpty())
247
                this.notEmpty.signal();
252
                this.notEmpty.signal();
-
 
253
            return res;
248
        } finally {
254
        } finally {
249
            this.itemsLock.unlock();
255
            this.itemsLock.unlock();
250
        }
256
        }
251
    }
257
    }
252
 
258