/** * Slice this buffer at the given positions. Useful for extracting multiple segments of data from * a buffer when the exact indices of that data is already known. * * @param positions The start and end positions of the slices. * @return A list of {@link View Views} pointing to the slices. */ public List<View> slice(int... positions) { Assert.notNull(positions, "Positions cannot be null."); if (positions.length == 0) { return Collections.emptyList(); } snapshot(); List<View> views = new ArrayList<View>(); int len = positions.length; for (int i = 0; i < len; i++) { int start = positions[i]; int end = (i + 1 < len ? positions[++i] : this.limit); views.add(createView(start, end)); reset(); } return views; }
@Test public void indexBugTest() throws InterruptedException { int numOfItems = 20; // this line causes an java.lang.ArrayIndexOutOfBoundsException unless there is a break point in // ZipAction // .createSubscriber() RingBufferProcessor<String> ring = RingBufferProcessor.create("test", 1024); // this line works // Broadcaster<String> ring = Broadcaster.create(Environment.get()); Stream<String> stream = Streams.wrap(ring.start()); Stream<String> stream2 = stream .zipWith( Streams.createWith( (d, s) -> { for (int i = 0; i < d; i++) { s.onNext(System.currentTimeMillis()); } }), t -> String.format("%s : %s", t.getT2(), t.getT1())) .observeError( Throwable.class, (o, t) -> { System.err.println(t.toString()); t.printStackTrace(); }); Promise<List<String>> p = stream2.observe(System.out::println).buffer(numOfItems).next(); for (int curr = 0; curr < numOfItems; curr++) { if (curr % 5 == 0 && curr % 3 == 0) ring.onNext("FizBuz"); else if (curr % 3 == 0) ring.onNext("Fiz"); else if (curr % 5 == 0) ring.onNext("Buz"); else ring.onNext(String.valueOf(curr)); } Assert.isTrue(p.await(5, TimeUnit.SECONDS) != null, "Has not returned list"); }
/** * Creates a new {@code NotifyConsumer} that will notify the given {@code observable} using the * given {@code notifyKey}. If {@code notifyKey} is {@code null}, {@code observable} will be * notified without a key. * * @param notifyKey The notification key, may be {@code null} * @param observable The observable to notify. May not be {@code null} */ public NotifyConsumer(Object notifyKey, Bus<?, ?> observable) { Assert.notNull(observable, "Observable cannot be null."); this.notifyKey = notifyKey; this.observable = observable; this.wrapEvent = EventBus.class.isAssignableFrom(observable.getClass()); }