com.google.android.agera.Supplier Java Examples

The following examples show how to use com.google.android.agera.Supplier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleActivityA.java    From AndroidAgeraTutorial with Apache License 2.0 6 votes vote down vote up
private void setUpRepository() {
    mObservable = new OnClickObservable() {
        @Override
        public void onClick( ) {
            dispatchUpdate();
        }
    };
    Supplier<Result<Integer>> supplier = new Supplier<Result<Integer>>() {
        @NonNull
        @Override
        public Result<Integer> get() {
            return Result.success(MockRandomData.getRandomColor());
        }
    };

    mRepository = Repositories.repositoryWithInitialValue(Result.<Integer>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            .thenGetFrom(supplier)
            .compile();
}
 
Example #2
Source File: SimpleActivityH.java    From AndroidAgeraTutorial with Apache License 2.0 6 votes vote down vote up
private void setUpRepository() {
    mExecutor = Executors.newSingleThreadExecutor();
    mReservoir = Reservoirs.reservoir();
    Supplier<Result<Integer>> supplier = new Supplier<Result<Integer>>() {
        @NonNull
        @Override
        public Result<Integer> get() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mReservoir.get();// consume receiver
            return Result.success(++mCount);
        }
    };
    mRepository = Repositories.repositoryWithInitialValue(Result.<Integer>absent())
            .observe(mReservoir)
            .onUpdatesPerLoop()
            .goTo(mExecutor)
            .thenGetFrom(supplier)
            .compile();
}
 
Example #3
Source File: AgeraCallAdapterFactoryTest.java    From retrofit-agera-call-adapter with Apache License 2.0 6 votes vote down vote up
@Test public void responseTypes() {
    Type oBodyClass = new TypeToken<Supplier<Result<String>>>() {}.getType();
    assertThat(factory.get(oBodyClass, NO_ANNOTATIONS, retrofit).responseType(),
        equalTo(new TypeToken<String>() {}.getType()));

    Type oBodyWildcard = new TypeToken<Supplier<Result<? extends String>>>() {}.getType();
    assertThat(factory.get(oBodyWildcard, NO_ANNOTATIONS, retrofit).responseType(),
        equalTo(new TypeToken<String>() {}.getType()));

    Type oBodyGeneric = new TypeToken<Supplier<Result<List<String>>>>() {}.getType();
    assertThat(factory.get(oBodyGeneric, NO_ANNOTATIONS, retrofit).responseType(),
        equalTo(new TypeToken<List<String>>() {}.getType()));

    Type oResponseClass = new TypeToken<Supplier<Result<Response<String>>>>() {}.getType();
    assertThat(factory.get(oResponseClass, NO_ANNOTATIONS, retrofit).responseType(),
        equalTo(new TypeToken<String>() {}.getType()));

    Type oResponseWildcard
        = new TypeToken<Supplier<Result<Response<? extends String>>>>() {}.getType();
    assertThat(factory.get(oResponseWildcard, NO_ANNOTATIONS, retrofit).responseType(),
        equalTo(new TypeToken<String>() {}.getType()));
}
 
Example #4
Source File: AgeraCallAdapterFactoryTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void noUseResultAsFirstInnerTypeThrows() {
    Type reservoirType = new TypeToken<Supplier<String>>() {}.getType();
    try {
        factory.get(reservoirType, NO_ANNOTATIONS, retrofit);
        fail();
    } catch (IllegalStateException e) {
        assertThat(e, hasMessage(containsString(
            "Supplier return type must be parameterized as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>")));
    }
}
 
Example #5
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void shouldReturnSuccessWithNullBodyResponse() throws Exception {
    MockResponse mockResponse = new MockResponse().setStatus(STATUS_NO_CONTENT);
    assertNull(mockResponse.getBody());
    server.enqueue(mockResponse);

    Supplier<Result<Response<Void>>> supplier = service.responseOfDeleteXXX();
    Result<Response<Void>> responseResult = supplier.get();
    assertTrue(responseResult.succeeded());

    Response<Void> response = responseResult.get();
    assertEquals(response.code(), 204);
    assertNull(response.body());
}
 
Example #6
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void shouldReturnAbsentIfNullBody() throws Exception {
    MockResponse mockResponse = new MockResponse().setStatus(STATUS_NO_CONTENT);
    assertNull(mockResponse.getBody());
    server.enqueue(mockResponse);

    Supplier<Result<Void>> supplier = service.deleteXXX();
    Result<Void> result = supplier.get();
    assertTrue(result.isAbsent());
}
 
Example #7
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void responseFailure() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST));

    Supplier<Result<Response<String>>> supplier = service.response();
    try {
        supplier.get().get();
        fail();
    } catch (FailedResultException e) {
        assertThat(e.getCause(), instanceOf(IOException.class));
    }
}
 
Example #8
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void responseSuccess404() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("error"));

    Supplier<Result<Response<String>>> supplier = service.response();
    try {
        supplier.get().get();
        fail();
    } catch (FailedResultException e) {
        assertThat(e.getCause(), instanceOf(HttpException.class));
        assertThat(e.getCause(), hasMessage(containsString("HTTP 404 Client Error")));
    }
}
 
Example #9
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a sql query {@link Function}.
 */
@NonNull
public static <T> Function<SqlRequest, Result<List<T>>> databaseQueryFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database,
    @NonNull Function<Cursor, T> rowMap) {
  return new DatabaseFunction<>(database, new DatabaseQueryMerger<>(rowMap));
}
 
Example #10
Source File: AgeraCallAdapterFactoryTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void rawResponseTypeThrows() {
    Type reservoirType = new TypeToken<Supplier<Result<Response>>>() {}.getType();
    try {
        factory.get(reservoirType, NO_ANNOTATIONS, retrofit);
        fail();
    } catch (IllegalStateException e) {
        assertThat(e, hasMessage(containsString(
            "Response must be parameterized as Response<Foo> or Response<? extends Foo>")));
    }
}
 
Example #11
Source File: AgeraCallAdapterFactoryTest.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Test public void rawBodyTypeThrows() {
    Type reservoirType = new TypeToken<Supplier>() {}.getType();
    try {
        factory.get(reservoirType, NO_ANNOTATIONS, retrofit);
        fail();
    } catch (IllegalStateException e) {
        assertThat(e, hasMessage(containsString(
            "Supplier return type must be parameterized as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>")));
    }
}
 
Example #12
Source File: AgeraCallAdapterFactory.java    From retrofit-agera-call-adapter with Apache License 2.0 5 votes vote down vote up
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Supplier.class) {
        return null;
    }
    if (!(returnType instanceof ParameterizedType)) {
        throw new IllegalStateException("Supplier return type must be parameterized"
            + " as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>");
    }

    Type innerType = getParameterUpperBound(0, (ParameterizedType) returnType);
    if (getRawType(innerType) != Result.class) {
        throw new IllegalStateException("Supplier return type must be parameterized"
            + " as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>");
    }
    Type innerTypeOfInnerType = getParameterUpperBound(0, (ParameterizedType) innerType);
    if (getRawType(innerTypeOfInnerType) != Response.class) {
        // Generic type is not Response<T>. Use it for body-only adapter.
        return new BodyCallAdapter(innerTypeOfInnerType);
    }

    // Generic type is Response<T>. Extract T and create the Response version of the adapter.
    if (!(innerTypeOfInnerType instanceof ParameterizedType)) {
        throw new IllegalStateException("Response must be parameterized"
            + " as Response<Foo> or Response<? extends Foo>");
    }
    Type responseType = getParameterUpperBound(0, (ParameterizedType) innerTypeOfInnerType);
    return new ResponseCallAdapter(responseType);
}
 
Example #13
Source File: SimpleActivityB.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    mObservable = new OnClickObservable() {
        @Override
        public void onClick( ) {
            dispatchUpdate();
        }
    };

    Supplier<String> imageUriSupplier = new Supplier<String>() {
        @NonNull
        @Override
        public String get() {
            return MockRandomData.getRandomImage();
        }
    };

    mRepository = Repositories.repositoryWithInitialValue(Result.<Bitmap>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            .getFrom(imageUriSupplier)
            .goTo(networkExecutor)
            .thenTransform(new Function<String, Result<Bitmap>>() {
                @NonNull
                @Override
                public Result<Bitmap> apply(@NonNull String input) {
                    return new ImageSupplier(input).get();
                }
            })
            .compile();
}
 
Example #14
Source File: RepositoryAdapterRecycleViewActivity.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    mObservable = new SimpleObservable() { };


    mRepository = Repositories.repositoryWithInitialValue(Result.<List<GirlInfo>>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            .goTo(networkExecutor)
            .getFrom(new GirlsSupplier(new Supplier<Integer>() {
                @NonNull
                @Override
                public Integer get() {
                    return mPagination;
                }
            }))
            .thenTransform(new Function<Result<ApiResult<GirlInfo>>, Result<List<GirlInfo>>>() {
                @NonNull
                @Override
                public Result<List<GirlInfo>> apply(@NonNull Result<ApiResult<GirlInfo>> input) {
                    if (input.succeeded() && !input.get().error) {
                        return Result.success(input.get().results);
                    } else {
                        return Result.absent();
                    }
                }
            })
            .onDeactivation(RepositoryConfig.SEND_INTERRUPT)
            .compile();
}
 
Example #15
Source File: RecycleViewActivity.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    uiExecutor = UiThreadExecutor.newUiThreadExecutor();
    mObservable = new SimpleObservable() { };


    mRepository = Repositories.repositoryWithInitialValue(Result.<ApiResult<GirlInfo>>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            //.goTo(uiExecutor)
            .getFrom(new Supplier<Object>() {
                @NonNull
                @Override
                public Object get() {
                    Toast.makeText(RecycleViewActivity.this, "load data begin", Toast.LENGTH_LONG).show();
                    return new Object();
                }
            })
            .goTo(networkExecutor)
            .getFrom(new GirlsSupplier(new Supplier<Integer>() {
                @NonNull
                @Override
                public Integer get() {
                    return mPagination;
                }
            }))

            .goTo(uiExecutor)
            .thenTransform(new Function<Result<ApiResult<GirlInfo>>, Result<ApiResult<GirlInfo>>>() {
                @NonNull
                @Override
                public Result<ApiResult<GirlInfo>> apply(@NonNull Result<ApiResult<GirlInfo>> input) {
                    Toast.makeText(RecycleViewActivity.this, "load data end", Toast.LENGTH_LONG).show();
                    return input;
                }
            })
            .onDeactivation(RepositoryConfig.SEND_INTERRUPT)
            .compile();
}
 
Example #16
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
DatabaseFunction(@NonNull final Supplier<Result<SQLiteDatabase>> databaseSupplier,
    @NonNull final Merger<SQLiteDatabase, R, Result<T>>
        databaseWithSqlArgumentMerger) {
  this.databaseSupplier = checkNotNull(databaseSupplier);
  this.databaseWithSqlArgument = checkNotNull(databaseWithSqlArgumentMerger);
}
 
Example #17
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql update {@link Function}.
 */
@NonNull
public static Function<SqlUpdateRequest, Result<Integer>> databaseUpdateFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseUpdateMerger());
}
 
Example #18
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql insert {@link Function}.
 */
@NonNull
public static Function<SqlInsertRequest, Result<Long>> databaseInsertFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseInsertMerger());
}
 
Example #19
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql delete {@link Function}.
 */
@NonNull
public static Function<SqlDeleteRequest, Result<Integer>> databaseDeleteFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseDeleteMerger());
}
 
Example #20
Source File: SupplierGives.java    From agera with Apache License 2.0 4 votes vote down vote up
@NonNull
@Factory
public static <T> Matcher<Supplier<T>> has(@NonNull final T value) {
  return new SupplierGives<>(value);
}
 
Example #21
Source File: SupplierGives.java    From agera with Apache License 2.0 4 votes vote down vote up
@NonNull
@Factory
public static <T> Matcher<Supplier<T>> gives(@NonNull final T value) {
  return new SupplierGives<>(value);
}
 
Example #22
Source File: SupplierGives.java    From agera with Apache License 2.0 4 votes vote down vote up
@Override
protected void describeMismatchSafely(final Supplier<T> supplier,
    final Description description) {
  description.appendText("was ").appendValue(supplier.get());
}
 
Example #23
Source File: SupplierGives.java    From agera with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean matchesSafely(@NonNull final Supplier<T> reference) {
  return value.equals(reference.get());
}
 
Example #24
Source File: AgeraCallAdapterFactory.java    From retrofit-agera-call-adapter with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<Result<R>> adapt(Call<R> call) {
    return new CallSupplier<R>(call);
}
 
Example #25
Source File: AgeraCallAdapterFactory.java    From retrofit-agera-call-adapter with Apache License 2.0 4 votes vote down vote up
@Override public Supplier<Result<Response<R>>> adapt(Call<R> call) {
    return new CallResponseSupplier<R>(call);
}
 
Example #26
Source File: GirlsSupplier.java    From AndroidAgeraTutorial with Apache License 2.0 4 votes vote down vote up
public GirlsSupplier(@NonNull Supplier<Integer> supplier ) {
    mSupplierPagination = supplier;
}
 
Example #27
Source File: MainActivity.java    From retrofit-agera-call-adapter with Apache License 2.0 votes vote down vote up
@GET("{page}") Supplier<Result<Response<Gank>>> android(@Path("page") int page); 
Example #28
Source File: MainActivity.java    From retrofit-agera-call-adapter with Apache License 2.0 votes vote down vote up
@GET("1") Supplier<Result<Gank>> android(); 
Example #29
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 votes vote down vote up
@DELETE("/xxx") Supplier<Result<Response<Void>>> responseOfDeleteXXX(); 
Example #30
Source File: SupplierTest.java    From retrofit-agera-call-adapter with Apache License 2.0 votes vote down vote up
@DELETE("/xxx") Supplier<Result<Void>> deleteXXX();