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


001:        /*
002:         *  Copyright 1999-2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.iterators;
017:
018:        import java.util.ListIterator;
019:        import java.util.NoSuchElementException;
020:
021:        import org.apache.commons.collections.ResettableListIterator;
022:
023:        /**
024:         * <code>SingletonIterator</code> is an {@link ListIterator} over a single 
025:         * object instance.
026:         *
027:         * @since Commons Collections 2.1
028:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
029:         * 
030:         * @author Stephen Colebourne
031:         * @author Rodney Waldhoff
032:         */
033:        public class SingletonListIterator implements  ListIterator,
034:                ResettableListIterator {
035:
036:            private boolean beforeFirst = true;
037:            private boolean nextCalled = false;
038:            private boolean removed = false;
039:            private Object object;
040:
041:            /**
042:             * Constructs a new <code>SingletonListIterator</code>.
043:             *
044:             * @param object  the single object to return from the iterator
045:             */
046:            public SingletonListIterator(Object object) {
047:                super ();
048:                this .object = object;
049:            }
050:
051:            /**
052:             * Is another object available from the iterator?
053:             * <p>
054:             * This returns true if the single object hasn't been returned yet.
055:             * 
056:             * @return true if the single object hasn't been returned yet
057:             */
058:            public boolean hasNext() {
059:                return beforeFirst && !removed;
060:            }
061:
062:            /**
063:             * Is a previous object available from the iterator?
064:             * <p>
065:             * This returns true if the single object has been returned.
066:             * 
067:             * @return true if the single object has been returned
068:             */
069:            public boolean hasPrevious() {
070:                return !beforeFirst && !removed;
071:            }
072:
073:            /**
074:             * Returns the index of the element that would be returned by a subsequent
075:             * call to <tt>next</tt>.
076:             *
077:             * @return 0 or 1 depending on current state. 
078:             */
079:            public int nextIndex() {
080:                return (beforeFirst ? 0 : 1);
081:            }
082:
083:            /**
084:             * Returns the index of the element that would be returned by a subsequent
085:             * call to <tt>previous</tt>. A return value of -1 indicates that the iterator is currently at
086:             * the start.
087:             *
088:             * @return 0 or -1 depending on current state. 
089:             */
090:            public int previousIndex() {
091:                return (beforeFirst ? -1 : 0);
092:            }
093:
094:            /**
095:             * Get the next object from the iterator.
096:             * <p>
097:             * This returns the single object if it hasn't been returned yet.
098:             *
099:             * @return the single object
100:             * @throws NoSuchElementException if the single object has already 
101:             *    been returned
102:             */
103:            public Object next() {
104:                if (!beforeFirst || removed) {
105:                    throw new NoSuchElementException();
106:                }
107:                beforeFirst = false;
108:                nextCalled = true;
109:                return object;
110:            }
111:
112:            /**
113:             * Get the previous object from the iterator.
114:             * <p>
115:             * This returns the single object if it has been returned.
116:             *
117:             * @return the single object
118:             * @throws NoSuchElementException if the single object has not already 
119:             *    been returned
120:             */
121:            public Object previous() {
122:                if (beforeFirst || removed) {
123:                    throw new NoSuchElementException();
124:                }
125:                beforeFirst = true;
126:                return object;
127:            }
128:
129:            /**
130:             * Remove the object from this iterator.
131:             * @throws IllegalStateException if the <tt>next</tt> or <tt>previous</tt> 
132:             *        method has not yet been called, or the <tt>remove</tt> method 
133:             *        has already been called after the last call to <tt>next</tt>
134:             *        or <tt>previous</tt>.
135:             */
136:            public void remove() {
137:                if (!nextCalled || removed) {
138:                    throw new IllegalStateException();
139:                } else {
140:                    object = null;
141:                    removed = true;
142:                }
143:            }
144:
145:            /**
146:             * Add always throws {@link UnsupportedOperationException}.
147:             *
148:             * @throws UnsupportedOperationException always
149:             */
150:            public void add(Object obj) {
151:                throw new UnsupportedOperationException(
152:                        "add() is not supported by this iterator");
153:            }
154:
155:            /**
156:             * Set sets the value of the singleton.
157:             *
158:             * @param obj  the object to set
159:             * @throws IllegalStateException if <tt>next</tt> has not been called 
160:             *          or the object has been removed
161:             */
162:            public void set(Object obj) {
163:                if (!nextCalled || removed) {
164:                    throw new IllegalStateException();
165:                }
166:                this .object = obj;
167:            }
168:
169:            /**
170:             * Reset the iterator back to the start.
171:             */
172:            public void reset() {
173:                beforeFirst = true;
174:                nextCalled = false;
175:            }
176:
177:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.