Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1009403 // SynchronizedCollection [not copied from JDK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (132L/1K).

1  
persistable static class SynchronizedCollection<E> implements Collection<E>, Serializable {
2  
    @java.io.Serial
3  
    private static final long serialVersionUID = 3053995032091335093L;
4  
5  
    @SuppressWarnings("serial") // Conditionally serializable
6  
    /*final*/ Collection<E> c;  // Backing Collection
7  
    @SuppressWarnings("serial") // Conditionally serializable
8  
    /*final*/ Object mutex;     // Object on which to synchronize
9  
10  
    SynchronizedCollection(Collection<E> c) {
11  
        this.c = Objects.requireNonNull(c);
12  
        mutex = this;
13  
    }
14  
15  
    SynchronizedCollection(Collection<E> c, Object mutex) {
16  
        this.c = Objects.requireNonNull(c);
17  
        this.mutex = Objects.requireNonNull(mutex);
18  
    }
19  
20  
    public int size() {
21  
        synchronized (mutex) {return c.size();}
22  
    }
23  
    public boolean isEmpty() {
24  
        synchronized (mutex) {return c.isEmpty();}
25  
    }
26  
    public boolean contains(Object o) {
27  
        synchronized (mutex) {return c.contains(o);}
28  
    }
29  
    public Object[] toArray() {
30  
        synchronized (mutex) {return c.toArray();}
31  
    }
32  
    public <T> T[] toArray(T[] a) {
33  
        synchronized (mutex) {return c.toArray(a);}
34  
    }
35  
    public <T> T[] toArray(java.util.function.IntFunction<T[]> f) {
36  
        synchronized (mutex) {return c.toArray(f);}
37  
    }
38  
39  
    public Iterator<E> iterator() {
40  
        return c.iterator(); // Must be manually synched by user!
41  
    }
42  
43  
    public boolean add(E e) {
44  
        synchronized (mutex) {return c.add(e);}
45  
    }
46  
    public boolean remove(Object o) {
47  
        synchronized (mutex) {return c.remove(o);}
48  
    }
49  
50  
    public boolean containsAll(Collection<?> coll) {
51  
        synchronized (mutex) {return c.containsAll(coll);}
52  
    }
53  
    public boolean addAll(Collection<? extends E> coll) {
54  
        synchronized (mutex) {return c.addAll(coll);}
55  
    }
56  
    public boolean removeAll(Collection<?> coll) {
57  
        synchronized (mutex) {return c.removeAll(coll);}
58  
    }
59  
    public boolean retainAll(Collection<?> coll) {
60  
        synchronized (mutex) {return c.retainAll(coll);}
61  
    }
62  
    public void clear() {
63  
        synchronized (mutex) {c.clear();}
64  
    }
65  
    public String toString() {
66  
        synchronized (mutex) {return c.toString();}
67  
    }
68  
    // Override default methods in Collection
69  
    @Override
70  
    public void forEach(java.util.function.Consumer<? super E> consumer) {
71  
        synchronized (mutex) {c.forEach(consumer);}
72  
    }
73  
    @Override
74  
    public boolean removeIf(java.util.function.Predicate<? super E> filter) {
75  
        synchronized (mutex) {return c.removeIf(filter);}
76  
    }
77  
    @Override
78  
    public Spliterator<E> spliterator() {
79  
        return c.spliterator(); // Must be manually synched by user!
80  
    }
81  
    @Override
82  
    public java.util.stream.Stream<E> stream() {
83  
        return c.stream(); // Must be manually synched by user!
84  
    }
85  
    @Override
86  
    public java.util.stream.Stream<E> parallelStream() {
87  
        return c.parallelStream(); // Must be manually synched by user!
88  
    }
89  
    @java.io.Serial
90  
    private void writeObject(ObjectOutputStream s) throws IOException {
91  
        synchronized (mutex) {s.defaultWriteObject();}
92  
    }
93  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, imzmzdywqqli, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009403
Snippet name: SynchronizedCollection [not copied from JDK]
Eternal ID of this version: #1009403/16
Text MD5: 2e31ce5697888aa59138283d59221c79
Transpilation MD5: 277872da4ed21243acc3230188d504e4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-16 17:46:22
Source code size: 3212 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 575 / 2600
Version history: 15 change(s)
Referenced in: [show references]