View Javadoc

1   /*
2    *  $Id: SortedList.java,v 1.2 2004/07/25 14:12:19 roku Exp $ 
3    *
4    *  Copyright 2004 University of Hannover
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package olr.rdf.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  public abstract class SortedList extends ArrayList
26  {
27  
28      protected abstract boolean isValidItem(Object obj);
29  
30      protected abstract int compare(Object obj, Object obj1);
31  
32      public SortedList()
33      {
34      }
35  
36      public SortedList(Collection coll)
37      {
38          this();
39          for(Iterator it = coll.iterator(); it.hasNext(); add(it.next()));
40      }
41  
42      public boolean add(Object o)
43      {
44          if(isValidItem(o))
45          {
46              for(int i = 0; i < size(); i++)
47                  if(compare(get(i), o) > 0)
48                  {
49                      super.add(i, o);
50                      return true;
51                  }
52  
53              return super.add(o);
54          } else
55          {
56              return false;
57          }
58      }
59  
60      public void add(int pos, Object o)
61      {
62          if(isValidItem(o))
63              add(o);
64      }
65  
66      public boolean addAll(Collection collection)
67      {
68          for(Iterator it = collection.iterator(); it.hasNext(); add(it.next()));
69          return true;
70      }
71  
72      public boolean addAll(int pos, Collection collection)
73      {
74          return addAll(collection);
75      }
76  }