code: public static <T, R> Transformer<T, R> collectWhile(final Func0<R> factory,
              final Action2<? super R, ? super T> collect,
              final Func2<? super R, ? super T, Boolean> condition,
              final Func1<? super R, Boolean> isEmpty) {
          Func3<R, T, Observer<R>, R> transition = new Func3<R, T, Observer<R>, R>() {
  
              @Override
              public R call(R collection, T t, Observer<R> observer) {
  
                  if (condition.call(collection, t)) {
                      collect.call(collection, t);
                      return collection;
                  } else {
                      observer.onNext(collection);
                      R r = factory.call();
                      collect.call(r, t);
                      return r;
                  }
              }
  
          };
          Func2<R, Observer<R>, Boolean> completionAction = new Func2<R, Observer<R>, Boolean>() {
              @Override
              public Boolean call(R collection, Observer<R> observer) {
                  if (!isEmpty.call(collection)) {
                      observer.onNext(collection);
                  }
                  return true;
              }
          };
          return Transformers.stateMachine(factory, transition, completionAction);
      }
docs: <p>
  Returns a {@link Transformer} that returns an {@link Observable} that is
  collected into {@code Collection} instances created by {@code factory}
  that are emitted when the collection and latest emission do not satisfy
  {@code condition} or on completion.
  
  <p>
  <img src=
  "https://github.com/davidmoten/rxjava-extras/blob/master/src/docs/collectWhile.png?raw=true"
  alt="marble diagram">
  
  @param factory
  collection instance creator
  @param collect
  collection action
  @param condition
  returns true if and only if emission should be collected in
  current collection being prepared for emission
  @param isEmpty
  indicates that the collection is empty
  @param <T>
  generic type of source observable
  @param <R>
  collection type emitted by transformed Observable
  @return transformer as above