All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.shaded.vertx.core.spi.launcher.DefaultCommandFactory Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.spi.launcher;

import com.arangodb.shaded.vertx.core.cli.CLI;
import com.arangodb.shaded.vertx.core.cli.CommandLine;
import com.arangodb.shaded.vertx.core.cli.annotations.CLIConfigurator;
import com.arangodb.shaded.vertx.core.cli.impl.ReflectionUtils;

import java.util.function.Supplier;

/**
 * Default implementation of {@link CommandFactory}. This implementation defines the {@link CLI} from the
 * given {@link Command} implementation (by reading the annotation). Then, {@link Command} instance are
 * created by calling an empty constructor on the given {@link Command} implementation.
 *
 * @author Clement Escoffier 
 */
public class DefaultCommandFactory implements CommandFactory {

  private final Class clazz;
  private final Supplier supplier;

  /**
   * Creates a new {@link CommandFactory}.
   *
   * @param clazz the {@link Command} implementation
   * @deprecated Please use {@link #DefaultCommandFactory(Class, Supplier)}
   */
  @Deprecated
  public DefaultCommandFactory(Class clazz) {
    this(clazz, () -> ReflectionUtils.newInstance(clazz));
  }

  /**
   * Creates a new {@link CommandFactory}.
   *
   * @param clazz the {@link Command} implementation
   * @param supplier the {@link Command} implementation
   */
  public DefaultCommandFactory(Class clazz, Supplier supplier) {
    this.clazz = clazz;
    this.supplier = supplier;
  }

  /**
   * @return a new instance of the command by invoking the default constructor of the given class.
   */
  @Override
  public C create(CommandLine cl) {
    return supplier.get();
  }

  /**
   * @return the {@link CLI} instance by reading the annotation.
   */
  @Override
  public CLI define() {
    return CLIConfigurator.define(clazz);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy