-
-
Save WindSekirun/49fe273ee5c1feef06e2be6610c61480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.annimon.stream.Stream; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
public class ItemChangedList<T> extends ArrayList<T> { | |
private List<OnPropertyChangedObserver<T>> _changedObserverList = new ArrayList<>(); | |
public static final String ITEM_ADDED = "bbed36af-0b7b-4e53-abc9-02d6a14d7f34"; | |
public static final String ITEM_REMOVED = "7390116e-586d-4e62-9343-5b82b0a8c6c5"; | |
@Override | |
public boolean add(T item) { | |
sendPropertyChanged(item, ITEM_ADDED); | |
return super.add(item); | |
} | |
@Override | |
public Iterator<T> iterator() { | |
return super.iterator(); | |
} | |
@Override | |
public void clear() { | |
for (T item : this) { | |
sendPropertyChanged(item, ITEM_REMOVED); | |
} | |
super.clear(); | |
} | |
@Override | |
public boolean contains(Object item) { | |
return super.contains(item); | |
} | |
@Override | |
public boolean remove(Object item) { | |
sendPropertyChanged(item, ITEM_REMOVED); | |
return super.remove(item); | |
} | |
@Override | |
public int indexOf(Object item) { | |
return super.indexOf(item); | |
} | |
@Override | |
public T get(int index) { | |
return super.get(index); | |
} | |
@Override | |
public T set(int index, T item) { | |
return super.set(index, item); | |
} | |
public void copyTo(T[] array, int arrayIndex) { | |
// Using https://github.com/aNNiMON/Lightweight-Stream-API | |
addAll(Stream.of(array).skip(arrayIndex).toList()); | |
// Traditional Java way | |
// addAll(Arrays.stream(array).skip(arrayIndex).collect(Collectors.toList())); | |
} | |
public int count() { | |
return super.size(); | |
} | |
public boolean isReadOnly() { | |
return false; | |
} | |
public void insert(int index, T item) { | |
sendPropertyChanged(item, ITEM_ADDED); | |
super.add(index, item); | |
} | |
public void removeAt(int index) { | |
super.remove(index); | |
} | |
public void addObserver(OnPropertyChangedObserver<T> observer) { | |
_changedObserverList.add(observer); | |
} | |
public void removeObserver(OnPropertyChangedObserver<T> observer) { | |
_changedObserverList.remove(observer); | |
} | |
public void clearObserver() { | |
_changedObserverList.clear(); | |
} | |
private void sendPropertyChanged(Object item, String args) { | |
for (OnPropertyChangedObserver<T> observer : _changedObserverList) { | |
observer.onChanged(item, args); | |
} | |
} | |
public interface OnPropertyChangedObserver<T> { | |
void onChanged(Object item, String args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment