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

81
LINES

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

JavaX fragment (include)

sclass SynchronizedCollection<A> implements Collection<A> {
  Collection<A> c;  // Backing Collection
  Object mutex;     // Object on which to synchronize

  *() {} // for persistence
  *(Collection<A> c) {
      this.c = Objects.requireNonNull(c);
      mutex = this;
  }

  *(Collection<A> c, Object mutex) {
      this.c = Objects.requireNonNull(c);
      this.mutex = Objects.requireNonNull(mutex);
  }

  public int size() {
      synchronized (mutex) {return c.size();}
  }
  public boolean isEmpty() {
      synchronized (mutex) {return c.isEmpty();}
  }
  public boolean contains(Object o) {
      synchronized (mutex) {return c.contains(o);}
  }
  public Object[] toArray() {
      synchronized (mutex) {return c.toArray();}
  }
  public <T> T[] toArray(T[] a) {
      synchronized (mutex) {return c.toArray(a);}
  }

  public Iterator<A> iterator() {
      return c.iterator(); // Must be manually synched by user!
  }

  public boolean add(A A) {
      synchronized (mutex) {return c.add(A);}
  }
  public boolean remove(Object o) {
      synchronized (mutex) {return c.remove(o);}
  }

  public boolean containsAll(Collection<?> coll) {
      synchronized (mutex) {return c.containsAll(coll);}
  }
  public boolean addAll(Collection<? extends A> coll) {
      synchronized (mutex) {return c.addAll(coll);}
  }
  public boolean removeAll(Collection<?> coll) {
      synchronized (mutex) {return c.removeAll(coll);}
  }
  public boolean retainAll(Collection<?> coll) {
      synchronized (mutex) {return c.retainAll(coll);}
  }
  public void clear() {
      synchronized (mutex) {c.clear();}
  }
  public String toString() {
      synchronized (mutex) {return c.toString();}
  }
  
  /*public void forEach(Consumer<? super A> consumer) {
      synchronized (mutex) {c.forEach(consumer);}
  }*/
 
  /*public boolean removeIf(Predicate<? super A> filter) {
      synchronized (mutex) {return c.removeIf(filter);}
  }*/
  @Override
  public Spliterator<A> spliterator() {
      return c.spliterator(); // Must be manually synched by user!
  }
  
  /*public Stream<A> stream() {
      return c.stream(); // Must be manually synched by user!
  }*/
  
  /*public Stream<A> parallelStream() {
      return c.parallelStream(); // Must be manually synched by user!
  }*/
}

Author comment

Began life as a copy of #1009403

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031169
Snippet name: SynchronizedCollection [not copied from JDK]
Eternal ID of this version: #1031169/1
Text MD5: 89286b89521102e4a53beeae44076beb
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:43:24
Source code size: 2354 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 67 / 96
Referenced in: [show references]