KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > changeorder > ChangeOrderUtils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.changeorder;
14
15 import java.util.List JavaDoc;
16
17 /**
18  * @author av
19  */

20 public class ChangeOrderUtils {
21
22   /**
23    * validates that oldIndex and newIndex fit into an array of size
24    * @return true when the indexes are valid
25    */

26   static boolean checkBounds(int size, int oldIndex, int newIndex) {
27     if (oldIndex < 0 || oldIndex >= size)
28       return false;
29     if (newIndex < 0 || newIndex >= size)
30       return false;
31     return true;
32   }
33   
34   public static boolean move(List JavaDoc list, int oldIndex, int newIndex) {
35     if (!checkBounds(list.size(), oldIndex, newIndex))
36         return false;
37     Object JavaDoc item = list.remove(oldIndex);
38     list.add(newIndex, item);
39     return true;
40   }
41
42
43   public static boolean move(Object JavaDoc[] array, int oldIndex, int newIndex) {
44     if (!checkBounds(array.length, oldIndex, newIndex))
45       return false;
46     if (oldIndex == newIndex)
47       return true;
48     Object JavaDoc item = array[oldIndex];
49     if (oldIndex < newIndex) {
50       for (int i = oldIndex; i < newIndex; i++)
51         array[i] = array[i+1];
52     }
53     else {
54       for (int i = oldIndex; i > newIndex; i--)
55         array[i] = array[i-1];
56     }
57     array[newIndex] = item;
58     
59     return true;
60   }
61
62   public static int indexOf(Object JavaDoc[] array, Object JavaDoc item) {
63     for (int i = 0; i < array.length; i++)
64       if (item.equals(array[i]))
65         return i;
66     return -1;
67   }
68
69 }
70
Popular Tags