package jetbrains.mps.lang.test.pluginSolution.plugin;

/*Generated by MPS */

import javax.swing.AbstractListModel;
import java.util.List;
import java.util.ArrayList;

/*package*/ class LM<E> extends AbstractListModel<E> {
  private final List<E> myElements;
  private final int[] myIndex;

  /*package*/ LM(List<E> elements) {
    myElements = elements;
    myIndex = new int[elements.size()];
    for (int i = 0; i < myIndex.length;) {
      myIndex[i] = i++;
    }
  }

  @Override
  public int getSize() {
    return myIndex.length;
  }

  @Override
  public E getElementAt(int i) {
    return myElements.get(myIndex[i]);
  }

  public void moveUp(int i) {
    if (i <= 0) {
      return;
    }
    int a = myIndex[i];
    myIndex[i] = myIndex[i - 1];
    myIndex[i - 1] = a;
    fireContentsChanged(this, i - 1, i);
  }

  public void moveDown(int i) {
    if (i >= myIndex.length - 1) {
      return;
    }
    int a = myIndex[i];
    myIndex[i] = myIndex[i + 1];
    myIndex[i + 1] = a;
    fireContentsChanged(this, i, i + 1);
  }

  public List<E> toOrder() {
    ArrayList<E> rv = new ArrayList<>(myIndex.length);
    for (int i = 0; i < myIndex.length; i++) {
      rv.add(myElements.get(myIndex[i]));
    }
    return rv;
  }

  public boolean isOrderChanged() {
    for (int i = 0; i < myIndex.length; i++) {
      if (myIndex[i] != i) {
        return true;
      }
    }
    return false;
  }
}
