From 07d31aa44ad208202acd4818ba71d07c7ba79bd8 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 3 Oct 2016 11:02:12 +0300 Subject: [PATCH] SortedListModel moved to platform-api where Java 8 is available and changed to use ComboBoxModel with generic parameter --- .../src/com/intellij/ui/SortedListModel.java | 43 ++++++++++--------- .../com/intellij/ui/SortedListModelTest.java | 5 +-- 2 files changed, 25 insertions(+), 23 deletions(-) rename platform/{util => platform-api}/src/com/intellij/ui/SortedListModel.java (84%) rename platform/{util => platform-tests}/testSrc/com/intellij/ui/SortedListModelTest.java (94%) diff --git a/platform/util/src/com/intellij/ui/SortedListModel.java b/platform/platform-api/src/com/intellij/ui/SortedListModel.java similarity index 84% rename from platform/util/src/com/intellij/ui/SortedListModel.java rename to platform/platform-api/src/com/intellij/ui/SortedListModel.java index aba4518b615a..7a0db37d7f61 100644 --- a/platform/util/src/com/intellij/ui/SortedListModel.java +++ b/platform/platform-api/src/com/intellij/ui/SortedListModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,12 @@ package com.intellij.ui; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.ArrayUtil; import javax.swing.*; import java.util.*; -public class SortedListModel extends AbstractListModel { - private List myItems = new ArrayList(); +public class SortedListModel extends AbstractListModel { + private List myItems = new ArrayList<>(); private final Comparator myComparator; public SortedListModel(Comparator comparator) { @@ -30,30 +29,24 @@ public class SortedListModel extends AbstractListModel { } public static SortedListModel create(Comparator comparator) { - return new SortedListModel(comparator); + return new SortedListModel<>(comparator); } public int add(T item) { int index; - if (myComparator != null) + if (myComparator != null) { index = Collections.binarySearch(myItems, item, myComparator); - else + } + else { index = myItems.size(); + } index = index >= 0 ? index : -(index + 1); add(index, item); return index; } - public int[] addAll(Object[] items) { - int[] indices = new int[items.length]; - for (int i = 0; i < items.length; i++) { - T item = (T)items[i]; - int newIndex = add(item); - for (int j = 0; j < i ; j++) - if (indices[j] >= newIndex) indices[j]++; - indices[i] = newIndex; - } - return indices; + public int[] addAll(T[] items) { + return addAll(Arrays.asList(items)); } public int[] addAll(Iterator iterator) { @@ -61,7 +54,17 @@ public class SortedListModel extends AbstractListModel { } public int[] addAll(Collection items) { - return addAll(ArrayUtil.toObjectArray(items)); + int[] indices = new int[items.size()]; + int i = 0; + for (T item : items) { + int newIndex = add(item); + for (int j = 0; j < i; j++) { + if (indices[j] >= newIndex) indices[j]++; + } + indices[i] = newIndex; + i++; + } + return indices; } public void remove(int index) { @@ -94,7 +97,7 @@ public class SortedListModel extends AbstractListModel { } @Override - public Object getElementAt(int index) { + public T getElementAt(int index) { return myItems.get(index); } @@ -108,7 +111,7 @@ public class SortedListModel extends AbstractListModel { public void clear() { int oldSize = getSize(); - myItems = new ArrayList(); + myItems = new ArrayList<>(); if (oldSize > 0) fireIntervalRemoved(this, 0, oldSize - 1); } diff --git a/platform/util/testSrc/com/intellij/ui/SortedListModelTest.java b/platform/platform-tests/testSrc/com/intellij/ui/SortedListModelTest.java similarity index 94% rename from platform/util/testSrc/com/intellij/ui/SortedListModelTest.java rename to platform/platform-tests/testSrc/com/intellij/ui/SortedListModelTest.java index ab8604f0eaf5..ff56eb715275 100644 --- a/platform/util/testSrc/com/intellij/ui/SortedListModelTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ui/SortedListModelTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,8 +26,7 @@ import java.util.Comparator; import java.util.Iterator; public class SortedListModelTest extends TestCase { - private final SortedListModel myModel = new SortedListModel((o1, o2) -> ((Comparable) o1).compareTo((Comparable) o2) - ); + private final SortedListModel myModel = new SortedListModel<>((o1, o2) -> o1.compareTo(o2)); private final Assertion CHECK = new Assertion(); public void testAdding() {