/*
 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.util.containers;

import java.util.*;

/**
 * @author peter
 */
public class SortedList<T> extends AbstractList<T>{
  private final Comparator<T> myComparator;
  private boolean mySorted;
  private final List<T> myDelegate = new ArrayList<T>();

  public SortedList(final Comparator<T> comparator) {
    myComparator = comparator;
  }

  @Override
  public void add(final int index, final T element) {
    mySorted = false;
    myDelegate.add(index, element);
  }

  @Override
  public T remove(final int index) {
    return myDelegate.remove(index);
  }

  public T get(final int index) {
    if (!mySorted) {
      sort(myDelegate);
      mySorted = true;
    }
    return myDelegate.get(index);
  }

  protected void sort(List<T> delegate) {
    Collections.sort(myDelegate, myComparator);
  }

  @Override
  public void clear() {
    myDelegate.clear();
  }

  public int size() {
    return myDelegate.size();
  }
}

@@ -711,24 +711,115 @@
 nt index) %7B%0A
+    ensureSorted();%0A    return myDelegate.get(index);%0A  %7D%0A%0A  private void ensureSorted() %7B%0A
     if (!myS
@@ -823,24 +823,24 @@
 mySorted) %7B%0A
-
       sort(m
@@ -878,48 +878,14 @@
 ue;%0A
+
     %7D%0A
-    return myDelegate.get(index);%0A
   %7D%0A
@@ -1063,24 +1063,44 @@
 nt size() %7B%0A
+    ensureSorted();%0A
     return m

fix tests