KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > IntSet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util.collection;
25
26 public class IntSet {
27     int[] members = new int[6];
28     int size = 0;
29         
30     public IntSet() {
31     }
32         
33     public boolean add(int value) {
34         for (int i=0; i<size; i++) {
35             if (members[i] == value) {
36                 return false;
37             }
38         }
39             
40         if (size == members.length) {
41             int[] temp = new int[size + 4];
42             System.arraycopy(members, 0, temp, 0, size);
43             members = temp;
44         }
45         members[size++] = value;
46         return true;
47     }
48     
49     public boolean remove(int value) {
50         for (int i=0; i<size; i++) {
51             if (members[i] == value) {
52                 for (int j=i+1; j<size; j++) {
53                     members[j-1] = members[j];
54                 }
55                 return true;
56             }
57         }
58         return false;
59     }
60             
61     public boolean contains(int value) {
62         for (int i=0; i<size; i++) {
63             if (members[i] == value) {
64                 return true;
65             }
66         }
67         return false;
68     }
69             
70     
71         
72     public int size() {
73         return size;
74     }
75         
76     public int[] getMembers() {
77         return members;
78     }
79         
80 }
81     
82
Popular Tags