litesoft
@ 947
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.util; import java.util.*; public class CopyOnWriteUniqueIdentityListManager<E> { List<E> mEntries = Collections.emptyList(); public synchronized List<E> getAll() { return mEntries; } public synchronized void removeAll() { mEntries = Collections.emptyList(); } /** * @return True if pE actually added */ public synchronized boolean add( E pE ) { if ( (pE != null) && !contains( pE ) ) { List<E> zNewList = new ArrayList<E>( mEntries ); zNewList.add( pE ); mEntries = Collections.unmodifiableList( zNewList ); return true; } return false; } /** * @return True if pE actually removed */ public synchronized boolean remove( E pE ) { int zAt = indexOf( pE ); if ( zAt != -1 ) { if ( mEntries.size() == 1 ) { mEntries = Collections.emptyList(); } else { List<E> zNewList = new ArrayList<E>(); int i = 0; for (; i < zAt; i++ ) { zNewList.add( mEntries.get( i ) ); } while ( ++i < mEntries.size() ) { zNewList.add( mEntries.get( i ) ); } mEntries = Collections.unmodifiableList( zNewList ); } return true; } return false; } private int indexOf( E pE ) { if ( pE != null ) { List<E> zList = getAll(); for ( int i = 0; i < zList.size(); i++ ) { if ( pE == zList.get( i ) ) { return i; } } } return -1; } private boolean contains( E pE ) { return -1 != indexOf( pE ); } } |