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

objectos.way.AppOption Maven / Gradle / Ivy

There is a newer version: 0.1.11
Show newest version
/*
 * Copyright (C) 2023-2024 Objectos Software LTDA.
 *
 * 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
 *
 * 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 objectos.way;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

final class AppOption implements App.Option {

  private record Validator(Predicate predicate, String reasonPhrase) {
    public final void accept(AppBootstrap collector, String name, T value) {
      if (!predicate.test(value)) {
        collector.addMessage("Invalid " + name + " value: " + reasonPhrase);
      }
    }
  }

  private final App.Option.Converter converter;

  private RuntimeException error;

  private final String name;

  private boolean required;

  private List> validators;

  private T value;

  AppOption(String name, Converter converter) {
    this.name = name;

    this.converter = converter;
  }

  @Override
  public final T get() {
    return value;
  }

  final int accept(String[] args, int index) {
    if (index < args.length) {
      String arg;
      arg = args[index++];

      try {
        value = converter.convert(arg);
      } catch (RuntimeException e) {
        error = e;
      }
    }

    return index;
  }

  final void acceptByName(Map> map) {
    AppOption previous;
    previous = map.put(name, this);

    if (previous != null) {
      throw new IllegalArgumentException("Duplicate option name: " + name);
    }
  }

  final void addValidator(Predicate predicate, String reasonPhrase) {
    if (validators == null) {
      validators = Util.createList();
    }

    validators.add(
        new Validator<>(predicate, reasonPhrase)
    );
  }

  final void required() {
    required = true;
  }

  final void set(T newValue) {
    value = newValue;
  }

  final void validate(AppBootstrap collector) {
    if (error != null) {
      String message;
      message = error.getMessage();

      message = String.valueOf(message);

      collector.addMessage(message);
    }

    if (required && value == null) {
      String message;
      message = "Missing required option: " + name;

      collector.addMessage(message);
    }

    if (validators != null && value != null) {

      int sizeBefore;
      sizeBefore = collector.messagesSize();

      for (Validator validator : validators) {
        validator.accept(collector, name, value);
      }

      int sizeAfter;
      sizeAfter = collector.messagesSize();

      if (sizeAfter > sizeBefore) {
        value = null;
      }

    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy