pnowojski commented on a change in pull request #10084: [FLINK-14382][yarn] Incorrect handling of FLINK_PLUGINS_DIR on Yarn URL: https://github.com/apache/flink/pull/10084#discussion_r345625618
########## File path: flink-java/src/main/java/org/apache/flink/api/java/utils/MultipleParameterTool.java ########## @@ -0,0 +1,251 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.api.java.utils; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.java.Utils; +import org.apache.flink.util.Preconditions; + +import org.apache.commons.lang3.math.NumberUtils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + * This class provides simple utility methods for reading and parsing program arguments from different sources. This is + * a enhanced tool for {@link ParameterTool} with multiple parameters support. + */ +@PublicEvolving +public class MultipleParameterTool extends ParameterTool { + private static final long serialVersionUID = 1L; + + // ------------------ Constructors ------------------------ + + /** + * Returns {@link MultipleParameterTool} for the given arguments. The arguments are keys followed by values. + * Keys have to start with '-' or '--' + * + * <p><strong>Example arguments:</strong> + * --key1 value1 --key2 value2 -key3 value3 + * --multi multiValue1 --multi multiValue2 + * + * @param args Input array arguments + * @return A {@link MultipleParameterTool} + */ + public static MultipleParameterTool fromArgs(String[] args) { + final Map<String, Collection<String>> map = new HashMap<>(args.length / 2); + + int i = 0; + while (i < args.length) { + final String key = Utils.getKeyFromArgs(args, i); + + i += 1; // try to find the value + + map.putIfAbsent(key, new ArrayList<>()); + if (i >= args.length) { + map.get(key).add(NO_VALUE_KEY); + } else if (NumberUtils.isNumber(args[i])) { + map.get(key).add(args[i]); + i += 1; + } else if (args[i].startsWith("--") || args[i].startsWith("-")) { + // the argument cannot be a negative number because we checked earlier + // -> the next argument is a parameter name + map.get(key).add(NO_VALUE_KEY); + } else { + map.get(key).add(args[i]); + i += 1; + } + } + + return fromMultiMap(map); + } + + /** + * Returns {@link MultipleParameterTool} for the given multi map. + * + * @param multiMap A map of arguments. Key is String and value is a Collection. + * @return A {@link MultipleParameterTool} + */ + public static MultipleParameterTool fromMultiMap(Map<String, Collection<String>> multiMap) { + Preconditions.checkNotNull(multiMap, "Unable to initialize from empty map"); + return new MultipleParameterTool(multiMap); + } + + // ------------------ ParameterUtil ------------------------ + protected final Map<String, Collection<String>> data; Review comment: Couple of comments here, I think you shouldn't be shading the original `data` variable. If you prefer such form of inheritance (`MultipleParameterTool extends ParameterTool`, as opposed to a composition or some base common class), please rename this `data` -> `multiParameterData` and also make it exclusive. If a parameter is present in one structure, it shouldn't be present in the other. Otherwise it can lead to confusion and bugs. Couple of methods do not make sense for the `MultiParameterTool`, like `#getConfiguration` or `#getProperties`, as those structures are backed by a regular `Map`. Override them here and throw `UnsupportedOperationException`? Alternative approach to all of those issues would be to define `AbstractParameterTool`, without a `data` field at all, and with couple of abstract methods, like `#get()`, `#has()` and basically all of the methods that you have implemented in this class as well - those that are different between `MultipleParameterTool` and `ParameterTool`. `ParameterTool` could define `Map<String, String> data` while `MultipleParameterTool` would use `Map<String, Collection<String>> data`, both with appropriate implementation of the abstract methods. + `ParameterTool` would define those methods, that do not make sense for the `MultipleParameterTool` like `#getConfiguration()`. What do you think? ---------------------------------------------------------------- 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
