optional

Avoid isPresent() and get() in control logic

落花浮王杯 提交于 2019-12-05 09:41:32
Is there a prettier way of doing the following in Java 8, avoiding isPresent and get ? void doStuff(String someValue, Optional<Boolean> doIt) { if (doIt.isPresent()) { if (doIt.get()) { trueMethod(someValue); } else { falseMethod(someValue); } } } I tried using map , without success. But I probably didn't try hard enough? You can use ifPresent instead of isPresent and get : void doStuff(String someValue, Optional<Boolean> doIt) { doIt.ifPresent (b -> { if (b) trueMethod(someValue); else falseMethod(someValue); }); } EDIT: fixed my code, since you can't use the ternary operator if trueMethod

Java 8 optional: ifPresent return object orElseThrow exception

╄→гoц情女王★ 提交于 2019-12-05 08:47:58
问题 I'm trying to make something like this: private String getStringIfObjectIsPresent(Optional<Object> object){ object.ifPresent(() ->{ String result = "result"; //some logic with result and return it return result; }).orElseThrow(MyCustomException::new); } This won't work, because ifPresent takes Consumer functional interface as parameter, which has void accept(T t). It cannot return any value. Is there any other way to do it ? 回答1: Actually what you are searching is: Optional.map. Your code

AndroidL 开机展示Keyguard锁屏机制初探

China☆狼群 提交于 2019-12-05 08:24:02
目录 目录 锁屏时序图 开机启动到PhoneWindowManager的systemReady方法 锁屏加载流程 PhoneWindowManager KeyguardServiceDelegate KeyguardServiceWrapper KeyguardService KeyguardViewMediator onSystemReady onKeyguardLocked showLocked handleShow 重点一 重点二 StatusBarKeyguardViewManager show reset showBouncerOrKeyguard 锁屏时序图 研究了将近两天的Android5.1 Keyguard锁屏机制,不得不说,各种饶。这里先把锁屏流程时序图贡献给大家: 使用的是线编辑工具ProcessOn,用来编辑时序图效果看起来不是太好。不过没有太大关系,这个时序图只是为了方便我们能清晰的对锁屏流程有个大致的了解,接下来,我会详细的分析每个类的具体流程。 声明:本文基于AndroidLollipop 5.1.1_r6版本进行的源码分析。 开机启动到PhoneWindowManager的systemReady方法 准备先从开机启动到PhoneWindowManager类的systemReady方法调用开始介绍。开机启动流程其实也很复杂

'(NSObject, AnyObject)' is not convertible to 'String'

空扰寡人 提交于 2019-12-05 08:17:13
How do I convert an object of type (NSObject, AnyObject) to the type String ? At the end of the first line of the method below, as String causes the compiler error: '(NSObject, AnyObject)' is not convertible to 'String' Casting street to NSString instead of String compiles, but I'm casting street to String because I want to compare it to placemark.name , which has the type String! , not NSString . I know name and street are optionals, but I'm assuming they're not nil for now because all the places returned from MKLocalSearch seem to have non-nil names and streets. func formatPlacemark

DDD(十)--仓储

末鹿安然 提交于 2019-12-05 07:30:56
1、引言 DDD中的Repository(仓储):协调领域和数据映射层,利用类似与集合的接口来访问领域对象。——《领域驱动设计-软件核心复杂性应对之道》 仓储是DDD中产生的概念,也就是说,如果应用程序不是基于领域驱动设计的,那在设计中使用仓储是不是会有不合适的地方呢? Eric Evans 在《领域驱动设计-软件核心复杂性应对之道》定义中明确的那样:协调领域和数据映射层,两个关键字领域和数据映射层,这里面的领域是指领域模型(实体和值对象),这是桥的一头,另一头就是数据映射层,也就是我们常说的 ORM 工具。 除了这两个关键词,还有一个动词就是协调,仓储协调的是什么?怎么协调的?这个概念需要明确下,桥的一头-领域模型(主要是实体对象),这个就不多说了,桥的另一头-ORM(对象关系映射),其实仓储协调的是 ORM 中的“O”,也就是对象的概念,它是在数据映射层之上的,是一种概念,而不是一种实现。 2、DDD中的仓储 仓储代表一个聚合的集合,仓储用来存储和删除聚合,但同时提供针对聚合的显式查询以及汇总。 2.1、仓储与数据访问层的区别 仓储限定了只能通过聚合根来持久化和检索领域对象,以确保所有改动和不变性由聚合处理。 仓储通过隐藏聚合持久化和检索的底层技术实现领域层的的持久化无关性(即领域层不需要知道如何持久化领域对象)。 仓储在数据模型和领域模型定义了一个边界。 2.2、仓储举例

experimental::optional nullopt_t constructor

无人久伴 提交于 2019-12-05 07:03:31
Here is described the nullopt_t and nullopt for the optional object proposed for c++: struct nullopt_t{see below}; constexpr nullopt_t nullopt(unspecified); [...] Type nullopt_t shall not have a default constructor. It shall be a literal type. Constant nullopt shall be initialized with an argument of literal type. The reason for this is explained in the The op = {} syntax chapter of the document: for the op = {} to be unambiguous some tricks have to be adopted, one of which is that nullopt_t must not be default constructible. My question is about what does the literal type means here? I found

Casting to generic optional in Swift

人盡茶涼 提交于 2019-12-05 06:09:08
I'm fiddling around with generics in Swift and hit something I can't figure out: If I cast a value into the type of a generic parameter, the cast is not performed. If I try the same with static types, it works. class SomeClass<T> { init?() { if let _ = 4 as? T { println("should work") } else { return nil } } } if let _ = SomeClass<Int?>() { println("not called") } if let _ = 4 as? Int? { println("works") } Can anybody explain this behavior? Shouldn't be both cases equivalent? Update The above example is simplified to the max. The following example illustrates the need for a cast a little

Return type std::optional<std::variant<…>>

我怕爱的太早我们不能终老 提交于 2019-12-05 05:36:24
I have a situation where a function must return a value taken from a table. A cell in this table (let's assume the table just works...) may contain a value, or it might not. This value can also be one of several types: int, double, string, date (but no other type). What would such a function return? Is it a good idea to return std::optional<std::variant<std::string, int, double, std::chrono::time_point>> ? Would that be a good use of optional and variant ? I would consider this to be a useful use of std::monostate . Specifically, variant<std::monostate, int, double, std::string, std::chrono:

Type inference fails when using nil-coalescing operator with two optionals

扶醉桌前 提交于 2019-12-05 04:50:25
We are trying to figure whether this is a bug in Swift or us misusing generics, optionals, type inference and/or nil coalescing operator. Our framework contains some code for parsing dictionaries into models and we've hit a problem with optional properties with default values. We have a protocol SomeProtocol and two generic functions defined in a protocol extension: mapped<T>(...) -> T? mapped<T : SomeProtocol>(...) -> T? Our structs and classes adhere to this protocol and then parse their properties inside an init function required by the protocol. Inside the init(...) function we try to set

Optional<> and return type narrowing

蓝咒 提交于 2019-12-05 04:45:43
In Java < 8, returning "unsafe" objects (objects or null), I was able to specialize return type in subclass: class A {} class B extends A {} interface Sup { A a(); /* returns A instance, or null */ } interface Sub extends Sup { B a(); } In Java 8, if I want to make my API "safer", I should return Optional<A> instead of "raw" A : interface Sup { Optional<A> a(); } interface Sub extends Sup { Optional<B> a(); } But doesn't compile! Because Optional<B> is not a subclass of Optional<A> . How I'm supposed to resolve this issue? You could use wildcards. interface Sup { Optional<? extends A> a(); }