optional

is it possible to use twice ifPresent each one checking different return or bifurcate map

北慕城南 提交于 2019-12-06 05:27:07
Context: I have chained objects generated by wsdl2java. There is an already created method that call the soap web services, receives the xml and unmarshalling it in cascaded objects. Desire: I want to get two different objects which lies in different hierarchy nodes. From mapping perspective they are hold "far" from one another but from business perspective they are a "couple". This is currently working: Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode) //same return .map(CodeOndernemingOfVestigingType::getValue) // different object return

避免!=空语句

微笑、不失礼 提交于 2019-12-06 02:45:19
我经常使用 object != null 来避免 NullPointerException 。 有没有好的替代方法? 例如: if (someobject != null) { someobject.doCalc(); } 如果不知道对象是否为 null ,则可以避免 NullPointerException 。 请注意,接受的答案可能已过期,请参阅 https://stackoverflow.com/a/2386013/12943 以获取最新的方法。 #1楼 Java 7有一个新的 java.util.Objects 实用程序类,在该类上有一个 requireNonNull() 方法。 如果它的参数为null,则所有操作都将引发 NullPointerException ,但会稍微清理一下代码。 例: Objects.requireNonNull(someObject); someObject.doCalc(); 该方法对于 检查 构造函数中的赋值之前最有用,其中每次使用它可以节省三行代码: Parent(Child child) { if (child == null) { throw new NullPointerException("child"); } this.child = child; } 变成 Parent(Child child) { this.child =

upstream指令及负载均衡方式

帅比萌擦擦* 提交于 2019-12-05 19:47:18
UPSTREAM 语法: upstream name { ... } 默认值: — 上下文: http Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed. 定义一组服务器。 这些服务器可以监听不同的端口。 而且,监听TCP和UNIX域套接字的服务器可以混用。 Example: upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com backup; } By default, requests are distributed between the servers using a weighted round-robin balancing method. In the above example, each 7 requests will be distributed as

接口限流算法:漏桶算法和令牌桶算法

拥有回忆 提交于 2019-12-05 18:23:37
漏桶算法 漏桶可以看作是一个带有常量服务时间的单服务器队列,如果漏桶(包缓存)溢出,那么数据包会被丢弃。这一点和线程池原理是很相似的。 把请求比作是水,水来了都先放进桶里,并以限定的速度出水,当水来得过猛而出水不够快时就会导致水直接溢出,即拒绝服务。 需要注意的是,在某些情况下,漏桶算法不能够有效地使用网络资源,因为漏桶的漏出速率是固定的,所以即使网络中没有发生拥塞,漏桶算法也不能使某一个单独的数据流达到端口速率。因此,漏桶算法对于存在突发特性的流量来说缺乏效率。而令牌桶算法则能够满足这些具有突发特性的流量。 令牌桶算法 令牌桶算法是网络流量整形(Traffic Shaping)和速率限制(Rate Limiting)中最常使用的一种算法。典型情况下,令牌桶算法用来控制发送到网络上的数据的数目,并允许突发数据的发送。 令牌桶算法的原理是系统会以一个恒定的速度往桶里放入令牌,而如果请求需要被处理,则需要先从桶里获取一个令牌,当桶里没有令牌可取时,则拒绝服务。从原理上看,令牌桶算法和漏桶算法是相反的,一个“进水”,一个是“漏水”。 单机限流 Google的Guava包中的RateLimiter类就是令牌桶算法的解决方案。 首先说下单机限流 package yzy.guava.test; import com.google.common.base.Optional; import com

How do I concisely write a || b where a and b are Optional values?

对着背影说爱祢 提交于 2019-12-05 17:09:31
问题 I'm happy with an answer in any language, but I ultimately want an answer in Java. (Java 8+ is fine. Not limited to Java 8. I've tried to fix the tags.) If I have two Optional<Integer> values, how do I concisely compute the equivalent of a || b , meaning: a , if it's defined; otherwise b , if it's defined; otherwise empty() ? Optional<Integer> a = ...; Optional<Integer> b = ...; Optional<Integer> aOrB = a || b; // How to write this in Java 8+? I know that I can write a.orElse(12) , but what

Throw RuntimeException inside Stream with Optional.orElseThrow

喜夏-厌秋 提交于 2019-12-05 12:57:24
问题 The following code works fine: Stream.of("key1", "key2") .map(key -> { SomeObject foo = service.find(key); if (foo == null) { throw new RuntimeException("No entity found with key: " + key); } return foo; }) // ... However, when I use orElseThrow from Optional: Stream.of("key1", "key2") .map(key -> Optional.ofNullable(someService.find(key)) .orElseThrow(() -> new RuntimeException("No entity found with key: " + key))) // ... I get a compile time error: Error:(129, 33) java: unreported exception

Early return/golden path in Swift

こ雲淡風輕ζ 提交于 2019-12-05 12:47:17
I'm used to write code with early return/golden path in Objective-C. I tried this approach in Swift, and noticed that early return comes at the expense of using the forced unwrapping operator ( ! ) when optionals are involved. Take a method that calculates the size of a directory. First, the golden path version: private func calculateSize_GoldenPath(directory:String) -> UInt64 { let fileManager = NSFileManager.defaultManager() var error : NSError? var contents = fileManager.contentsOfDirectoryAtPath(directory, error: &error) as [String]? if contents == nil { NSLog("Failed to list directory

Stream.findFirst different than Optional.of?

旧城冷巷雨未停 提交于 2019-12-05 12:28:57
问题 Lets say I have two classes and two methods: class Scratch { private class A{} private class B extends A{} public Optional<A> getItems(List<String> items){ return items.stream() .map(s -> new B()) .findFirst(); } public Optional<A> getItems2(List<String> items){ return Optional.of( items.stream() .map(s -> new B()) .findFirst() .get() ); } } Why does getItems2 compile while getItems gives compiler error incompatible types: java.util.Optional<Scratch.B> cannot be converted to java.util

How to disengage std::experimental::optional?

女生的网名这么多〃 提交于 2019-12-05 10:44:00
With Boost I can create an optional in-place with: boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service)); And disengage it with: work = boost::none; With C++14 / experimental support, I can instead construct an optional in-place with: std::experimental::optional<boost::asio::io_service::work> work; work.emplace(boost::asio::io_service::work(io_service)); But I'm at a loss for how to disengage it... work = std::experimental::nullopt; should disengage work . The library fundamental TS specifies in [optional.nullopt]: The struct nullopt_t is an empty

Optional Int in Realm

六眼飞鱼酱① 提交于 2019-12-05 09:47:26
问题 I am trying to use an Optional Int in Realm and am getting an old error I think. Code dynamic var reps: Int? = nil Error 'Property cannot be marked dynamic because its type cannot be represented in Objective-C' I am using Realm 0.96.1 with XCode 7.1 I understand in the Realm documentation it says the Int isn't supported as an Optional but https://twitter.com/realm/status/656621989583548416. That is from the Realm twitter so thats why I am confused. Are Optional Int supported or still no? 回答1: