more efficient SortedList.remove
[fedora-idea.git] / platform / util / src / com / intellij / util / containers / SortedList.java
blob61c3114ba1ea8ce8119b83e9bc1d4ce3eaad752b
1 /*
2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
5 package com.intellij.util.containers;
7 import java.util.*;
9 /**
10 * @author peter
12 public class SortedList<T> extends AbstractList<T>{
13 private final Comparator<T> myComparator;
14 private boolean mySorted;
15 private final List<T> myDelegate = new ArrayList<T>();
17 public SortedList(final Comparator<T> comparator) {
18 myComparator = comparator;
21 @Override
22 public void add(final int index, final T element) {
23 mySorted = false;
24 myDelegate.add(index, element);
27 @Override
28 public T remove(final int index) {
29 return myDelegate.remove(index);
32 @Override
33 public boolean remove(Object o) {
34 ensureSorted();
35 final int i = Collections.binarySearch(myDelegate, (T)o, myComparator);
36 if (i >= 0) {
37 myDelegate.remove(i);
38 return true;
40 return false;
43 public T get(final int index) {
44 ensureSorted();
45 return myDelegate.get(index);
48 private void ensureSorted() {
49 if (!mySorted) {
50 sort(myDelegate);
51 mySorted = true;
55 protected void sort(List<T> delegate) {
56 Collections.sort(myDelegate, myComparator);
59 @Override
60 public void clear() {
61 myDelegate.clear();
64 @Override
65 public Iterator<T> iterator() {
66 ensureSorted();
67 return super.iterator();
70 @Override
71 public ListIterator<T> listIterator() {
72 ensureSorted();
73 return super.listIterator();
76 public int size() {
77 return myDelegate.size();