Issue 508
[zxing.git] / core / src / com / google / zxing / common / Collections.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.common;
18
19 import java.util.Vector;
20
21 /**
22  * <p>This is basically a substitute for <code>java.util.Collections</code>, which is not
23  * present in MIDP 2.0 / CLDC 1.1.</p>
24  *
25  * @author Sean Owen
26  */
27 public final class Collections {
28
29   private Collections() {
30   }
31
32   /**
33    * Sorts its argument (destructively) using insert sort; in the context of this package
34    * insertion sort is simple and efficient given its relatively small inputs.
35    *
36    * @param vector vector to sort
37    * @param comparator comparator to define sort ordering
38    */
39   public static void insertionSort(Vector vector, Comparator comparator) {
40     int max = vector.size();
41     for (int i = 1; i < max; i++) {
42       Object value = vector.elementAt(i);
43       int j = i - 1;
44       Object valueB;
45       while (j >= 0 && comparator.compare((valueB = vector.elementAt(j)), value) > 0) {
46         vector.setElementAt(valueB, j + 1);
47         j--;
48       }
49       vector.setElementAt(value, j + 1);
50     }
51   }
52
53 }