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

40
LINES

< > BotCompany Repo | #1004213 // ImmL - immutable list with efficient comparison

JavaX fragment (include)

1  
// should be thread-safe by nature
2  
// compare efficiently due to md5 caching
3  
// tested with A = S
4  
static class ImmL<A> extends AbstractList<A> {
5  
  new L<A> l;
6  
  int hash;
7  
  S md5;
8  
  
9  
  *() {}
10  
  *(L<A> _l) { l.addAll(_l); }
11  
  
12  
  // List methods
13  
  
14  
  public int size() {
15  
    ret l.size();
16  
  }
17  
  
18  
  public A get(int i) {
19  
    ret l.get(i);
20  
  }
21  
  
22  
  public synchronized int hashCode() {
23  
    if (hash == 0)
24  
      hash = l.hashCode();
25  
    ret hash;
26  
  }
27  
  
28  
  public bool equals(O o) {
29  
    if (o instanceof ImmL
30  
      && neq(md5(), ((ImmL) o).md5()))
31  
      false;
32  
    ret super.equals(o);
33  
  }
34  
  
35  
  public synchronized S md5() {
36  
    if (md5 == null)
37  
      md5 = main.md5(structure(l));
38  
    ret md5;
39  
  }
40  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004213
Snippet name: ImmL - immutable list with efficient comparison
Eternal ID of this version: #1004213/1
Text MD5: 4b0d2ff1357905a12f743b2a19299803
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-10 16:59:17
Source code size: 733 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 533 / 1213
Referenced in: [show references]