]> code.citadel.org Git - citadel.git/blob - shaggy/SortedVector.java
HTML/CSS calendar mimepart display
[citadel.git] / shaggy / SortedVector.java
1 /* SortedVector.java
2  * insert-sort vector (default to strings)
3  */
4
5 import java.awt.*;
6 import java.util.*;
7
8 public class SortedVector {
9   Vector                theList;
10   sorter                cmpr;
11         
12   public SortedVector() {
13     theList = new Vector();
14     cmpr = new sorter();
15   }
16
17   public SortedVector( sorter s ) {
18     theList = new Vector();
19     cmpr = s;
20   }
21         
22   public int addElement( Object theId ) {
23     int         i, cmp;
24     for( i = 0; i < theList.size(); i++ ) {
25
26       cmp = cmpr.cmp( theList.elementAt( i ), theId );
27
28       if( cmp == 0 ) return -1;
29       if( cmp > 0 )
30         break;
31     }
32                 
33     theList.insertElementAt( theId, i );
34     return i;
35   }
36
37   public Object firstElement() {
38     return theList.firstElement();
39   }
40         
41   public Object removeItem( int index ) {
42     if( (index < 0) || (index > theList.size()) )
43       return null;
44                 
45     Object      theItem = theList.elementAt( index );
46     theList.removeElementAt( index );
47     return theItem;
48   }
49         
50   public int removeElement( Object theItem ) {
51     for( int i = 0; i < theList.size(); i++ ) {
52       boolean equal;
53       equal = cmpr.cmp( theList.elementAt( i ), theItem ) == 0;
54       if( equal ) {
55         theList.removeElementAt( i );
56         return i;
57       }
58     }
59     return -1;
60   }
61         
62   public boolean isElement( Object theItem ) {
63     for( int i = 0; i < theList.size(); i++ ) {
64       if( cmpr.cmp( theList.elementAt( i ), theItem ) == 0 )
65         return true;
66     }
67     return false;
68   }
69         
70   public void removeAllElements() {
71     theList.removeAllElements();
72   }
73
74   public Enumeration elements() {
75     return theList.elements();
76   }
77
78   public int size() {
79     return theList.size();
80   }
81 }
82