Source Code Cross Referenced for RandomAccessByteList.java in  » Library » Apache-commons-primitives-1.0 » org » apache » commons » collections » primitives » 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 » Library » Apache commons primitives 1.0 » org.apache.commons.collections.primitives 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/RandomAccessByteList.java,v 1.3 2003/10/16 20:49:35 scolebourne Exp $
003:         * ====================================================================
004:         * The Apache Software License, Version 1.1
005:         *
006:         * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
007:         * reserved.
008:         *
009:         * Redistribution and use in source and binary forms, with or without
010:         * modification, are permitted provided that the following conditions
011:         * are met:
012:         *
013:         * 1. Redistributions of source code must retain the above copyright
014:         *    notice, this list of conditions and the following disclaimer.
015:         *
016:         * 2. Redistributions in binary form must reproduce the above copyright
017:         *    notice, this list of conditions and the following disclaimer in
018:         *    the documentation and/or other materials provided with the
019:         *    distribution.
020:         *
021:         * 3. The end-user documentation included with the redistribution, if
022:         *    any, must include the following acknowledgement:
023:         *       "This product includes software developed by the
024:         *        Apache Software Foundation (http://www.apache.org/)."
025:         *    Alternately, this acknowledgement may appear in the software itself,
026:         *    if and wherever such third-party acknowledgements normally appear.
027:         *
028:         * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029:         *    Foundation" must not be used to endorse or promote products derived
030:         *    from this software without prior written permission. For written
031:         *    permission, please contact apache@apache.org.
032:         *
033:         * 5. Products derived from this software may not be called "Apache"
034:         *    nor may "Apache" appear in their names without prior written
035:         *    permission of the Apache Software Foundation.
036:         *
037:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048:         * SUCH DAMAGE.
049:         * ====================================================================
050:         *
051:         * This software consists of voluntary contributions made by many
052:         * individuals on behalf of the Apache Software Foundation.  For more
053:         * information on the Apache Software Foundation, please see
054:         * <http://www.apache.org/>.
055:         *
056:         */
057:
058:        package org.apache.commons.collections.primitives;
059:
060:        import java.util.ConcurrentModificationException;
061:        import java.util.NoSuchElementException;
062:
063:        /**
064:         * Abstract base class for {@link ByteList}s backed 
065:         * by random access structures like arrays.
066:         * <p />
067:         * Read-only subclasses must override {@link #get}
068:         * and {@link #size}.  Mutable subclasses
069:         * should also override {@link #set}.  Variably-sized
070:         * subclasses should also override {@link #add} 
071:         * and {@link #removeElementAt}.  All other methods
072:         * have at least some base implementation derived from 
073:         * these.  Subclasses may choose to override these methods
074:         * to provide a more efficient implementation.
075:         * 
076:         * @since Commons Primitives 1.0
077:         * @version $Revision: 1.3 $ $Date: 2003/10/16 20:49:35 $
078:         * 
079:         * @author Rodney Waldhoff 
080:         */
081:        public abstract class RandomAccessByteList extends
082:                AbstractByteCollection implements  ByteList {
083:
084:            // constructors
085:            //-------------------------------------------------------------------------
086:
087:            /** Constructs an empty list. */
088:            protected RandomAccessByteList() {
089:            }
090:
091:            // fully abstract methods
092:            //-------------------------------------------------------------------------
093:
094:            public abstract byte get(int index);
095:
096:            public abstract int size();
097:
098:            // unsupported in base
099:            //-------------------------------------------------------------------------
100:
101:            /** 
102:             * Unsupported in this implementation. 
103:             * @throws UnsupportedOperationException since this method is not supported
104:             */
105:            public byte removeElementAt(int index) {
106:                throw new UnsupportedOperationException();
107:            }
108:
109:            /** 
110:             * Unsupported in this implementation. 
111:             * @throws UnsupportedOperationException since this method is not supported
112:             */
113:            public byte set(int index, byte element) {
114:                throw new UnsupportedOperationException();
115:            }
116:
117:            /** 
118:             * Unsupported in this implementation. 
119:             * @throws UnsupportedOperationException since this method is not supported
120:             */
121:            public void add(int index, byte element) {
122:                throw new UnsupportedOperationException();
123:            }
124:
125:            //-------------------------------------------------------------------------
126:
127:            // javadocs here are inherited
128:
129:            public boolean add(byte element) {
130:                add(size(), element);
131:                return true;
132:            }
133:
134:            public boolean addAll(int index, ByteCollection collection) {
135:                boolean modified = false;
136:                for (ByteIterator iter = collection.iterator(); iter.hasNext();) {
137:                    add(index++, iter.next());
138:                    modified = true;
139:                }
140:                return modified;
141:            }
142:
143:            public int indexOf(byte element) {
144:                int i = 0;
145:                for (ByteIterator iter = iterator(); iter.hasNext();) {
146:                    if (iter.next() == element) {
147:                        return i;
148:                    } else {
149:                        i++;
150:                    }
151:                }
152:                return -1;
153:            }
154:
155:            public int lastIndexOf(byte element) {
156:                for (ByteListIterator iter = listIterator(size()); iter
157:                        .hasPrevious();) {
158:                    if (iter.previous() == element) {
159:                        return iter.nextIndex();
160:                    }
161:                }
162:                return -1;
163:            }
164:
165:            public ByteIterator iterator() {
166:                return listIterator();
167:            }
168:
169:            public ByteListIterator listIterator() {
170:                return listIterator(0);
171:            }
172:
173:            public ByteListIterator listIterator(int index) {
174:                return new RandomAccessByteListIterator(this , index);
175:            }
176:
177:            public ByteList subList(int fromIndex, int toIndex) {
178:                return new RandomAccessByteSubList(this , fromIndex, toIndex);
179:            }
180:
181:            public boolean equals(Object that) {
182:                if (this  == that) {
183:                    return true;
184:                } else if (that instanceof  ByteList) {
185:                    ByteList thatList = (ByteList) that;
186:                    if (size() != thatList.size()) {
187:                        return false;
188:                    }
189:                    for (ByteIterator thatIter = thatList.iterator(), this Iter = iterator(); this Iter
190:                            .hasNext();) {
191:                        if (this Iter.next() != thatIter.next()) {
192:                            return false;
193:                        }
194:                    }
195:                    return true;
196:                } else {
197:                    return false;
198:                }
199:            }
200:
201:            public int hashCode() {
202:                int hash = 1;
203:                for (ByteIterator iter = iterator(); iter.hasNext();) {
204:                    hash = 31 * hash + ((int) (iter.next()));
205:                }
206:                return hash;
207:            }
208:
209:            public String toString() {
210:                StringBuffer buf = new StringBuffer();
211:                buf.append("[");
212:                for (ByteIterator iter = iterator(); iter.hasNext();) {
213:                    buf.append(iter.next());
214:                    if (iter.hasNext()) {
215:                        buf.append(", ");
216:                    }
217:                }
218:                buf.append("]");
219:                return buf.toString();
220:            }
221:
222:            // protected utilities
223:            //-------------------------------------------------------------------------
224:
225:            /** Get my count of structural modifications. */
226:            protected int getModCount() {
227:                return _modCount;
228:            }
229:
230:            /** Increment my count of structural modifications. */
231:            protected void incrModCount() {
232:                _modCount++;
233:            }
234:
235:            // attributes
236:            //-------------------------------------------------------------------------
237:
238:            private int _modCount = 0;
239:
240:            // inner classes
241:            //-------------------------------------------------------------------------
242:
243:            private static class ComodChecker {
244:                ComodChecker(RandomAccessByteList source) {
245:                    _source = source;
246:                    resyncModCount();
247:                }
248:
249:                protected RandomAccessByteList getList() {
250:                    return _source;
251:                }
252:
253:                protected void assertNotComodified()
254:                        throws ConcurrentModificationException {
255:                    if (_expectedModCount != getList().getModCount()) {
256:                        throw new ConcurrentModificationException();
257:                    }
258:                }
259:
260:                protected void resyncModCount() {
261:                    _expectedModCount = getList().getModCount();
262:                }
263:
264:                private RandomAccessByteList _source = null;
265:                private int _expectedModCount = -1;
266:            }
267:
268:            protected static class RandomAccessByteListIterator extends
269:                    ComodChecker implements  ByteListIterator {
270:                RandomAccessByteListIterator(RandomAccessByteList list,
271:                        int index) {
272:                    super (list);
273:                    if (index < 0 || index > getList().size()) {
274:                        throw new IndexOutOfBoundsException("Index " + index
275:                                + " not in [0," + getList().size() + ")");
276:                    } else {
277:                        _nextIndex = index;
278:                        resyncModCount();
279:                    }
280:                }
281:
282:                public boolean hasNext() {
283:                    assertNotComodified();
284:                    return _nextIndex < getList().size();
285:                }
286:
287:                public boolean hasPrevious() {
288:                    assertNotComodified();
289:                    return _nextIndex > 0;
290:                }
291:
292:                public int nextIndex() {
293:                    assertNotComodified();
294:                    return _nextIndex;
295:                }
296:
297:                public int previousIndex() {
298:                    assertNotComodified();
299:                    return _nextIndex - 1;
300:                }
301:
302:                public byte next() {
303:                    assertNotComodified();
304:                    if (!hasNext()) {
305:                        throw new NoSuchElementException();
306:                    } else {
307:                        byte val = getList().get(_nextIndex);
308:                        _lastReturnedIndex = _nextIndex;
309:                        _nextIndex++;
310:                        return val;
311:                    }
312:                }
313:
314:                public byte previous() {
315:                    assertNotComodified();
316:                    if (!hasPrevious()) {
317:                        throw new NoSuchElementException();
318:                    } else {
319:                        byte val = getList().get(_nextIndex - 1);
320:                        _lastReturnedIndex = _nextIndex - 1;
321:                        _nextIndex--;
322:                        return val;
323:                    }
324:                }
325:
326:                public void add(byte value) {
327:                    assertNotComodified();
328:                    getList().add(_nextIndex, value);
329:                    _nextIndex++;
330:                    _lastReturnedIndex = -1;
331:                    resyncModCount();
332:                }
333:
334:                public void remove() {
335:                    assertNotComodified();
336:                    if (-1 == _lastReturnedIndex) {
337:                        throw new IllegalStateException();
338:                    } else {
339:                        getList().removeElementAt(_lastReturnedIndex);
340:                        _lastReturnedIndex = -1;
341:                        _nextIndex--;
342:                        resyncModCount();
343:                    }
344:                }
345:
346:                public void set(byte value) {
347:                    assertNotComodified();
348:                    if (-1 == _lastReturnedIndex) {
349:                        throw new IllegalStateException();
350:                    } else {
351:                        getList().set(_lastReturnedIndex, value);
352:                        resyncModCount();
353:                    }
354:                }
355:
356:                private int _nextIndex = 0;
357:                private int _lastReturnedIndex = -1;
358:            }
359:
360:            protected static class RandomAccessByteSubList extends
361:                    RandomAccessByteList implements  ByteList {
362:                RandomAccessByteSubList(RandomAccessByteList list,
363:                        int fromIndex, int toIndex) {
364:                    if (fromIndex < 0 || toIndex > list.size()) {
365:                        throw new IndexOutOfBoundsException();
366:                    } else if (fromIndex > toIndex) {
367:                        throw new IllegalArgumentException();
368:                    } else {
369:                        _list = list;
370:                        _offset = fromIndex;
371:                        _limit = toIndex - fromIndex;
372:                        _comod = new ComodChecker(list);
373:                        _comod.resyncModCount();
374:                    }
375:                }
376:
377:                public byte get(int index) {
378:                    checkRange(index);
379:                    _comod.assertNotComodified();
380:                    return _list.get(toUnderlyingIndex(index));
381:                }
382:
383:                public byte removeElementAt(int index) {
384:                    checkRange(index);
385:                    _comod.assertNotComodified();
386:                    byte val = _list.removeElementAt(toUnderlyingIndex(index));
387:                    _limit--;
388:                    _comod.resyncModCount();
389:                    incrModCount();
390:                    return val;
391:                }
392:
393:                public byte set(int index, byte element) {
394:                    checkRange(index);
395:                    _comod.assertNotComodified();
396:                    byte val = _list.set(toUnderlyingIndex(index), element);
397:                    incrModCount();
398:                    _comod.resyncModCount();
399:                    return val;
400:                }
401:
402:                public void add(int index, byte element) {
403:                    checkRangeIncludingEndpoint(index);
404:                    _comod.assertNotComodified();
405:                    _list.add(toUnderlyingIndex(index), element);
406:                    _limit++;
407:                    _comod.resyncModCount();
408:                    incrModCount();
409:                }
410:
411:                public int size() {
412:                    _comod.assertNotComodified();
413:                    return _limit;
414:                }
415:
416:                private void checkRange(int index) {
417:                    if (index < 0 || index >= size()) {
418:                        throw new IndexOutOfBoundsException("index " + index
419:                                + " not in [0," + size() + ")");
420:                    }
421:                }
422:
423:                private void checkRangeIncludingEndpoint(int index) {
424:                    if (index < 0 || index > size()) {
425:                        throw new IndexOutOfBoundsException("index " + index
426:                                + " not in [0," + size() + "]");
427:                    }
428:                }
429:
430:                private int toUnderlyingIndex(int index) {
431:                    return (index + _offset);
432:                }
433:
434:                private int _offset = 0;
435:                private int _limit = 0;
436:                private RandomAccessByteList _list = null;
437:                private ComodChecker _comod = null;
438:
439:            }
440:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.