Skip to content

Commit

Permalink
Merge pull request #172 from JunkFood02/option_list
Browse files Browse the repository at this point in the history
Change Map to List to use the same command option multiple times
  • Loading branch information
p3g4asus authored Aug 7, 2022
2 parents 218763a + 73662e6 commit 3a45309
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,71 @@

public class YoutubeDLOptions {

private Map<String, String> options = new LinkedHashMap<>();
private final Map<String, List<String>> options = new LinkedHashMap<>();

public YoutubeDLOptions addOption(@NonNull String key, @NonNull String value){
options.put(key, value);
public YoutubeDLOptions addOption(@NonNull String option, @NonNull String argument) {
if (!options.containsKey(option)) {
List<String> arguments = new ArrayList<>();
arguments.add(argument);
options.put(option, arguments);
} else {
options.get(option).add(argument);
}
return this;
}

public YoutubeDLOptions addOption(@NonNull String key, @NonNull Number value){
options.put(key, value.toString());
public YoutubeDLOptions addOption(@NonNull String option, @NonNull Number argument) {
if (!options.containsKey(option)) {
List<String> arguments = new ArrayList<>();
arguments.add(argument.toString());
options.put(option, arguments);
} else {
options.get(option).add(argument.toString());
}
return this;
}

public YoutubeDLOptions addOption(String key){
options.put(key, null);
public YoutubeDLOptions addOption(String option) {
if (!options.containsKey(option)) {
List<String> arguments = new ArrayList<>();
arguments.add("");
options.put(option, arguments);
} else {
options.get(option).add("");
}
return this;
}

public Object getOption(String key){
return options.get(key);
public String getArgument(String option) {
if (!options.containsKey(option))
return null;
String argument = options.get(option).get(0);
if (argument.isEmpty())
return null;
else return argument;
}

public List<String> getArguments(String option) {
if (!options.containsKey(option))
return null;
return options.get(option);
}

public boolean hasOption(String key){
return options.containsKey(key);
public boolean hasOption(String option) {
return options.containsKey(option);
}

public List<String> buildOptions(){
List<String> optionsList = new ArrayList<>();
for (Map.Entry<String, String> entry : options.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
optionsList.add(name);
if(null != value) optionsList.add(value);
public List<String> buildOptions() {
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : options.entrySet()) {
String option = entry.getKey();
for (String argument : entry.getValue()) {
commandList.add(option);
if (!argument.isEmpty())
commandList.add(argument);
}
}
return optionsList;
return commandList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,60 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class YoutubeDLRequest {

private List<String> urls;
private YoutubeDLOptions options = new YoutubeDLOptions();
private final List<String> urls;
private final YoutubeDLOptions options = new YoutubeDLOptions();

private final List<String> commandList = new ArrayList<>();

public YoutubeDLRequest(String url) {
this.urls = Arrays.asList(url);
this.urls = Collections.singletonList(url);
}

public YoutubeDLRequest(@NonNull List<String> urls) {
public YoutubeDLRequest(@NonNull List<String> urls) {
this.urls = urls;
}

public YoutubeDLRequest addOption(@NonNull String key, @NonNull String value){
options.addOption(key, value);
public YoutubeDLRequest addOption(@NonNull String option, @NonNull String argument) {
options.addOption(option, argument);
return this;
}

public YoutubeDLRequest addOption(@NonNull String option, @NonNull Number argument) {
options.addOption(option, argument);
return this;
}

public YoutubeDLRequest addOption(@NonNull String key, @NonNull Number value){
options.addOption(key, value);
public YoutubeDLRequest addOption(String option) {
options.addOption(option);
return this;
}

public YoutubeDLRequest addOption(String key){
options.addOption(key);
public YoutubeDLRequest addCommands(List<String> commands) {
commandList.addAll(commands);
return this;
}

public Object getOption(String key){
return options.getOption(key);
public String getOption(String option) {
return options.getArgument(option);
}

public List<String> getArguments(String option) {
return options.getArguments(option);
}

public boolean hasOption(String key){
return options.hasOption(key);
public boolean hasOption(String option) {
return options.hasOption(option);
}

public List<String> buildCommand(){
List<String> command = new ArrayList<>();
command.addAll(options.buildOptions());
command.addAll(urls);
return command;
public List<String> buildCommand() {
commandList.addAll(options.buildOptions());
commandList.addAll(urls);
return commandList;
}

}

0 comments on commit 3a45309

Please sign in to comment.