/**
   * Adds an element to the list, inserting it at the given position. The indeces of elements at and
   * after that position must be updated accordignly.
   *
   * <p>If the index is negative or greater or equal than the size of the list, then an appropriate
   * error must be returned.
   *
   * <p>If a null object is provided to insert in the list, the request must be ignored and an
   * appropriate error must be returned.
   *
   * @param index the position at which the item should be inserted in the list
   * @param item the value to insert into the list
   * @return an ReturnObject, empty if the operation is successful or containing an appropriate
   *     error message otherwise
   */
  public ReturnObject add(int index, Object item) {
    ErrorMessage error1;
    ReturnObject returnobj;

    if (item.equals(null)) {
      error1 = ErrorMessage.INVALID_ARGUMENT;
    } else if (index < 0 || index >= size_list) {
      error1 = ErrorMessage.INDEX_OUT_OF_BOUNDS;
    } else {
      error1 = ErrorMessage.NO_ERROR;
    }

    if (error1.equals(ErrorMessage.NO_ERROR)) {
      Element elem = startElement;

      for (int j = 0; j < index + 1; j++) {
        elem = elem.getNext();
      }
      Element newElement = new Element(item);
      newElement.setNext(elem.getNext());
      elem.setNext(newElement);

      size_list++;
      returnobj = new ReturnObjectImpl(item);
    } else {
      returnobj = new ReturnObjectImpl(error1);
    }

    return returnobj;
  }
  /**
   * Returns a list with the elements in this list except the head. The elements must be in the same
   * order. The original list must not change or be affected by changes in the new list.
   *
   * <p>If the list is empty, another empty list is returned.
   */
  public FunctionalList rest() {

    ErrorMessage error1 = errortype(0);
    FunctionalList list1 = new FunctionalLinkedList();

    if (error1.equals(ErrorMessage.NO_ERROR)) {
      // list1.setList(this.getList());
      list1.remove(0);
      return list1;
    } else {
      return list1;
    }
  }
  /**
   * Returns the element at the beginning of the list.
   *
   * <p>If the list is empty, an appropriate error is returned.
   *
   * @return a copy of the element at the beginning of the list or an error if the list is empty.
   */
  public ReturnObject head() {

    ErrorMessage error1 = errortype(0);
    ReturnObject returnobj;

    if (error1.equals(ErrorMessage.NO_ERROR)) {
      Element elem = startElement;
      returnobj = new ReturnObjectImpl(elem.getNext().returnObject());
    } else {
      returnobj = new ReturnObjectImpl(error1);
    }

    return returnobj;
  }
  /**
   * Returns the element at the given position.
   *
   * <p>If the index is negative or greater or equal than the size of the list, then an appropriate
   * error must be returned.
   *
   * @param index the position in the list of the item to be retrieved
   * @return the element or an appropriate error message, encapsulated in a ReturnObject
   */
  public ReturnObject get(int index) {
    ErrorMessage error1 = errortype(index);
    ReturnObject returnobj;

    if (error1.equals(ErrorMessage.NO_ERROR)) {
      Element elem = startElement;
      for (int j = 0; j < index + 1; j++) {
        elem = elem.getNext();
      }
      returnobj = new ReturnObjectImpl(elem.returnObject());
    } else {
      returnobj = new ReturnObjectImpl(error1);
    }

    return returnobj;
  }
  /**
   * Returns the elements at the given position and removes it from the list. The indeces of
   * elements after the removed element must be updated accordignly.
   *
   * <p>If the index is negative or greater or equal than the size of the list, then an appropriate
   * error must be returned.
   *
   * @param index the position in the list of the item to be retrieved
   * @return the element or an appropriate error message, encapsulated in a ReturnObject
   */
  public ReturnObject remove(int index) {
    ErrorMessage error1 = errortype(index);
    ReturnObject returnobj;

    if (error1.equals(ErrorMessage.NO_ERROR) && size_list > 0) {

      Element elem = startElement;
      // Element elem2=startElement;

      for (int j = 0; j < index + 1; j++) {
        elem = elem.getNext();
      }

      elem.setNext(elem.getNext().getNext());

      returnobj = new ReturnObjectImpl(elem.getNext().returnObject());
      size_list--;

    } else {
      returnobj = new ReturnObjectImpl(error1);
    }

    return returnobj;
  }
Esempio n. 6
0
 public boolean hasError() {
   return (errorMessage.equals(ErrorMessage.NO_ERROR) == false);
 }