public void bsync(NativeObject jsRWB) { Map<String, Object> jRWB = (Map) Context.jsToJava(jsRWB, Map.class); RWBStatement stmt = RWBStatement.make(); Object req = jRWB.get("request"); if (req != null) { if (req instanceof BEvent) { stmt = stmt.request((BEvent) req); } else if (req instanceof NativeArray) { NativeArray arr = (NativeArray) req; stmt = stmt.request( Arrays.asList(arr.getIndexIds()) .stream() .map(i -> (BEvent) arr.get(i)) .collect(toSet())); } } stmt = stmt.waitFor(convertToEventSet(jRWB.get("waitFor"))) .block(convertToEventSet(jRWB.get("block"))) .breakUpon(convertToEventSet(jRWB.get("breakUpon"))); bthread.bsync(stmt); }
private EventSet convertToEventSet(Object jsObject) { if (jsObject == null) return Events.emptySet; // This covers event sets AND events. if (jsObject instanceof EventSet) { return (EventSet) jsObject; } else if (jsObject instanceof NativeArray) { NativeArray arr = (NativeArray) jsObject; if (Stream.of(arr.getIds()).anyMatch(id -> arr.get(id) == null)) { throw new RuntimeException("EventSet Array contains null sets."); } return ComposableEventSet.anyOf( Arrays.asList(arr.getIndexIds()) .stream() .map(i -> (EventSet) arr.get(i)) .collect(toSet())); } else { final String errorMessage = "Cannot convert " + jsObject + " of class " + jsObject.getClass() + " to an event set"; Logger.getLogger(BThread.class.getName()).log(Level.SEVERE, errorMessage); throw new IllegalArgumentException(errorMessage); } }
@Test public void getIndexIdsShouldIncludeIdsLessThan2ToThe32() { int maxIndex = (int) (1L << 31) - 1; array.put(maxIndex, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {maxIndex})); }
@Test public void getIndexIdsShouldNotIncludeNegativeIds() { array.put(-1, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }
@Test public void getIndexIdsShouldWorkWhenIndicesSetAsString() { array.put("0", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {0})); }
@Test public void getIndexIdsShouldBeAZeroForSimpleSingletonArray() { array.put(0, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {0})); }
@Test public void getIndexIdsShouldNotReturnNonNumericIds() { array.put("x", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }
@Test public void getIndexIdsShouldNotIncludeIdsGreaterThanOrEqualTo2ToThe32() { array.put((1L << 31) + "", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }