本文整理汇总了Java中com.annimon.stream.Optional类的典型用法代码示例。如果您正苦于以下问题:Java Optional类的具体用法?Java Optional怎么用?Java Optional使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Optional类属于com.annimon.stream包,在下文中一共展示了Optional类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setOffsetForItem
import com.annimon.stream.Optional; //导入依赖的package包/类
/**
* Sets the offset (spacing) for the {@code view}.
*
* @param outRect the bounds of the view. The spacing must be set to this object
* @param view the view to add the spacing
* @param recyclerView the recycler view that holds the {@code view}
* @param gridLayoutManager the layout manager of the recycler view
*/
private void setOffsetForItem(Rect outRect, View view, RecyclerView recyclerView, GridLayoutManager gridLayoutManager) {
int position = recyclerView.getChildLayoutPosition(view);
int spanCount = gridLayoutManager.getSpanCount();
int numberOfItems = recyclerView.getAdapter().getItemCount();
GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
Optional.ofNullable(setItemOffsetConsumer)
.executeIfPresent(consumer -> consumer.accept(outRect, recyclerView))
.executeIfAbsent(() -> {
int firstRowTopSpacing = Optional.ofNullable(this.firstRowTopSpacing).orElse(itemSpacing);
int firstColumnLeftSpacing = Optional.ofNullable(this.firstColumnLeftSpacing).orElse(itemSpacing);
int lastColumnRightSpacing = Optional.ofNullable(this.lastColumnRightSpacing).orElse(itemSpacing);
int lastRowBottomSpacing = Optional.ofNullable(this.lastRowBottomSpacing).orElse(itemSpacing);
outRect.top = isFirstRow(position, spanCount, spanSizeLookup) ? firstRowTopSpacing : itemSpacing;
outRect.left = isFirstColumn(position, spanCount, spanSizeLookup) ? firstColumnLeftSpacing : itemSpacing;
outRect.right = isLastColumn(position, spanCount, numberOfItems, spanSizeLookup) ? lastColumnRightSpacing : itemSpacing;
outRect.bottom = isLastRow(position, spanCount, numberOfItems, spanSizeLookup) ? lastRowBottomSpacing : itemSpacing;
});
}
示例2: createDialog
import com.annimon.stream.Optional; //导入依赖的package包/类
/**
* Creates a {@link TimePickerDialog} instance.
*
* @param context to retrieve the time format (12 or 24 hour)
* @param localTime the initial localTime
* @param listener to listen for a localTime selection
* @param clock to retrieve the calendar from
* @return a new instance of {@link TimePickerDialog}
*/
@NonNull
public static TimePickerDialog createDialog(@NonNull Context context, @Nullable LocalTime localTime,
@Nullable Consumer<LocalTime> listener, @NonNull Clock clock) {
TimePickerDialog.OnTimeSetListener dialogCallBack = (view, hourOfDay, minute, second) -> {
LocalTime time = LocalTime.of(hourOfDay, minute, second);
Optional.ofNullable(listener)
.ifPresent(theListener -> theListener.accept(time));
};
localTime = Optional.ofNullable(localTime)
.orElse(LocalTime.now(clock));
TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(
dialogCallBack,
localTime.getHour(),
localTime.getMinute(),
localTime.getSecond(),
DateFormat.is24HourFormat(context)
);
timePickerDialog.dismissOnPause(true);
return timePickerDialog;
}
示例3: createDialog
import com.annimon.stream.Optional; //导入依赖的package包/类
/**
* Creates a {@link DatePickerDialog} instance with the <code>localDate</code> selected.
*
* @param localDate the selected start localDate
* @param listener to be triggered when the user selects a localDate
* @param clock to get the current date
* @return the {@link DatePickerDialog} created instance
*/
@NonNull
public static DatePickerDialog createDialog(@Nullable LocalDate localDate, @Nullable Consumer<LocalDate> listener,
@NonNull Clock clock) {
localDate = Optional.ofNullable(localDate)
.orElse(LocalDate.now(clock));
DatePickerDialog.OnDateSetListener dialogCallBack = (view, year, monthOfYear, dayOfMonth) -> {
LocalDate date = LocalDate.of(year, monthOfYear + 1, dayOfMonth);
Optional.ofNullable(listener)
.ifPresent(theListener -> theListener.accept(date));
};
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
dialogCallBack,
localDate.getYear(),
localDate.getMonthValue() - 1,
localDate.getDayOfMonth()
);
datePickerDialog.dismissOnPause(true);
return datePickerDialog;
}
示例4: showWord
import com.annimon.stream.Optional; //导入依赖的package包/类
@Override
public void showWord(Optional<VocabularyWord> word) {
if (!word.isPresent()) {
// In phone view, just don't open this screen
// FIXME: when we turn screen from tablet to phone, current state is word
// But when we have no words, we don't show anything
// Either also show "no data" here and remove return
// Or make sure that if we are moving from tablet to phone and and there are no words, show list
// Ugh...
return;
}
hideFragment(vocabularListFragment);
fab.hide();
if (vocabularyWordFragment == null) {
vocabularyWordFragment = new VocabularyWordFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.main_container, vocabularyWordFragment, VocabularyWordFragment.TAG)
.commitNow();
} else {
showFragment(vocabularyWordFragment);
}
vocabularyWordFragment.getPresenter().showWord(word);
setArrowIndicator(false);
}
示例5: create
import com.annimon.stream.Optional; //导入依赖的package包/类
public static VocabularyWord create(
String word,
String normalizedWord,
Optional<String> pos,
Optional<String> gender,
int strength,
List<String> translations,
String uiLanguage,
String learningLanguage) {
return new AutoValue_VocabularyWord(
word,
normalizedWord,
pos,
gender,
strength,
translations,
uiLanguage,
learningLanguage);
}
示例6: buildVocabularyData
import com.annimon.stream.Optional; //导入依赖的package包/类
private VocabularyData buildVocabularyData(String uiLanguage, String learningLanguage) {
List<VocabularyWord> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(VocabularyWord.create(
learningLanguage + "_word_" + i,
learningLanguage + "_word_" + i,
Optional.of("pos"),
Optional.of("gender"),
(int) (Math.random() * 4),
Collections.singletonList(uiLanguage + "_translation_" + i),
uiLanguage,
learningLanguage)
);
}
return VocabularyData.create(list, uiLanguage, learningLanguage);
}
示例7: processData
import com.annimon.stream.Optional; //导入依赖的package包/类
private void processData(List<VocabularyWord> list) {
logger.debug("processData() called with list of size = [{}]", list.size());
vocabularyList = list;
hasRefreshError = false;
if (list.isEmpty()) {
selectedPosition = NO_POSITION;
applyState(view -> {
view.showEmpty();
navigator.showWord(Optional.empty());
});
} else {
if (selectedPosition == NO_POSITION) {
// Select first one by default
selectedPosition = 0;
} else if (selectedPosition > list.size() - 1) {
// Selected position is outside of list bounds
selectedPosition = 0;
}
applyState(view -> view.showWords(vocabularyList, selectedPosition));
}
}
示例8: updateServerList
import com.annimon.stream.Optional; //导入依赖的package包/类
private void updateServerList() {
serverListView.findView().setVisibility(View.INVISIBLE);
progressBar.findView().setVisibility(View.VISIBLE);
final ILibraryProvider libraryProvider = new LibraryRepository(this);
libraryProvider
.getAllLibraries()
.eventually(LoopedInPromise.response(perform(libraries -> {
final int chosenLibraryId = new SelectedBrowserLibraryIdentifierProvider(this).getSelectedLibraryId();
final Optional<Library> selectedBrowserLibrary = Stream.of(libraries).filter(l -> l.getId() == chosenLibraryId).findFirst();
serverListView.findView().setAdapter(
new ServerListAdapter(
this,
Stream.of(libraries).sortBy(Library::getId).collect(Collectors.toList()),
selectedBrowserLibrary.isPresent() ? selectedBrowserLibrary.get() : null,
new BrowserLibrarySelection(this, LocalBroadcastManager.getInstance(this), libraryProvider)));
progressBar.findView().setVisibility(View.INVISIBLE);
serverListView.findView().setVisibility(View.VISIBLE);
}), this));
}
示例9: findLastFilledTextItem
import com.annimon.stream.Optional; //导入依赖的package包/类
private Optional<Button> findLastFilledTextItem() {
final List<Button> reverse = new ArrayList<>(Arrays.asList(mButtonItems));
Collections.reverse(reverse);
return Stream.of(reverse)
.filter(shown())
.filter(notEmpty())
.findFirst();
}
示例10: fetchFirebaseData
import com.annimon.stream.Optional; //导入依赖的package包/类
private void fetchFirebaseData() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference ordersRef = database.getReference("orders");
Query query = ordersRef.limitToLast(1);
final AdminPageActivity thisActivity = this;
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<GroupOrder> groupOrders = new ArrayList<>();
for (DataSnapshot groupOrderSnapshot : dataSnapshot.getChildren()) {
String uid = groupOrderSnapshot.getKey();
GroupOrder groupOrder = groupOrderSnapshot.getValue(GroupOrder.class);
groupOrder.setUid(uid);
groupOrders.add(groupOrder);
}
Optional<GroupOrder> maybeNewestOrder = Stream.of(groupOrders)
.findFirst();
thisActivity.maybeNewestOrder = maybeNewestOrder;
thisActivity.updateView();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
示例11: handleServiceErrors
import com.annimon.stream.Optional; //导入依赖的package包/类
/**
* Handles the {@link HttpException} and {@link ServiceExceptionWithMessage} exceptions, logging them if any error
* is present.
*/
public void handleServiceErrors() {
generalErrorHelper.setErrorHandlerForThrowable(ServiceExceptionWithMessage.class, serviceErrorHandler);
generalErrorHelper.setErrorHandlerForThrowable(HttpException.class, throwable ->
Optional.ofNullable((HttpException) throwable)
.ifPresent(t -> serviceErrorHandler.accept(new ServiceExceptionWithMessage(t)))
);
}
示例12: getItemOffsets
import com.annimon.stream.Optional; //导入依赖的package包/类
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (!(parent.getLayoutManager() instanceof GridLayoutManager)) {
throw new IllegalArgumentException("This Item Decoration can only be used with GridLayoutManager");
}
GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
Optional.ofNullable(setItemOffsetConsumer)
.ifPresentOrElse(
consumer -> consumer.accept(outRect, parent),
() -> setOffsetForItem(outRect, view, parent, layoutManager)
);
}
示例13: initializeEmptyView
import com.annimon.stream.Optional; //导入依赖的package包/类
/** Initializes the empty view using the resource identifier {@link #emptyViewId}, if it exists. */
private void initializeEmptyView() {
if (emptyViewId > 0) {
post(() -> Optional.ofNullable(getRootView().findViewById(emptyViewId))
.ifPresent(view -> {
emptyView = view;
showCorrectView();
}));
}
}
示例14: showCorrectView
import com.annimon.stream.Optional; //导入依赖的package包/类
/**
* Decides which view should be visible (recycler view or empty view) and shows it
*
* To do that, it checks for the presence of {@link #isInEmptyState} callback and uses it to determine whether or not
* the empty view should be shown.
* If the callback was not set, then it uses the adapter item count information, where zero elements means the empty
* state should be shown.
*/
private void showCorrectView() {
Adapter<?> adapter = getAdapter();
if (emptyView != null) {
boolean hasItems = Optional.ofNullable(isInEmptyState)
.map(state -> state.apply(this))
.orElse(adapter == null || adapter.getItemCount() > 0);
emptyView.setVisibility(hasItems ? GONE : VISIBLE);
setVisibility(hasItems ? VISIBLE : GONE);
}
}
示例15: log
import com.annimon.stream.Optional; //导入依赖的package包/类
@Override
public void log(@NonNull Map<String, String> logInformation, @Nullable Throwable t) {
if (logInformation.isEmpty()) {
return;
}
String exceptionMessage = Stream.of(logInformation)
.reduce("", (accumulator, entry) -> {
Crashlytics.setString(entry.getKey(), entry.getValue());
return accumulator + entry.getKey() + " = " + entry.getValue() + "\n";
});
Optional.ofNullable(t)
.executeIfAbsent(() -> Crashlytics.logException(new Exception(exceptionMessage)))
.ifPresent(throwable -> Crashlytics.logException(t));
}