Source Code Cross Referenced for IntegerSet.java in  » Parser » chaperon-3.0 » net » sourceforge » chaperon » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » chaperon 3.0 » net.sourceforge.chaperon.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright (C) Chaperon. All rights reserved.
003:         *  -------------------------------------------------------------------------
004:         *  This software is published under the terms of the Apache Software License
005:         *  version 1.1, a copy of which has been included  with this distribution in
006:         *  the LICENSE file.
007:         */
008:
009:        package net.sourceforge.chaperon.common;
010:
011:        /**
012:         * This class represents a set of integer values, which means there doesn't exist multiple
013:         * instances of values.
014:         *
015:         * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
016:         * @version CVS $Id: IntegerSet.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
017:         */
018:        public class IntegerSet implements  IntegerCollection {
019:            private int capacityIncrement = 100;
020:            private int elementCount = 0;
021:            private int[] list = new int[10];
022:            private int dummy;
023:
024:            /**
025:             * Creates an empty set of integer values.
026:             */
027:            public IntegerSet() {
028:            }
029:
030:            /**
031:             * Add a value to this set.
032:             *
033:             * @param value Integer value.
034:             *
035:             * @return Index of this value.
036:             */
037:            public int add(int value) {
038:                int index = indexOf(value);
039:
040:                if (index == -1) {
041:                    ensureCapacity(elementCount + 1);
042:                    list[elementCount++] = value;
043:                    return elementCount - 1;
044:                }
045:
046:                return index;
047:            }
048:
049:            /**
050:             * Add the values of an other integer collection to this set.
051:             *
052:             * @param collection Collection of values.
053:             */
054:            public void add(IntegerCollection collection) {
055:                for (int i = 0; i < collection.getCount(); i++)
056:                    add(collection.get(i));
057:            }
058:
059:            /**
060:             * Add the values of an array to this set.
061:             *
062:             * @param array Array of integer values.
063:             */
064:            public void add(int[] array) {
065:                for (int i = 0; i < array.length; i++)
066:                    add(array[i]);
067:            }
068:
069:            /**
070:             * Removes a value giving by an index.
071:             *
072:             * @param index Index of the integer value.
073:             */
074:            public void remove(int index) {
075:                if (index >= elementCount)
076:                    throw new ArrayIndexOutOfBoundsException(index);
077:
078:                elementCount--;
079:                if (index < elementCount)
080:                    System.arraycopy(list, index + 1, list, index, elementCount
081:                            - index);
082:
083:                list[elementCount] = 0;
084:            }
085:
086:            /**
087:             * Replace a value given by an index.
088:             *
089:             * @param index Index of the value, which should be replaced.
090:             * @param value The new value.
091:             */
092:            public void set(int index, int value) {
093:                if (index >= elementCount)
094:                    throw new ArrayIndexOutOfBoundsException(index);
095:
096:                if (contains(value) && (!(get(index) == value)))
097:                    throw new IllegalArgumentException(
098:                            "Set already contains the value");
099:
100:                list[index] = value;
101:            }
102:
103:            /**
104:             * Return a value from this set given by an index.
105:             *
106:             * @param index Index of the value.
107:             *
108:             * @return Integer value.
109:             */
110:            public int get(int index) {
111:                if (index >= elementCount)
112:                    throw new ArrayIndexOutOfBoundsException(index);
113:
114:                return list[index];
115:            }
116:
117:            /**
118:             * Returns the count of value in this set.
119:             *
120:             * @return Count of integer values.
121:             */
122:            public int getCount() {
123:                return elementCount;
124:            }
125:
126:            /**
127:             * Return the index of a value, otherwise -1
128:             *
129:             * @param value Value, which should be found in this set.
130:             *
131:             * @return Index of this value.
132:             */
133:            public int indexOf(int value) {
134:                for (int i = 0; i < elementCount; i++) {
135:                    if (list[i] == value)
136:                        return i;
137:                }
138:
139:                return -1;
140:            }
141:
142:            /**
143:             * If the list contains a value.
144:             *
145:             * @param value Value, which should be found in this set
146:             *
147:             * @return True, if this set contains the value.
148:             */
149:            public boolean contains(int value) {
150:                for (int i = 0; i < elementCount; i++) {
151:                    if (list[i] == value)
152:                        return true;
153:                }
154:
155:                return false;
156:            }
157:
158:            /**
159:             * Removes a value from this set.
160:             *
161:             * @param value Value to remove.
162:             */
163:            public void removeValue(int value) {
164:                int index;
165:
166:                while ((index = indexOf(value)) != -1)
167:                    remove(index);
168:            }
169:
170:            /**
171:             * If this set contains no values.
172:             *
173:             * @return True, if this set is empty.
174:             */
175:            public boolean isEmpty() {
176:                return (elementCount <= 0);
177:            }
178:
179:            /**
180:             * Pops a value of the end of this set.
181:             *
182:             * @return Value from the end of this set.
183:             */
184:            public int pop() {
185:                if (0 >= elementCount)
186:                    throw new java.util.EmptyStackException();
187:
188:                return list[--elementCount];
189:            }
190:
191:            /**
192:             * Pushs a value to this list.
193:             *
194:             * @param value Value, which should be added.
195:             */
196:            public void push(int value) {
197:                add(value);
198:            }
199:
200:            /**
201:             * Push values from an array.
202:             *
203:             * @param values Array of integer values.
204:             */
205:            public void push(int[] values) {
206:                add(values);
207:            }
208:
209:            /**
210:             * Peek a value of the end of this set.
211:             *
212:             * @return Integer value.
213:             */
214:            public int peek() {
215:                if (0 >= elementCount)
216:                    throw new java.util.EmptyStackException();
217:
218:                return list[elementCount - 1];
219:            }
220:
221:            /**
222:             * Removes all values from this set
223:             */
224:            public void clear() {
225:                elementCount = 0;
226:            }
227:
228:            /**
229:             * Swaps two values from this set.
230:             *
231:             * @param index1 Index from the first value.
232:             * @param index2 Index from the second value.
233:             */
234:            public void swap(int index1, int index2) {
235:                dummy = list[index1];
236:                list[index1] = list[index2];
237:                list[index2] = dummy;
238:            }
239:
240:            /**
241:             * Return a string representation of the collection.
242:             *
243:             * @return String representation of the collection.
244:             */
245:            public String toString() {
246:                StringBuffer buffer = new StringBuffer();
247:
248:                buffer.append("[");
249:                for (int i = 0; i < elementCount; i++) {
250:                    buffer.append(String.valueOf(list[i]));
251:                    if (i < (elementCount - 1))
252:                        buffer.append(",");
253:                }
254:
255:                buffer.append("]");
256:                return buffer.toString();
257:            }
258:
259:            /**
260:             * Ensure the capacity for adding values.
261:             *
262:             * @param minCapacity Minimum capacity.
263:             */
264:            private void ensureCapacity(int minCapacity) {
265:                if (list.length >= minCapacity)
266:                    return;
267:
268:                int newCapacity = list.length + capacityIncrement;
269:
270:                if (capacityIncrement <= 0)
271:                    newCapacity = list.length * 2;
272:
273:                int[] newArray = new int[Math.max(newCapacity, minCapacity)];
274:
275:                System.arraycopy(list, 0, newArray, 0, list.length);
276:                list = newArray;
277:            }
278:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.