1 // $Id: SortedList.java,v 1.1.1.1 2002/09/29 17:26:07 powerpete Exp $
2 package de.jface.util;
3
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7
8 /***
9 * Class <code>SortedList</code>
10 * of project RemotePostIt.
11 *
12 * @author Moritz Petersen
13 * @version $Revision: 1.1.1.1 $
14 */
15 public class SortedList extends ArrayList
16 {
17 /***
18 * Returns the given object from the list. The list is looked up using the
19 * {@link Collections#binarySearch(List, Object)} method. If the list
20 * contains an object which returns 0 in the <code>compareTo()</code>
21 * method, then the Object that is already stored in the list is returned.
22 * <p>
23 * Otherwise the parameter </code>o</code> is inserted into the list so that
24 * the order of the list remains intact. Then the same object will be
25 * returned.
26 *
27 * @param o The object that is used to look up the list. <b>Note</b> that
28 * the object o and all the objects in the list must implement the
29 * {@link Comparable} interface.
30 + @return either the parameter, if it doesn't already exist in the list or
31 * the instance from the list.
32 * @see Comparable, Collections#binarySearch(List, Object)
33 */
34 public Object get(Object o)
35 {
36 final int index = Collections.binarySearch(this, o);
37
38 return insertOrReturn(index, o);
39 }
40
41 public Object get(Object o, Comparator c)
42 {
43 final int index = Collections.binarySearch(this, o, c);
44
45 return insertOrReturn(index, o);
46 }
47
48 private Object insertOrReturn(int index, Object o)
49 {
50 if (index >= 0)
51 {
52 return get(index);
53 }
54 else
55 {
56 // NOTE: The insertion point is calculated using the formula given
57 // in the JavaDoc of the binarySearch algorithm:
58 // index = (-(insertion point) - 1);
59 final int insertionPoint = -(index + 1);
60
61 add(insertionPoint, o);
62
63 return o;
64 }
65 }
66 }
67
68 // ------1---------2---------3---------4---------5---------6---------7---------8
This page was automatically generated by Maven