<commit_before>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.ui.impl.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jboss.forge.addon.ui.annotation.Command;
import org.jboss.forge.addon.ui.annotation.Option;
import org.jboss.forge.addon.ui.annotation.handler.EnableCommandHandler;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.impl.input.InputComponentProducer;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.addon.ui.util.Metadata;

/**
 * This class acts as an adapter to the UI API for methods with the annotation @Command
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class AnnotationCommandAdapter implements UICommand
{
   private final List<InputComponent<?, ?>> inputs = new ArrayList<>();
   private final InputComponentProducer factory;
   private final Method method;
   private final Object instance;
   private final EnableCommandHandler enabledHandler;

   public AnnotationCommandAdapter(Method method, Object instance, InputComponentProducer factory,
            EnableCommandHandler handler)
   {
      this.method = method;
      this.instance = instance;
      this.factory = factory;
      this.enabledHandler = handler;
   }

   @Override
   public UICommandMetadata getMetadata(UIContext context)
   {
      Command ann = method.getAnnotation(Command.class);
      String name = ann.value();
      if (name.isEmpty())
      {
         name = method.getName();
      }
      return Metadata.forCommand(method.getDeclaringClass()).name(name).description(ann.help());
   }

   @Override
   public boolean isEnabled(UIContext context)
   {
      return enabledHandler.isEnabled(context);
   }

   @Override
   public void initializeUI(UIBuilder builder) throws Exception
   {
      int position = 0;
      for (Class<?> parameterType : method.getParameterTypes())
      {
         if (!ReservedParameters.isReservedParameter(parameterType))
         {
            Option option = null;
            for (Annotation annotation : method.getParameterAnnotations()[position])
            {
               if (annotation instanceof Option)
               {
                  option = (Option) annotation;
               }
            }

            String paramName = (option == null || option.value().isEmpty()) ? "param" + position : option.value();
            InputComponent<?, ?> input;
            if (Collection.class.isAssignableFrom(parameterType))
            {
               // TODO: UIInputMany or UISelectMany ?
               input = factory.createInputMany(paramName, parameterType);
            }
            else if (parameterType.isEnum() || Boolean.class == parameterType)
            {
               input = factory.createSelectOne(paramName, parameterType);
               factory.setupSelectComponent((SelectComponent<?, ?>) input);
            }
            else<commit_msg>Using Iterable instead of Collection<commit_after>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.ui.impl.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.jboss.forge.addon.ui.annotation.Command;
import org.jboss.forge.addon.ui.annotation.Option;
import org.jboss.forge.addon.ui.annotation.handler.EnableCommandHandler;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.impl.input.InputComponentProducer;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.addon.ui.util.Metadata;

/**
 * This class acts as an adapter to the UI API for methods with the annotation @Command
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class AnnotationCommandAdapter implements UICommand
{
   private final List<InputComponent<?, ?>> inputs = new ArrayList<>();
   private final InputComponentProducer factory;
   private final Method method;
   private final Object instance;
   private final EnableCommandHandler enabledHandler;

   public AnnotationCommandAdapter(Method method, Object instance, InputComponentProducer factory,
            EnableCommandHandler handler)
   {
      this.method = method;
      this.instance = instance;
      this.factory = factory;
      this.enabledHandler = handler;
   }

   @Override
   public UICommandMetadata getMetadata(UIContext context)
   {
      Command ann = method.getAnnotation(Command.class);
      String name = ann.value();
      if (name.isEmpty())
      {
         name = method.getName();
      }
      return Metadata.forCommand(method.getDeclaringClass()).name(name).description(ann.help());
   }

   @Override
   public boolean isEnabled(UIContext context)
   {
      return enabledHandler.isEnabled(context);
   }

   @Override
   public void initializeUI(UIBuilder builder) throws Exception
   {
      int position = 0;
      for (Class<?> parameterType : method.getParameterTypes())
      {
         if (!ReservedParameters.isReservedParameter(parameterType))
         {
            Option option = null;
            for (Annotation annotation : method.getParameterAnnotations()[position])
            {
               if (annotation instanceof Option)
               {
                  option = (Option) annotation;
               }
            }

            String paramName = (option == null || option.value().isEmpty()) ? "param" + position : option.value();
            InputComponent<?, ?> input;
            if (Iterable.class.isAssignableFrom(parameterType))
            {
               // TODO: UIInputMany or UISelectMany ?
               input = factory.createInputMany(paramName, parameterType);
            }
            else if (parameterType.isEnum() || Boolean.class == parameterType)
            {
               input = factory.createSelectOne(paramName, parameterType);
               factory.setupSelectComponent((SelectComponent<?, ?>) input);
            }
            else
{'fraction_non_alphanumeric': 0.10725938009787928, 'fraction_numerical': 0.0024469820554649264, 'mean_word_length': 3.0312328767123287, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '22444', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2028, 'n_words': 423}