@Test public void computableMapElementsAreComputable() throws Exception { String key = "KEY"; String string1 = "STRING 1"; String string2 = "STRING 2"; StringClass stringClass = new StringClass(string1); ObservableArrayMap<String, StringClass> strings = new ObservableArrayMap<>(); strings.put(key, stringClass); ObservableField<String> computedString = Cinder.computeField(()->strings.get(key).element.get(), Cinder.observable(strings, StringClass.class, "element")).immediate(); assertEquals(computedString.get(), string1); strings.get(key).element.set(string2); assertEquals(computedString.get(), string2); }
@Test public void simpleObservesObservableMaps() throws Exception { String key1 = "KEY 1"; String string1 = "STRING 1"; String string2 = "STRING 2"; String stringMissing = "MISSING"; StringClass stringClass1 = new StringClass(string1); StringClass stringClass2 = new StringClass(string2); ObservableArrayMap<String, StringClass> observableMap = new ObservableArrayMap<>(); ObservableField<String> observingString = new ObservableField<>(""); Cinder.observe(()-> observingString.set((observableMap.get(key1) != null) ? observableMap.get(key1).element.get() : stringMissing), observableMap); observableMap.put(key1, stringClass1); assertEquals(observingString.get(), string1); observableMap.put(key1, stringClass2); assertEquals(observingString.get(), string2); observableMap.remove(key1); assertEquals(observingString.get(), stringMissing); }
@Test public void stopArrayMapStops() throws Exception { String string1 = "STRING 1"; String string2 = "STRING 2"; int TEST = 43; ObservableArrayMap<String, TestClass> observed = new ObservableArrayMap<>(); TestClass testClass = new TestClass(); testClass.number.set(TEST); observed.put(string1, testClass); CinderComputable computing = Cinder.observable(observed, TestClass.class, "number").immediate(); CinderInt observing = Cinder.computeInt(()-> observed.get(string1).number.get(), computing).immediate(); assertEquals(TEST, observing.get()); computing.stop(); observed.get(string1).number.set(23); assertEquals(TEST, observing.get()); observed.put(string2, new TestClass()); assertEquals(TEST, observing.get()); }
@Test public void takeTakesCorrectTimes() throws Exception { String key1 = "KEY 1"; String string1 = "STRING 1"; String string2 = "STRING 2"; String string3 = "STRING 3"; ObservableField<String> observed = new ObservableField<>(string1); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ list.put(key1, observed.get()); }, observed).take(2).immediate(); assertEquals(string1, observing.get(key1)); observed.set(string2); assertEquals(string2, observing.get(key1)); observed.set(string3); assertEquals(string2, observing.get(key1)); }
@Test public void takeWhileTakesWhileTrue() throws Exception { String badString = "BAD STRING"; String goodString = "GOOD STRING"; String gooderString = "GOODER STRING"; ObservableField<String> observed = new ObservableField<>(""); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ list.put(key1, observed.get()); }, observed).takeWhile(()->observed.get().startsWith("GOOD")); observed.set(goodString); assertEquals(goodString, observing.get(key1)); observed.set(badString); assertEquals(goodString, observing.get(key1)); observed.set(gooderString); assertEquals(goodString, observing.get(key1)); }
@Test public void skipWhileSkipsWhileTrue() throws Exception { String badString = "BAD STRING"; String goodString = "GOOD STRING"; String gooderString = "GOODER STRING"; ObservableField<String> observed = new ObservableField<>(""); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ list.put(key1, observed.get()); }, observed).skipWhile(()->observed.get().startsWith("BAD")); observed.set(badString); assertEquals(0, observing.size()); observed.set(goodString); assertEquals(goodString, observing.get(key1)); observed.set(gooderString); assertEquals(gooderString, observing.get(key1)); }
public static void main(String[] args) { ObservableModel user1 = new ObservableModel(); user1.firstName.set("Google"); int age = user1.age.get(); ObservableArrayMap<String, Object> user2 = new ObservableArrayMap<>(); user2.put("firstName", "Google"); user2.put("lastName", "Inc."); user2.put("age", 17); ObservableArrayList<Object> user3 = new ObservableArrayList<>(); user3.add("Google"); user3.add("Inc."); user3.add(17); }
Listener(final ObservableArrayMap<K, V> observableArrayMap, final Observer<? super ObservableArrayMapOnMapChangeEvent<K, V>> observer) { this.observableArrayMap = observableArrayMap; this.onMapChangedCallback = new ObservableMap.OnMapChangedCallback<ObservableArrayMap<K, V>, K, V>() { @Override public void onMapChanged(ObservableArrayMap<K, V> observableArrayMap, K key) { observer.onNext(ObservableArrayMapOnMapChangeEvent.create(observableArrayMap, key)); } }; }
@Test @UiThreadTest public void mapChange() { final ObservableArrayMap<String, String> observableArrayMap = new ObservableArrayMap<>(); String key = "key"; String value = "value"; ObservableArrayMapOnMapChangeEvent<String, String> next; RecordingObserver<ObservableArrayMapOnMapChangeEvent<String, String>> o = new RecordingObserver<>(); RxObservableArrayMap.mapChanges(observableArrayMap).subscribe(o); o.assertNoMoreEvents(); observableArrayMap.put(key, value); next = o.takeNext(); assertEquals(observableArrayMap, next.observableArrayMap()); assertEquals(key, next.key()); value = "corrupted"; observableArrayMap.put(key, value); next = o.takeNext(); assertEquals(observableArrayMap, next.observableArrayMap()); assertEquals(key, next.key()); observableArrayMap.put(key + "2", value + "2"); o.takeNext(); observableArrayMap.remove(key); next = o.takeNext(); assertEquals(observableArrayMap, next.observableArrayMap()); assertEquals(key, next.key()); observableArrayMap.clear(); next = o.takeNext(); assertEquals(observableArrayMap, next.observableArrayMap()); assertNull(next.key()); o.dispose(); observableArrayMap.put(key, value); o.assertNoMoreEvents(); }
@Test public void computableMapsAreComputable() throws Exception { String key1 = "KEY 1"; String key2 = "KEY 2"; String value1 = "VALUE 1"; String value2 = "VALUE 2"; ObservableArrayMap<String, String> strings = new ObservableArrayMap<>(); strings.put(key1, value1); ObservableInt computedCount = Cinder.computeInt(()->strings.size(), Cinder.observable(strings)).immediate(); assertEquals(computedCount.get(), 1); strings.put(key2, value2); assertEquals(computedCount.get(), 2); }
@Test public void observesObservableMaps() throws Exception { String key1 = "KEY 1"; String string1 = "STRING 1"; String string2 = "STRING 2"; String string3 = "STRING 3"; String stringMissing = "MISSING"; StringClass stringClass1 = new StringClass(string1); StringClass stringClass2 = new StringClass(string2); ObservableArrayMap<String, StringClass> observableMap = new ObservableArrayMap<>(); ObservableField<String> observingString = new ObservableField<>(""); Cinder.observe(()-> observingString.set((observableMap.get(key1) != null) ? observableMap.get(key1).element.get() : stringMissing), observableMap, StringClass.class, "element"); observableMap.put(key1, stringClass1); assertEquals(observingString.get(), string1); observableMap.put(key1, stringClass2); assertEquals(observingString.get(), string2); stringClass2.element.set(string3); assertEquals(observingString.get(), string3); observableMap.remove(key1); assertEquals(observingString.get(), stringMissing); }
@Test public void onceTakesOnce() throws Exception { String string1 = "STRING 1"; String string2 = "STRING 2"; ObservableField<String> observed = new ObservableField<>(string1); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)-> list.put(key1, observed.get()), observed).once().immediate(); assertEquals(string1, observing.get(key1)); observed.set(string2); assertEquals(string1, observing.get(key1)); }
@Test public void skipSkipsCorrectTimes() throws Exception { String string1 = "STRING 1"; String string2 = "STRING 2"; ObservableField<String> observed = new ObservableField<>(); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ list.put(key1, observed.get()); }, observed).skip(1); observed.set(string1); assertEquals(0, observing.size()); observed.set(string2); assertEquals(string2, observing.get(key1)); }
@Test public void filterFilters() throws Exception { String badString = "BAD STRING"; String goodString = "GOOD STRING"; ObservableField<String> observed = new ObservableField<>(""); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ list.put(key1, observed.get()); }, observed).filter(()->observed.get().startsWith("GOOD")); observed.set(badString); assertEquals(0, observing.size()); observed.set(goodString); assertEquals(goodString, observing.get(key1)); }
@Test public void withDefaultAssignsDefault() throws Exception { String testString = "TEST"; ArrayMap<String, String> defaultValue = new ArrayMap<>(); defaultValue.put(key1, testString); ObservableArrayMap<String, String> observing = Cinder.<String, String>computeArrayMap((list)->{ }).withDefault(defaultValue); assertEquals(testString, observing.get(key1)); }
public static <K, V> ObservableArrayMap<K, V> convertArrayMap(Observable<Map<K, V>> observable){ ObservableArrayMap<K, V> observableArrayMap = new ObservableArrayMap<>(); observable.subscribe((Map<K, V> a)->{ observableArrayMap.clear(); observableArrayMap.putAll(a); }); return observableArrayMap; }
@Test public void convertsObservableArrayMapToRx() throws Exception { ObservableArrayMap<String, String> observableStringMap = new ObservableArrayMap<>(); Observable<ArrayMap<String, String>> rxStringMap = RxCinder.convert(observableStringMap); final String TEST_KEY = "TEST_KEY"; final ArrayMap<String, String> testStringMap = new ArrayMap<>(); rxStringMap.subscribe((stringMap)->{ testStringMap.put(TEST_KEY, stringMap.get(TEST_KEY));}); final String TEST_VALUE = "TEST_VALUE"; observableStringMap.put(TEST_KEY, TEST_VALUE); assertTrue(testStringMap.get(TEST_KEY).equals(TEST_VALUE)); }
@Test public void convertsRxMapToObservable() throws Exception { final String TEST_KEY = "Apple"; final String TEST_STRING = "Banana"; ArrayMap<String, String> map = new ArrayMap<>(); map.put(TEST_KEY, TEST_STRING); Observable<Map<String, String>> rxMap= Observable.just(map); ObservableArrayMap<String, String> observableMap = RxCinder.convertArrayMap(rxMap); assertTrue(observableMap.get(TEST_KEY).equals(TEST_STRING)); }
public static <K, V> ObservableArrayMapOnMapChangeEvent<K, V> create( ObservableArrayMap<K, V> observableArrayMap, @Nullable K key) { return new AutoValue_ObservableArrayMapOnMapChangeEvent<>(observableArrayMap, key); }
/** The observable from which this event occurred. */ public abstract ObservableArrayMap<K, V> observableArrayMap();
ObservableArrayMapObservable(ObservableArrayMap<K, V> observableArrayMap) { this.observableArrayMap = observableArrayMap; }
public static <K, V> Observable<ArrayMap<K, V>> convert(ObservableArrayMap<K, V> observableArrayMap){ return Observable.create((e)-> Cinder.observe(()->e.onNext(observableArrayMap), observableArrayMap) ); }
/** * Create an observable of property change events on {@code observableArrayMap}. * <p> * <em>Warning:</em> The created observable keeps a strong reference to {@code * observableArrayMap}. * Unsubscribe to free this reference. */ @CheckResult @NonNull public static <K, V> Observable<ObservableArrayMapOnMapChangeEvent<K, V>> mapChanges( @NonNull ObservableArrayMap<K, V> observableArrayMap) { checkNotNull(observableArrayMap, "observableArrayMap == null"); return new ObservableArrayMapObservable<>(observableArrayMap); }
void onComputeChange(ObservableArrayMap<K, V> map);