/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 Christian Stein
 *
 * Licensed 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
 *
 *     https://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 de.sormuras.bach.tool;

import de.sormuras.bach.Tool;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Optional;
import java.util.spi.ToolProvider;

/** An abstract tool implementation providing support for additional arguments. */
public /*static*/ abstract class AbstractTool implements Tool {

  private final String name;
  private final Arguments additionalArguments = new Arguments();

  public AbstractTool(String name) {
    this.name = name;
  }

  public Arguments getAdditionalArguments() {
    return additionalArguments;
  }

  @Override
  public ToolProvider toolProvider() {
    return ToolProvider.findFirst(name).orElseThrow();
  }

  @Override
  public String[] toolArguments() {
    var arguments = new Arguments();
    arguments(arguments);
    return arguments.add(getAdditionalArguments()).toStringArray();
  }

  protected void arguments(Arguments arguments) {}

  /** Return {@code true} if the given object is not null in any form, otherwise {@code false}. */
  protected boolean assigned(Object object) {
    if (object == null) return false;
    if (object instanceof Number) return ((Number) object).intValue() != 0;
    if (object instanceof String) return !((String) object).isEmpty();
    if (object instanceof Optional) return ((Optional<?>) object).isPresent();
    if (object instanceof Collection) return !((Collection<?>) object).isEmpty();
    if (object.getClass().isArray()) return Array.getLength(object) != 0;
    return true;
  }
}

@@ -705,26 +705,74 @@
 ava.
-lang.reflect.Array
+io.File;%0Aimport java.lang.reflect.Array;%0Aimport java.nio.file.Path
 ;%0Aim
@@ -859,16 +859,52 @@
 rovider;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/** An
@@ -1051,1179 +1051,1454 @@
 %0A%0A  
-private final String name;%0A  private final Arguments additionalArguments = new Arguments();%0A%0A  public AbstractTool(String name) %7B%0A    this.name = name;%0A  %7D%0A%0A  public Arguments getAdditionalArguments() %7B%0A    return additionalArguments;%0A  %7D%0A%0A  @Override%0A  public ToolProvider toolProvider() %7B%0A    return ToolProvider.findFirst(name).orElseThrow();%0A  %7D%0A%0A  @Override%0A  public String%5B%5D toolArguments() %7B%0A    var arguments = new Arguments();%0A    arguments(arguments);%0A    return arguments.add(getAdditionalArguments()).toStringArray();%0A  %7D%0A%0A  protected void arguments(Arguments arguments) %7B%7D%0A%0A  /** Return %7B@code true%7D if the given object is not null in any form, otherwise %7B@code false%7D. */%0A  protected boolean assigned(Object object) %7B%0A    if (object == null) return false;%0A    if (object instanceof Number) return ((Number) object).intValue() != 0;%0A    if (object instanceof String) return !((String) object).isEmpty();%0A    if (object instanceof Optional) return ((Optional%3C?%3E) object).isPresent();%0A    if (object instanceof Collection) return !((Collection%3C?%3E) object).isEmpty();%0A    if (object.getClass().isArray()) return Array.getLength(object) != 0;%0A    return true;%0A  
+/** Return %7B@code true%7D if the given object is not null in any form, otherwise %7B@code false%7D. */%0A  public static boolean assigned(Object object) %7B%0A    if (object == null) return false;%0A    if (object instanceof Number) return ((Number) object).intValue() != 0;%0A    if (object instanceof String) return !((String) object).isEmpty();%0A    if (object instanceof Optional) return ((Optional%3C?%3E) object).isPresent();%0A    if (object instanceof Collection) return !((Collection%3C?%3E) object).isEmpty();%0A    if (object.getClass().isArray()) return Array.getLength(object) != 0;%0A    return true;%0A  %7D%0A%0A  public static String join(Collection%3CPath%3E paths) %7B%0A    return paths.stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator));%0A  %7D%0A%0A  public static String joinPaths(Collection%3CString%3E paths) %7B%0A    return String.join(File.pathSeparator, paths);%0A  %7D%0A%0A  private final String name;%0A  private final Arguments additionalArguments = new Arguments();%0A%0A  public AbstractTool(String name) %7B%0A    this.name = name;%0A  %7D%0A%0A  public Arguments getAdditionalArguments() %7B%0A    return additionalArguments;%0A  %7D%0A%0A  @Override%0A  public ToolProvider toolProvider() %7B%0A    return ToolProvider.findFirst(name).orElseThrow();%0A  %7D%0A%0A  @Override%0A  public String%5B%5D toolArguments() %7B%0A    var arguments = new Arguments();%0A    arguments(arguments);%0A    return arguments.add(getAdditionalArguments()).toStringArray();%0A  %7D%0A%0A  protected void arguments(Arguments arguments) %7B
 %7D%0A%7D%0A

Extend abstract tool with static helpers