Lightweight observable collections (used by VMF and VRL).
UPDATE 1: we might switch to another event API. Expect changes...
UPDATE 2: we are almost done...
- Observable List
- Mapped List (keeps two lists with different element types in sync)
- coming sooner or later: ObservableMap and ObservableSet
public class Main {
public static void main(String[] args) {
// creates an ordinary list
List<Integer> list = new ArrayList<>();
// to make the list observable, we wrap it in a VList
VList<Integer> vList = VList.newInstance(list);
// to get notified, we add a change listener to the VList
Subscription subscription = vList.addChangeListener((evt) -> {
// for now, we just print the changes
System.out.println(EventUtil.toStringWithDetails(evt));
});
// add individual elements (generates 3 events)
System.out.println(">> add 3 individual elements");
vList.add(1);
vList.add(2);
vList.add(3);
// add collection of elements (generates only one event)
System.out.println(">> add one collection of elements");
vList.addAll(Arrays.asList(4, 5, 6));
// remove individual elements (generates 3 events)
System.out.println(">> remove 3 individual elements");
vList.remove((Integer) 1);
vList.remove((Integer) 2);
vList.remove((Integer) 3);
// remove collection of elements (generates only one event)
System.out.println(">> remove one collection of elements");
vList.removeAll(Arrays.asList(4,5,6));
// unsubscribe the listener from vList
subscription.unsubscribe();
// add elements without generating events
Syste
780F
m.out.println(">> add one collection of elements without notification");
vList.addAll(Arrays.asList(4, 5, 6));
}
}
- Java >= 1.8
- Internet connection (dependencies are downloaded automatically)
- IDE: Gradle Plugin (not necessary for command line usage)
Open the VCollections
Gradle project in your favourite IDE (tested with NetBeans 8.2) and build it
by calling the assemble
task.
Navigate to the Gradle project (e.g., path/to/VCollections
) and enter the following command
bash gradlew assemble
gradlew assemble