package joptsimple;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import joptsimple.internal.Reflection;
import joptsimple.internal.ReflectionException;

abstract class AbstractOptionSpec<V> implements OptionSpec<V>, OptionDescriptor {
   private final List<String> options;
   private final String description;
   private boolean forHelp;

   protected AbstractOptionSpec(String option) {
      this(Collections.singletonList(option), "");
   }

   protected AbstractOptionSpec(Collection<String> options, String description) {
      this.options = new ArrayList();
      this.arrangeOptions(options);
      this.description = description;
   }

   public final Collection<String> options() {
      return Collections.unmodifiableList(this.options);
   }

   public final List<V> values(OptionSet detectedOptions) {
      return detectedOptions.valuesOf((OptionSpec)this);
   }

   public final V value(OptionSet detectedOptions) {
      return detectedOptions.valueOf((OptionSpec)this);
   }

   public String description() {
      return this.description;
   }

   public final AbstractOptionSpec<V> forHelp() {
      this.forHelp = true;
      return this;
   }

   public final boolean isForHelp() {
      return this.forHelp;
   }

   public boolean representsNonOptions() {
      return false;
   }

   protected abstract V convert(String var1);

   protected V convertWith(ValueConverter<V> converter, String argument) {
      try {
         return Reflection.convertWith(converter, argument);
      } catch (ReflectionException var4) {
         throw new OptionArgumentConversionException(this.options(), argument, converter.valueType(), var4);
      } catch (ValueConversionException var5) {
         throw new OptionArgumentConversionException(this.options(), argument, converter.valueType(), var5);
      }
   }

   protected String argumentTypeIndicatorFrom(ValueConverter<V> converter) {
      if (converter == null) {
         return null;
      } else {
         String pattern = converter.valuePattern();
         return pattern == null ? converter.valueType().getName() : pattern;
      }
   }

   abstract void handleOption(OptionParser var1, ArgumentList var2, OptionSet var3, String var4);

   private void arrangeOptions(Collection<String> unarranged) {
      if (unarranged.size() == 1) {
         this.options.addAll(unarranged);
      } else {
         List<String> shortOptions = new ArrayList();
         List<String> longOptions = new ArrayList();
         Iterator i$ = unarranged.iterator();

         while(i$.hasNext()) {
            String each = (String)i$.next();
            if (each.length() == 1) {
               shortOptions.add(each);
            } else {
               longOptions.add(each);
            }
         }

         Collections.sort(shortOptions);
         Collections.sort(longOptions);
         this.options.addAll(shortOptions);
         this.options.addAll(longOptions);
      }
   }

   public boolean equals(Object that) {
      if (!(that instanceof AbstractOptionSpec)) {
         return false;
      } else {
         AbstractOptionSpec<?> other = (AbstractOptionSpec)that;
         return this.options.equals(other.options);
      }
   }

   public int hashCode() {
      return this.options.hashCode();
   }

   public String toString() {
      return this.options.toString();
   }
}
