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

53
LINES

< > BotCompany Repo | #1001495 - AList (a synchronized object list with history)

JavaX fragment (include)

!include #1001196 // Lisp

static class AList<A> implements Iterable<A> {
  new L<A> data; // don't access this from outside
  
  // these are ok to access (but not change) from outside
  final S id = randomID(8);
  final L<Lisp> history = synchroList();
  
  public synchronized void add(A a) {
    data.add(a);
    history.add(lisp("add", structure(a)));
  }
  
  public synchronized void clear() {
    data.clear();
    history.add(lisp("clear"));
  }
  
  public synchronized void remove(int i) {
    data.remove(i);
    history.add(lisp("remove", "" + i));
  }
  
  public synchronized void remove(A a) {
    int i = data.indexOf(a);
    if (i >= 0) remove(i);
  }
  
  public synchronized int indexOf(A a) {
    return data.indexOf(a);
  }
  
  public synchronized A get(int i) {
    return data.get(i);
  }
  
  public S getID() {
    return id;
  }
  
  public synchronized int size() {
    return data.size();
  }
  
  public synchronized Iterator<A> iterator() {
    ret new ArrayList<A>(data).iterator();
  }
  
  public synchronized List<A> shoot() {
    ret new ArrayList<A>(data);
  }
}

Author comment

Began life as a copy of #1001414

download  show line numbers  debug dex   

Travelled to 9 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1001495
Snippet name: AList (a synchronized object list with history)
Eternal ID of this version: #1001495/1
Text MD5: 8ec9f01afcc0ce6630453be106f5bca6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-20 23:50:49
Source code size: 1152 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 377 / 419
Referenced in: [show references]

Formerly at http://tinybrain.de/1001495 & http://1001495.tinybrain.de