ex00 commented on a change in pull request #8632: [FLINK-12744][ml] add shared
params in ml package
URL: https://github.com/apache/flink/pull/8632#discussion_r293098782
##########
File path:
flink-ml-parent/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java
##########
@@ -47,14 +70,33 @@
* @throws RuntimeException if the Params doesn't contains the specific
parameter, while the
* param is not optional but has no default
value in the {@code info}
*/
- @SuppressWarnings("unchecked")
public <V> V get(ParamInfo<V> info) {
- V value = (V) paramMap.getOrDefault(info.getName(),
info.getDefaultValue());
- if (value == null && !info.isOptional() &&
!info.hasDefaultValue()) {
- throw new RuntimeException(info.getName() +
- " not exist which is not optional and don't
have a default value");
+ Stream<V> stream = getParamNameAndAlias(info)
+ .filter(this.params::containsKey)
+ .map(x -> this.params.get(x))
+ .map(x -> valueFromJson(x, info.getValueClass()))
+ .limit(1);
Review comment:
> If not use stream object, we need more codes.
>
> For the value of parameter may be null, and the findFirst method is not
friendly for the null, thus we chose the limit(.) method.
>
> When the user sets a Null as the Value of Param, using the findFirst
method in the Stream class will throw NullPointerException.
> Looking deep into the code, findFirst returns an instance of Optional,
> but Optional can only use non-null value as a constructor argument.
> For example, the following code will throw NullPointerException.
> ` Stream.of(null, "2").findFirst(); `
I suppose if we will have `null` in the stream is an invalid state, and we
need to handle this situation.
anyway, we can filtering `null` before getting results, for e.g. by
`.filter(Objects::nonNull)`
I agree with @becketqin and @c4emmmm that the code without stream will be
much easier to reading
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services