optional

Mockito.when().thenReturn() doesn't work or returns null

断了今生、忘了曾经 提交于 2020-01-03 16:56:08
问题 During the test there is a NullPointerException thrown. I tried to debug it and the only thing I worked out was that eventOptional is always null. Just as if Mockito.when().thenReturn() didn't work. Can anybody help? Here's my code for a tested service and for the test itself: @Service public class EventService { @Autowired public EventService(EventRepository eventRepository) { this.eventRepository = eventRepository; } //... public void updateEvent(EventDTO eventDTO) { Optional<Event>

Optional characters in a regex

你。 提交于 2020-01-03 09:05:13
问题 The task is pretty simple, but I've not been able to come up with a good solution yet: a string can contain numbers, dashes and pluses, or only numbers. ^[0-9+-]+$ does most of what I need, except when a user enters garbage like "+-+--+" I've not had luck with regular lookahead, since the dashes and pluses could potentially be anywhere in the string. Valid strings: 234654 24-3+-2 -234 25485+ Invalid: ++--+ 回答1: How about: ^[0-9+-]*[0-9][0-9+-]*$ This ensures that there is at least one digit

Is there a non-messy way to chain the results of functions that return Option values?

99封情书 提交于 2020-01-03 08:14:51
问题 I have some code that looks like this: f(a).and_then(|b| { g(b).and_then(|c| { h(c).map(|d| { do_something_with(a, b, c, d) }) }) }) Where f , g , and h return Option values. I need to use all the intermediate values ( a , b , c , and d ) in the do_something_with calculation. The indentation is very deep. Is there a better way to do this? Ideally it would look something like this (which of course doesn't work): try { let b = f(a); let c = g(b); let d = h(c); do_something_with(a, b, c, d) }

Is there a pretty way to increment an optional Int?

非 Y 不嫁゛ 提交于 2020-01-03 07:18:11
问题 I want to increment an Int? Currently I have written this : return index != nil ? index!+1 : nil Is there some prettier way to write this ? 回答1: For the sake of completeness, Optional has a map() method: /// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`. @warn_unused_result @rethrows public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U? Therefore index != nil ? index! + 1 : nil is equivalent to index.map { $0 + 1 } 回答2: You can call the advanced(by:) function

Is there a pretty way to increment an optional Int?

时光毁灭记忆、已成空白 提交于 2020-01-03 07:18:08
问题 I want to increment an Int? Currently I have written this : return index != nil ? index!+1 : nil Is there some prettier way to write this ? 回答1: For the sake of completeness, Optional has a map() method: /// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`. @warn_unused_result @rethrows public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U? Therefore index != nil ? index! + 1 : nil is equivalent to index.map { $0 + 1 } 回答2: You can call the advanced(by:) function

Is there a way to combine Java8 Optional returning a value with printing a message on null?

不问归期 提交于 2020-01-03 06:42:09
问题 I'm starting with this code: String startingValue = getMyValue(); String finishingValue = ""; if (startingValue != null) { finishingValue = startingValue; } else { System.out.println("Value was null"); } I want to transform it using Java 8 options to something like this: Optional<String> startingOptional = getMyOptional(); String finishingValue = startingOptional .map(value -> value) .orElse(System.out.println("value not found")); My question is: Is there a way to combine Java8 Optional

How to deal with non-optional values in NSUserDefaults in Swift

喜欢而已 提交于 2020-01-03 03:08:10
问题 To get a value from NSUserDefaults I would do something like this: let userDefaults = NSUserDefaults.standardUserDefaults() if let value = userDefaults.objectForKey(key) { print(value) } However, these methods do not return optionals: boolForKey (defaults to false if not set) integerForKey (defaults to 0 if not set) floatForKey (defaults to 0.0 if not set) doubleForKey (defaults to 0.0 if not set) In my app I want to used the saved value of an integer if it has been previously set. And if it

How to deal with non-optional values in NSUserDefaults in Swift

时间秒杀一切 提交于 2020-01-03 03:08:05
问题 To get a value from NSUserDefaults I would do something like this: let userDefaults = NSUserDefaults.standardUserDefaults() if let value = userDefaults.objectForKey(key) { print(value) } However, these methods do not return optionals: boolForKey (defaults to false if not set) integerForKey (defaults to 0 if not set) floatForKey (defaults to 0.0 if not set) doubleForKey (defaults to 0.0 if not set) In my app I want to used the saved value of an integer if it has been previously set. And if it

Equivalence of short-cut optional binding syntax in Swift

与世无争的帅哥 提交于 2020-01-03 00:38:48
问题 Given var maybeInt: Int? how to the following two conditional statements differ? // (1) if let y = maybeInt { y } else { println("Nope") } // (2) if let y = maybeInt? { y } else { println("Nope") } They seem to behave exactly the same. Is the former a short-cut for the latter? 回答1: The second syntax is optional chaining — just with nothing after the chaining operator. It accesses whatever optional comes before it (if the optional is not nil ), allows chaining property accesses or method calls

How to disengage std::experimental::optional?

╄→гoц情女王★ 提交于 2020-01-02 04:55:13
问题 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... 回答1: work = std::experimental::nullopt; should