<commit_before>package ie.ucd.clops.runtime.options;

import ie.ucd.clops.runtime.options.exception.InvalidOptionPropertyValueException;
import ie.ucd.clops.runtime.options.exception.InvalidOptionValueException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * @author Fintan
 *
 * @param <T>
 */
public abstract class ListOption<T> extends OneArgumentOption<List<T>> {

  public static final String DEFAULT_SPLIT = ",";

  private List<T> value;
  private boolean allowMultiple;
  protected String splittingString;

  public ListOption(String identifier, String prefix) {
    super(identifier, prefix);
    this.value = new LinkedList<T>();
    this.allowMultiple = true;
    this.splittingString = DEFAULT_SPLIT;

    try {
      super.setProperty(ARGUMENTNAME, "<args>");
    } catch (InvalidOptionPropertyValueException e) {};
  }

  public List<T> getRawValue() {
    return value;
  }

  public void set(List<T> value) throws InvalidOptionValueException {
    this.value = value;
  }

  public void setFromString(String valueString) throws InvalidOptionValueException {
    if (allowMultiple) {
      String[] parts = valueString.split(splittingString);
      for (String part : parts) {
        T newValue = convertFromStringToListValue(part);
        value.add(newValue);
      }
    } else {
      T newValue = convertFromStringToListValue(valueString);
      value.add(newValue);
    }
  }

  /**
   * Convert from a String to a value to be stored in the list.
   * @param valueString the String to be converted.
   * @return an object of the appropriate type after conversion.
   * @throws InvalidOptionValueException if the String provided is invalid.
   */
  public abstract T convertFromStringToListValue(String valueString) throws InvalidOptionValueException;



  /**
   * This should never be used, but is required by the interface of Option.
   */
  public final List<T> convertStringToValue(String valueString) {
    //Should not be used
    assert false;
    return null;
  }

  public void unset() {
    this.value = new LinkedList<T>();
  }

  //Static for space/time efficiency (we don't need one per instance) 
  private static Collection<String> acceptedPropertyNames; 
  protected static Collection<String> getStaticAcceptedPropertyNames() {
    if (acceptedPropertyNames == null) {
      acceptedPropertyNames = new LinkedList<String>();  
      acceptedPropertyNames.addAll(OneArgumentOption.getStaticAcceptedPropertyNames());
      acceptedPropertyNames.add("allowmultiple");
      acceptedPropertyNames.add("splitter");
    }
    return acceptedPropertyNames;
  }

  @Override
  public Collection<String> getAcceptedPropertyNames() {
    return getStaticAcceptedPropertyNames();
  }

  @Override
  public void setProperty(String propertyName, String propertyValue)
  throws InvalidOptionPropertyValueException {
    if (propertyName.equalsIgnoreCase("allowmultiple")) {
      allowMultiple = Options.parseBooleanProperty(propertyName, propertyValue);
    } else if (propertyName.equalsIgnoreCase("splitter")) {
      splittingString = propertyValue;
    } else {
      super.setProperty(propertyName, propertyValue);
    }<commit_msg>Fixing the behaviour of list options wrt setting, unsetting and hasValue().<commit_after>package ie.ucd.clops.runtime.options;

import ie.ucd.clops.runtime.options.exception.InvalidOptionPropertyValueException;
import ie.ucd.clops.runtime.options.exception.InvalidOptionValueException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * @author Fintan
 *
 * @param <T>
 */
public abstract class ListOption<T> extends OneArgumentOption<List<T>> {

  public static final String DEFAULT_SPLIT = ",";

  /** A variable indicating if this list option has been set. */
  private boolean isSet;
  
  private List<T> value;
  private boolean allowMultiple;
  protected String splittingString;

  public ListOption(String identifier, String prefix) {
    super(identifier, prefix);
    this.value = new LinkedList<T>();
    this.allowMultiple = true;
    this.splittingString = DEFAULT_SPLIT;
    this.isSet = false;
    
    try {
      super.setProperty(ARGUMENTNAME, "<args>");
    } catch (InvalidOptionPropertyValueException e) {};
  }

  public List<T> getRawValue() {
    return value;
  }

  public void set(List<T> value) throws InvalidOptionValueException {
    this.value = value;
    this.isSet = true;
  }

  public void setFromString(String valueString) throws InvalidOptionValueException {
    if (allowMultiple) {
      String[] parts = valueString.split(splittingString);
      for (String part : parts) {
        T newValue = convertFromStringToListValue(part);
        value.add(newValue);
      }
    } else {
      T newValue = convertFromStringToListValue(valueString);
      value.add(newValue);
    }
    isSet = true;
  }

  /**
   * Convert from a String to a value to be stored in the list.
   * @param valueString the String to be converted.
   * @return an object of the appropriate type after conversion.
   * @throws InvalidOptionValueException if the String provided is invalid.
   */
  public abstract T convertFromStringToListValue(String valueString) throws InvalidOptionValueException;

  /**
   * This should never be used, but is required by the interface of Option.
   */
  public final List<T> convertStringToValue(String valueString) {
    //Should not be used
    assert false;
    return null;
  }

  public void unset() {
    this.value = new LinkedList<T>();
    this.isSet = false;
  }

  @Override
  public boolean hasValue() {
    return isSet;
  }

  //Static for space/time efficiency (we don't need one per instance) 
  private static Collection<String> acceptedPropertyNames; 
  protected static Collection<String> getStaticAcceptedPropertyNames() {
    if (acceptedPropertyNames == null) {
      acceptedPropertyNames = new LinkedList<String>();  
      acceptedPropertyNames.addAll(OneArgumentOption.getStaticAcceptedPropertyNames());
      acceptedPropertyNames.add("allowmultiple");
      acceptedPropertyNames.add("splitter");
    }
    return acceptedPropertyNames;
  }

  @Override
  public Collection<String> getAcceptedPropertyNames() {
    return getStaticAcceptedPropertyNames();
  }

  @Override
  public void setProperty(String propertyName, String propertyValue)
  throws InvalidOptionPropertyValueException {
    if (propertyName.equalsIgnoreCase("allowmultiple")) {
      allowMultiple = Options.parseBooleanProperty(propertyName, propertyValue);
    } else if (propertyName.equalsIgnoreCase("splitter")) {
      splittingString = propertyValue;
    } else {
      super.setProperty(propertyName, propertyValue);
    }
{'fraction_non_alphanumeric': 0.09213850497845148, 'fraction_numerical': 0.0, 'mean_word_length': 3.93040293040293, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '526301', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1817, 'n_words': 489}