KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > RegistryIndexChildren


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.registry;
12
13 public class RegistryIndexChildren {
14
15     static final int[] EMPTY_ARRAY = new int[0];
16
17     private int[] children;
18
19     public RegistryIndexChildren() {
20         children = EMPTY_ARRAY;
21     }
22
23     public RegistryIndexChildren(int[] children) {
24         this.children = children;
25     }
26
27     public int[] getChildren() {
28         return children;
29     }
30
31     public int findChild(int id) {
32         for (int i = 0; i < children.length; i++) {
33             if (children[i] == id)
34                 return i;
35         }
36         return -1;
37     }
38
39     public boolean unlinkChild(int id) {
40         int index = findChild(id);
41         if (index == -1)
42             return false; // there is no such element
43

44         // copy the array except one element at index
45
int[] result = new int[children.length - 1];
46         System.arraycopy(children, 0, result, 0, index);
47         System.arraycopy(children, index + 1, result, index, children.length - index - 1);
48         children = result;
49         return true;
50     }
51
52     public boolean linkChild(int id) {
53         if (children.length == 0) {
54             children = new int[] {id};
55             return true;
56         }
57
58         // add new element at the end
59
int[] result = new int[children.length + 1];
60         System.arraycopy(children, 0, result, 0, children.length);
61         result[children.length] = id;
62         children = result;
63         return true;
64     }
65
66     public boolean linkChildren(int[] IDs) {
67         if (children.length == 0) {
68             children = IDs;
69             return true;
70         }
71         int[] result = new int[children.length + IDs.length];
72         System.arraycopy(children, 0, result, 0, children.length);
73         System.arraycopy(IDs, 0, result, children.length, IDs.length);
74         children = result;
75         return true;
76     }
77
78     public boolean unlinkChildren(int[] IDs) {
79         if (children.length == 0)
80             return (IDs.length == 0);
81
82         int size = children.length;
83         for (int i = 0; i < IDs.length; i++) {
84             int index = findChild(IDs[i]);
85             if (index != -1) {
86                 children[i] = -1;
87                 size--;
88             }
89         }
90         if (size == 0) {
91             children = EMPTY_ARRAY;
92             return true;
93         }
94         int[] result = new int[size];
95         int pos = 0;
96         for (int i = 0; i < children.length; i++) {
97             if (children[i] == -1)
98                 continue;
99             result[pos] = children[i];
100             pos++;
101         }
102         return true;
103     }
104 }
105
Popular Tags