本文整理汇总了Java中java.util.Optional.ofNullable方法的典型用法代码示例。如果您正苦于以下问题:Java Optional.ofNullable方法的具体用法?Java Optional.ofNullable怎么用?Java Optional.ofNullable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Optional
的用法示例。
在下文中一共展示了Optional.ofNullable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listen
import java.util.Optional; //导入方法依赖的package包/类
@KafkaListener(topics = {"test-topic"})
public void listen(ConsumerRecord<?, ?> record) {
logger.debug("listen1 ");
Optional<?> kafkaMessage = Optional.ofNullable(record.value());
if (kafkaMessage.isPresent()) {
Object message = kafkaMessage.get();
logger.debug("listen1 : {}", message);
}
}
示例2: get
import java.util.Optional; //导入方法依赖的package包/类
public Optional<User> get(final String userId)
{
String url = remoteConfig.getDoubanUserGetUrl() + userId;
ResponseEntity<User> response = restTemplate.getForEntity(url, User.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
return Optional.ofNullable(response.getBody());
}
return Optional.empty();
}
示例3: findIssuerById
import java.util.Optional; //导入方法依赖的package包/类
@Override
public Optional<IssuerEntity> findIssuerById(long id) {
try {
return Optional.ofNullable(issuerRepository.findOne(id));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}
示例4: parseUserFromToken
import java.util.Optional; //导入方法依赖的package包/类
Optional<UserDetails> parseUserFromToken(String token) {
String username = Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
.getBody()
.getSubject();
return Optional.ofNullable(userService.loadUserByUsername(username));
}
示例5: getHaving
import java.util.Optional; //导入方法依赖的package包/类
@Override
public Optional<Predicate> getHaving() {
return Optional.ofNullable(having);
}
示例6: findConfiguration
import java.util.Optional; //导入方法依赖的package包/类
public Optional<Configuration> findConfiguration(String path) {
return Optional.ofNullable(hazelcastRepository.find(path));
}
示例7: onTryPickupItem
import java.util.Optional; //导入方法依赖的package包/类
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onTryPickupItem(EntityItemPickupEvent event)
{
if (!SettingsFlags.instance.enableCustomInventory)
{
return;
}
if (event.getItem() != null && !event.getItem().getItem().isEmpty() && !event.getItem().isDead && !event.getEntityPlayer().world.isRemote)
{
int i = PlayerInventoryHelper.findFirstAvailableSlotFor(event.getItem().getItem(), Optional.empty(), event.getEntityPlayer());
if (i == -1)
{
event.setCanceled(true);
}
else
{
if (i < Short.MAX_VALUE)
{
event.getEntityPlayer().inventory.setInventorySlotContents(i, event.getItem().getItem().copy());
net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerItemPickupEvent(event.getEntityPlayer(), event.getItem());
event.getEntityPlayer().onItemPickup(event.getItem(), event.getItem().getItem().getCount());
event.getEntityPlayer().addStat(StatList.getObjectsPickedUpStats(event.getItem().getItem().getItem()), event.getItem().getItem().getCount());
event.getItem().setDead();
event.setCanceled(true);
}
else
{
int slotID = i - Short.MAX_VALUE;
ItemStack toIncrement = event.getEntityPlayer().inventory.getStackInSlot(slotID);
Optional<Slot> s = Optional.ofNullable(event.getEntityPlayer().openContainer != null ? event.getEntityPlayer().openContainer.getSlot(slotID) : null);
int max = Math.min(toIncrement.getMaxStackSize(), s.map(slot -> slot.getItemStackLimit(toIncrement)).orElseGet(() -> event.getEntityPlayer().inventory.getInventoryStackLimit()));
int current = toIncrement.getCount();
final int added = Math.min(event.getItem().getItem().getCount(), max - current);
net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerItemPickupEvent(event.getEntityPlayer(), event.getItem());
event.getEntityPlayer().addStat(StatList.getObjectsPickedUpStats(event.getItem().getItem().getItem()), added);
event.getItem().getItem().shrink(added);
toIncrement.grow(added);
event.getEntityPlayer().world.playSound(null, event.getEntityPlayer().getPosition(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, (event.getEntityPlayer().world.rand.nextFloat() - event.getEntityPlayer().world.rand.nextFloat()) * 1.4F + 2.0F);
if (toIncrement.isEmpty())
{
event.getItem().setDead();
}
event.setCanceled(true);
}
}
}
}
示例8: getSequence
import java.util.Optional; //导入方法依赖的package包/类
@Override
public Optional<Integer> getSequence() {
return Optional.ofNullable(sequence);
}
示例9: getFootprintRadius
import java.util.Optional; //导入方法依赖的package包/类
public Optional<Float> getFootprintRadius() {
return Optional.ofNullable(footprintRadius);
}
示例10: getAccuracy
import java.util.Optional; //导入方法依赖的package包/类
@Nonnull
@Override
public Optional<Integer> getAccuracy() {
return Optional.ofNullable(accuracy);
}
示例11: getMandate
import java.util.Optional; //导入方法依赖的package包/类
public Optional<byte[]> getMandate() {
return Optional.ofNullable(mandate);
}
示例12: put
import java.util.Optional; //导入方法依赖的package包/类
@Override
public <T> Optional<T> put(String resourceKey, T value) throws UnsupportedOperationException {
return Optional.ofNullable(resources.put(resourceKey, value));
}
示例13: getCurrent
import java.util.Optional; //导入方法依赖的package包/类
/**
* Get the current {@link ViewNavigator}, if available as {@link Context} resource or from current UI.
* @return Current ViewNavigator, or an empty Optinal if not available
*/
static Optional<ViewNavigator> getCurrent() {
return Optional.ofNullable(Context.get().resource(ViewNavigator.CONTEXT_KEY, ViewNavigator.class)
.orElse(ViewNavigationUtils.getCurrentUIViewNavigator()));
}
示例14: search
import java.util.Optional; //导入方法依赖的package包/类
public Optional<Point> search(String query) {
return Optional.ofNullable(geoDataCache.computeIfAbsent(query, key -> searchInt(query).orElse(null)));
}
示例15: tryTribute
import java.util.Optional; //导入方法依赖的package包/类
protected Optional<Tribute> tryTribute(MatchPlayer player) {
return Optional.ofNullable(tributes.get(player.getPlayerId()));
}