com.annimon.stream.function.Consumer Java Examples

The following examples show how to use com.annimon.stream.function.Consumer. 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: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValueAbsent() {
    final Integer[] data = { 0 };
    Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            fail();
        }
    }, new Runnable() {
        @Override
        public void run() {
            data[0] = 1;
        }
    });
    assertThat(data[0], is(1));
}
 
Example #2
Source File: PermissionsRequest.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
PermissionsRequest(@Nullable Runnable allGrantedListener,
                   @Nullable Runnable anyDeniedListener,
                   @Nullable Runnable anyPermanentlyDeniedListener,
                   @Nullable Runnable anyResultListener,
                   @Nullable Consumer<List<String>> someGrantedListener,
                   @Nullable Consumer<List<String>> someDeniedListener,
                   @Nullable Consumer<List<String>> somePermanentlyDeniedListener)
{
  this.allGrantedListener            = allGrantedListener;

  this.anyDeniedListener             = anyDeniedListener;
  this.anyPermanentlyDeniedListener  = anyPermanentlyDeniedListener;
  this.anyResultListener             = anyResultListener;

  this.someGrantedListener           = someGrantedListener;
  this.someDeniedListener            = someDeniedListener;
  this.somePermanentlyDeniedListener = somePermanentlyDeniedListener;
}
 
Example #3
Source File: ExceptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfException() {
    for (final ExceptionType type : ExceptionType.values()) {
        Exceptional
                .of(new ThrowableSupplier<Integer, Throwable>() {
                    @Override
                    public Integer get() throws Throwable {
                        throwException(type);
                        return 10;
                    }
                })
                .ifException(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable value) {
                        assertEquals(type.getException(), value);
                    }
                });
    }
}
 
Example #4
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValueAbsent() {
    final Integer[] data = { 0 };
    Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            fail();
        }
    }, new Runnable() {
        @Override
        public void run() {
            data[0] = 1;
        }
    });
    assertThat(data[0], is(1));
}
 
Example #5
Source File: PeekTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeek() {
    final List<Integer> result = new ArrayList<Integer>();
    long count = Stream.range(0, 5)
            .peek(new Consumer<Integer>() {
                @Override
                public void accept(Integer t) {
                    result.add(t);
                }
            })
            .count();
    assertEquals(5, count);
    assertThat(result, contains(
            0, 1, 2, 3, 4
    ));
}
 
Example #6
Source File: LibraryViewModel.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
/**
 * Delete the given list of content
 *
 * @param contents   List of content to be deleted
 * @param onError    Callback to run when an error occurs
 */
public void deleteItems(@NonNull final List<Content> contents, Consumer<Throwable> onError) {
    // Flag the content as "being deleted" (triggers blink animation)
    for (Content c : contents) flagContentDelete(c, true);

    compositeDisposable.add(
            Observable.fromIterable(contents)
                    .subscribeOn(Schedulers.io())
                    .flatMap(s -> Observable.fromCallable(() -> doDeleteContent(s)))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            v -> {
                            },
                            onError::accept
                    )
    );
}
 
Example #7
Source File: ExceptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfException() {
    for (final ExceptionType type : ExceptionType.values()) {
        Exceptional
                .of(new ThrowableSupplier<Integer, Throwable>() {
                    @Override
                    public Integer get() throws Throwable {
                        throwException(type);
                        return 10;
                    }
                })
                .ifException(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable value) {
                        assertEquals(type.getException(), value);
                    }
                });
    }
}
 
Example #8
Source File: PermissionsRequest.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
PermissionsRequest(@Nullable Runnable allGrantedListener,
                   @Nullable Runnable anyDeniedListener,
                   @Nullable Runnable anyPermanentlyDeniedListener,
                   @Nullable Runnable anyResultListener,
                   @Nullable Consumer<List<String>> someGrantedListener,
                   @Nullable Consumer<List<String>> someDeniedListener,
                   @Nullable Consumer<List<String>> somePermanentlyDeniedListener)
{
  this.allGrantedListener            = allGrantedListener;

  this.anyDeniedListener             = anyDeniedListener;
  this.anyPermanentlyDeniedListener  = anyPermanentlyDeniedListener;
  this.anyResultListener             = anyResultListener;

  this.someGrantedListener           = someGrantedListener;
  this.someDeniedListener            = someDeniedListener;
  this.somePermanentlyDeniedListener = somePermanentlyDeniedListener;
}
 
Example #9
Source File: PermissionsRequest.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
PermissionsRequest(@Nullable Runnable allGrantedListener,
                   @Nullable Runnable anyDeniedListener,
                   @Nullable Runnable anyPermanentlyDeniedListener,
                   @Nullable Runnable anyResultListener,
                   @Nullable Consumer<List<String>> someGrantedListener,
                   @Nullable Consumer<List<String>> someDeniedListener,
                   @Nullable Consumer<List<String>> somePermanentlyDeniedListener)
{
  this.allGrantedListener            = allGrantedListener;

  this.anyDeniedListener             = anyDeniedListener;
  this.anyPermanentlyDeniedListener  = anyPermanentlyDeniedListener;
  this.anyResultListener             = anyResultListener;

  this.someGrantedListener           = someGrantedListener;
  this.someDeniedListener            = someDeniedListener;
  this.somePermanentlyDeniedListener = somePermanentlyDeniedListener;
}
 
Example #10
Source File: PeekTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeek() {
    final List<Integer> result = new ArrayList<Integer>();
    long count = Stream.range(0, 5)
            .peek(new Consumer<Integer>() {
                @Override
                public void accept(Integer t) {
                    result.add(t);
                }
            })
            .count();
    assertEquals(5, count);
    assertThat(result, contains(
            0, 1, 2, 3, 4
    ));
}
 
Example #11
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteIfPresentOnAbsentValue() {
    Optional.<Integer>empty()
            .executeIfPresent(new Consumer<Integer>() {
                @Override
                public void accept(Integer value) {
                    fail();
                }
            });
}
 
Example #12
Source File: ExceptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresentOnAbsentValue() {
    Exceptional
            .of(ioExceptionSupplier)
            .ifPresent(new Consumer<Integer>() {
                @Override
                public void accept(Integer value) {
                    fail();
                }
            });
}
 
Example #13
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testIfPresentOrElseWhenValueAbsentAndEmptyActionNull() {
    Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            fail("Should not have been executed.");
        }
    }, null);
}
 
Example #14
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValuePresentAndEmptyActionNull() {
    Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            assertEquals(10, (int) value);
        }
    }, null);
}
 
Example #15
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValuePresent() {
    Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            assertEquals(10, (int) value);
        }
    }, new Runnable() {
        @Override
        public void run() {
            fail("Should not execute empty action when value is present.");
        }
    });
}
 
Example #16
Source File: ExceptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresent() {
    final Integer[] data = { 0 };
    Exceptional
            .of(tenSupplier)
            .ifPresent(new Consumer<Integer>() {
                @Override
                public void accept(Integer value) {
                    data[0] = value;
                }
            });
    assertThat(data[0], is(10));
}
 
Example #17
Source File: Exceptional.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes consumer function if exception class matches {@code throwableClass}.
 *
 * @param <E> the type of exception
 * @param throwableClass  the class of an exception to be compared
 * @param consumer  a consumer function
 * @return an {@code Exceptional}
 */
@NotNull
@SuppressWarnings("unchecked")
public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) {
    if ( (throwable != null) &&
            (throwableClass.isAssignableFrom(throwable.getClass())) ) {
        consumer.accept((E) throwable);
    }
    return this;
}
 
Example #18
Source File: CustomTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomTerminalOperator_ForEach() {
    final List<Integer> list = new ArrayList<Integer>();
    Stream.range(0, 10)
            .custom(new CustomOperators.ForEach<Integer>(new Consumer<Integer>() {
                @Override
                public void accept(Integer t) {
                    list.add(t);
                }
            }));

    assertThat(list, contains(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
}
 
Example #19
Source File: Exceptional.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes consumer function if there were any exception.
 *
 * @param consumer  a consumer function
 * @return an {@code Exceptional}
 */
@NotNull
public Exceptional<T> ifException(@NotNull Consumer<Throwable> consumer) {
    if (throwable != null) {
        consumer.accept(throwable);
    }
    return this;
}
 
Example #20
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValuePresent() {
    Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            assertEquals(10, (int) value);
        }
    }, new Runnable() {
        @Override
        public void run() {
            fail("Should not execute empty action when value is present.");
        }
    });
}
 
Example #21
Source File: MainActivity.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Saves current tab index and fragment states.
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    FragmentManager fm = getSupportFragmentManager();
    Consumer<Fragment> putFragment = fragment -> {
        if (fragment != null && fragment.isAdded()) {
            fm.putFragment(outState, fragment.getClass().getName(), fragment);
        }
    };
    putFragment.accept(mFolderListFragment);
    putFragment.accept(mDeviceListFragment);
    putFragment.accept(mDrawerFragment);

    outState.putInt("currentTab", mViewPager.getCurrentItem());
    outState.putBoolean(BATTERY_DIALOG_DISMISSED, mBatteryOptimizationsDialog == null || !mBatteryOptimizationsDialog.isShowing());
    outState.putBoolean(IS_SHOWING_RESTART_DIALOG, mRestartDialog != null && mRestartDialog.isShowing());
    if(mQrCodeDialog != null && mQrCodeDialog.isShowing()) {
        outState.putBoolean(IS_QRCODE_DIALOG_DISPLAYED, true);
        ImageView qrCode = mQrCodeDialog.findViewById(R.id.qrcode_image_view);
        TextView deviceID = mQrCodeDialog.findViewById(R.id.device_id);
        outState.putParcelable(QRCODE_BITMAP_KEY, ((BitmapDrawable) qrCode.getDrawable()).getBitmap());
        outState.putString(DEVICEID_KEY, deviceID.getText().toString());
    }
    Util.dismissDialogSafe(mRestartDialog, this);
}
 
Example #22
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresent() {
    Optional.of(10).ifPresent(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            assertEquals(10, (int) value);
        }
    });
}
 
Example #23
Source File: Exceptional.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes consumer function with value if present.
 *
 * @param consumer  a consumer function
 * @return this {@code Exceptional}
 * @since 1.1.2
 */
@NotNull
public Exceptional<T> ifPresent(@NotNull Consumer<? super T> consumer) {
    if (throwable == null) {
        consumer.accept(value);
    }
    return this;
}
 
Example #24
Source File: ImageViewerViewModel.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private void onToggleFavouriteSuccess(ImageFile result, Consumer<ImageFile> callback) {
    List<ImageFile> imgs = getImages().getValue();
    if (imgs != null) {
        for (ImageFile img : imgs)
            if (img.getId() == result.getId()) {
                img.setFavourite(result.isFavourite()); // Update new state in memory
                result.setDisplayOrder(img.getDisplayOrder()); // Set the display order of the item to
                callback.accept(result); // Inform the view
            }
    }
    compositeDisposable.clear();
}
 
Example #25
Source File: ImageViewerViewModel.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public void togglePageFavourite(ImageFile file, Consumer<ImageFile> callback) {
    compositeDisposable.add(
            Single.fromCallable(() -> doTogglePageFavourite(getApplication().getApplicationContext(), file.getId()))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            result -> onToggleFavouriteSuccess(result, callback),
                            Timber::e
                    )
    );
}
 
Example #26
Source File: ImageViewerViewModel.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public void toggleShowFavouritePages(Consumer<Boolean> callback) {
    Content c = content.getValue();
    if (c != null) {
        showFavourites = !showFavourites;
        processContent(c);
        callback.accept(showFavourites);
    }
}
 
Example #27
Source File: LibraryViewModel.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
/**
 * Archive the given Content into a temp ZIP file
 *
 * @param content   Content to be archived
 * @param onSuccess Callback to run when the operation succeeds
 */
public void archiveContent(@NonNull final Content content, Consumer<File> onSuccess) {
    Timber.d("Building file list for: %s", content.getTitle());

    DocumentFile bookFolder = DocumentFile.fromTreeUri(getApplication(), Uri.parse(content.getStorageUri()));
    if (null == bookFolder || !bookFolder.exists()) return;

    List<DocumentFile> files = FileHelper.listDocumentFiles(getApplication(), bookFolder, null); // Everything (incl. JSON and thumb) gets into the archive
    if (!files.isEmpty()) {
        // Create folder to share from
        File sharedDir = new File(getApplication().getExternalCacheDir() + "/shared");
        if (FileHelper.createDirectory(sharedDir)) {
            Timber.d("Shared folder created.");
        }

        // Clean directory (in case of previous job)
        if (FileHelper.cleanDirectory(sharedDir)) {
            Timber.d("Shared folder cleaned up.");
        }

        // Build destination file
        File dest = new File(getApplication().getExternalCacheDir() + "/shared",
                content.getTitle().replaceAll(AUTHORIZED_CHARS, "_") + ".zip");
        Timber.d("Destination file: %s", dest);

        compositeDisposable.add(
                Single.fromCallable(() -> ZipUtil.zipFiles(getApplication(), files, dest))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(onSuccess::accept,
                                Timber::e)
        );
    }
}
 
Example #28
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteIfPresentOnAbsentValue() {
    Optional.<Integer>empty()
            .executeIfPresent(new Consumer<Integer>() {
                @Override
                public void accept(Integer value) {
                    fail();
                }
            });
}
 
Example #29
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testIfPresentOrElseWhenValueAbsentAndEmptyActionNull() {
    Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            fail("Should not have been executed.");
        }
    }, null);
}
 
Example #30
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfPresentOrElseWhenValuePresentAndEmptyActionNull() {
    Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
            assertEquals(10, (int) value);
        }
    }, null);
}