Search in sources :

Example 1 with Mutable

use of com.oath.cyclops.util.box.Mutable in project cyclops by aol.

the class MutableShortTest method externalMapInputObj.

@Test
public void externalMapInputObj() {
    value = 0;
    Mutable<Short> ext = MutableShort.fromExternal(() -> value, v -> this.value = v).mapInputToObj(s -> new Short((short) (s + ten)));
    ext.set((short) 50);
    assertThat(value, equalTo((short) 60));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) MutableShort(com.oath.cyclops.util.box.MutableShort) Stream(java.util.stream.Stream) Mutable(com.oath.cyclops.util.box.Mutable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BiFunction(java.util.function.BiFunction) Matchers.not(org.hamcrest.Matchers.not) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Function(java.util.function.Function) MutableShort(com.oath.cyclops.util.box.MutableShort) Test(org.junit.Test)

Example 2 with Mutable

use of com.oath.cyclops.util.box.Mutable in project cyclops by aol.

the class MutableByteTest method externalMapInputObj.

@Test
public void externalMapInputObj() {
    value = 0;
    Mutable<Byte> ext = MutableByte.fromExternal(() -> value, v -> this.value = v).mapInputToObj(s -> new Byte((byte) (s + ten)));
    ext.set((byte) 50);
    assertThat(value, equalTo((byte) 60));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) Stream(java.util.stream.Stream) Mutable(com.oath.cyclops.util.box.Mutable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BiFunction(java.util.function.BiFunction) Matchers.not(org.hamcrest.Matchers.not) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Function(java.util.function.Function) MutableByte(com.oath.cyclops.util.box.MutableByte) MutableByte(com.oath.cyclops.util.box.MutableByte) Test(org.junit.Test)

Example 3 with Mutable

use of com.oath.cyclops.util.box.Mutable in project cyclops by aol.

the class MutableFloatTest method externalMapInputObj.

@Test
public void externalMapInputObj() {
    value = 0;
    Mutable<Float> ext = MutableFloat.fromExternal(() -> value, v -> this.value = v).mapInputToObj(s -> new Float((float) (s + ten)));
    ext.set((float) 50);
    assertThat(value, equalTo((float) 60));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) Stream(java.util.stream.Stream) Mutable(com.oath.cyclops.util.box.Mutable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BiFunction(java.util.function.BiFunction) MutableFloat(com.oath.cyclops.util.box.MutableFloat) Matchers.not(org.hamcrest.Matchers.not) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Function(java.util.function.Function) MutableFloat(com.oath.cyclops.util.box.MutableFloat) Test(org.junit.Test)

Example 4 with Mutable

use of com.oath.cyclops.util.box.Mutable in project cyclops by aol.

the class Future method subscribe.

@Override
public final void subscribe(final Subscriber<? super T> sub) {
    Mutable<Future<T>> future = Mutable.of(this);
    sub.onSubscribe(new Subscription() {

        AtomicBoolean running = new AtomicBoolean(true);

        AtomicBoolean cancelled = new AtomicBoolean(false);

        @Override
        public void request(final long n) {
            if (n < 1) {
                sub.onError(new IllegalArgumentException("3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0."));
            }
            if (!running.compareAndSet(true, false)) {
                return;
            }
            future.mutate(f -> f.peek(sub::onNext).recover(t -> {
                sub.onError(t);
                return null;
            }).peek(i -> sub.onComplete()));
        }

        @Override
        public void cancel() {
            cancelled.set(true);
            future.get().cancel();
        }
    });
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) Higher(com.oath.cyclops.hkt.Higher) To(com.oath.cyclops.types.foldable.To) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) BiFunction(java.util.function.BiFunction) ValueSubscriber(com.oath.cyclops.types.reactive.ValueSubscriber) Zippable(com.oath.cyclops.types.Zippable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Tuple(cyclops.data.tuple.Tuple) RecoverableFrom(com.oath.cyclops.types.recoverable.RecoverableFrom) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NoSuchElementException(java.util.NoSuchElementException) Subscriber(org.reactivestreams.Subscriber) Completable(com.oath.cyclops.types.reactive.Completable) ReactiveTransformable(com.oath.cyclops.types.functor.ReactiveTransformable) CompletableFutures(cyclops.companion.CompletableFutures) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) java.util.concurrent(java.util.concurrent) Publisher(org.reactivestreams.Publisher) Monoid(cyclops.function.Monoid) OrElseValue(com.oath.cyclops.types.OrElseValue) EqualsAndHashCode(lombok.EqualsAndHashCode) BinaryOperator(java.util.function.BinaryOperator) Consumer(java.util.function.Consumer) Reducer(cyclops.function.Reducer) ReactiveSeq(cyclops.reactive.ReactiveSeq) Mutable(com.oath.cyclops.util.box.Mutable) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) DataWitness.future(com.oath.cyclops.hkt.DataWitness.future) Subscription(org.reactivestreams.Subscription) AllArgsConstructor(lombok.AllArgsConstructor) MonadicValue(com.oath.cyclops.types.MonadicValue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Subscription(org.reactivestreams.Subscription)

Aggregations

Mutable (com.oath.cyclops.util.box.Mutable)4 BiFunction (java.util.function.BiFunction)4 Function (java.util.function.Function)4 Stream (java.util.stream.Stream)4 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3 Matchers.is (org.hamcrest.Matchers.is)3 Matchers.not (org.hamcrest.Matchers.not)3 Assert.assertThat (org.junit.Assert.assertThat)3 Test (org.junit.Test)3 DataWitness.future (com.oath.cyclops.hkt.DataWitness.future)1 Higher (com.oath.cyclops.hkt.Higher)1 MonadicValue (com.oath.cyclops.types.MonadicValue)1 OrElseValue (com.oath.cyclops.types.OrElseValue)1 Zippable (com.oath.cyclops.types.Zippable)1 To (com.oath.cyclops.types.foldable.To)1 ReactiveTransformable (com.oath.cyclops.types.functor.ReactiveTransformable)1 Completable (com.oath.cyclops.types.reactive.Completable)1 ValueSubscriber (com.oath.cyclops.types.reactive.ValueSubscriber)1 RecoverableFrom (com.oath.cyclops.types.recoverable.RecoverableFrom)1 MutableByte (com.oath.cyclops.util.box.MutableByte)1