Source Code Cross Referenced for Pair.java in  » Parser » Rats-Parser-Generators » xtc » util » 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 » Rats Parser Generators » xtc.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * xtc - The eXTensible Compiler
003:         * Copyright (C) 2004 Robert Grimm
004:         *
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         */
019:        package xtc.util;
020:
021:        import java.util.ArrayList;
022:        import java.util.Iterator;
023:        import java.util.List;
024:        import java.util.NoSuchElementException;
025:
026:        /**
027:         * Implementation of a pair.  Pairs are used to construct immutable
028:         * singly-linked lists, not unlike cons cells in Scheme (with the
029:         * differences that pairs are immutable and that the second pointer
030:         * always references another pair).
031:         *
032:         * @author Robert Grimm
033:         * @version $Revision: 1.1 $
034:         */
035:        public class Pair {
036:
037:            /**
038:             * The pair representing the empty list.  This object serves as a
039:             * sentinel to avoid special-casing <code>null</code>.
040:             */
041:            public static final Pair EMPTY = new Pair();
042:
043:            /** The value. */
044:            private final Object value;
045:
046:            /** The next pair. */
047:            private final Pair next;
048:
049:            /** Create a new pair. */
050:            private Pair() {
051:                value = null;
052:                next = null;
053:            }
054:
055:            /**
056:             * Create a new pair.  The newly created pair represents a singleton
057:             * list.
058:             *
059:             * @param value The value.
060:             */
061:            public Pair(Object value) {
062:                this (value, EMPTY);
063:            }
064:
065:            /**
066:             * Create a new pair.
067:             *
068:             * @param value The value.
069:             * @param next  The next pair.
070:             * @throws NullPointerException
071:             *    Signals that <code>next</code> is <code>null</code>.
072:             */
073:            public Pair(Object value, Pair next) {
074:                if (null == next) {
075:                    throw new NullPointerException();
076:                }
077:
078:                this .value = value;
079:                this .next = next;
080:            }
081:
082:            /**
083:             * Determine whether the list starting at this pair is empty.
084:             *
085:             * @return <code>true</code> if the list is empty.
086:             */
087:            public boolean isEmpty() {
088:                return (this  == EMPTY);
089:            }
090:
091:            /**
092:             * Get this pair's value.
093:             *
094:             * @return This pair's value.
095:             * @throws IllegalStateException
096:             *    Signals that this pair represents the empty list.
097:             */
098:            public Object value() {
099:                if (this  == EMPTY) {
100:                    throw new IllegalStateException();
101:                }
102:
103:                return value;
104:            }
105:
106:            /**
107:             * Get the next pair.
108:             *
109:             * @return The next pair.
110:             * @throws IllegalStateException
111:             *    Signals that this pair represents the empty list.
112:             */
113:            public Pair next() {
114:                if (this  == EMPTY) {
115:                    throw new IllegalStateException();
116:                }
117:
118:                return next;
119:            }
120:
121:            /**
122:             * Get the size of the list starting at this pair.
123:             *
124:             * @return The size.
125:             */
126:            public int size() {
127:                Pair pair = this ;
128:                int size = 0;
129:
130:                while (pair != EMPTY) {
131:                    size++;
132:                    pair = pair.next;
133:                }
134:
135:                return size;
136:            }
137:
138:            /**
139:             * Get an iterator over the values of the list starting at this
140:             * pair.
141:             *
142:             * @return The iterator.
143:             */
144:            public Iterator iterator() {
145:                return new Iterator() {
146:                    private Pair pair = Pair.this ;
147:
148:                    public boolean hasNext() {
149:                        return (EMPTY != pair);
150:                    }
151:
152:                    public Object next() {
153:                        if (EMPTY == pair) {
154:                            throw new NoSuchElementException();
155:                        } else {
156:                            Object v = pair.value;
157:                            pair = pair.next;
158:                            return v;
159:                        }
160:                    }
161:
162:                    public void remove() {
163:                        throw new UnsupportedOperationException();
164:                    }
165:                };
166:            }
167:
168:            /**
169:             * Get a Java Collections list with the values of the list starting
170:             * at this pair.
171:             *
172:             * @return The Java Collections list.
173:             */
174:            public List list() {
175:                ArrayList list = new ArrayList(size());
176:                Pair pair = this;
177:
178:                while (EMPTY != pair) {
179:                    list.add(pair.value);
180:                    pair = pair.next;
181:                }
182:
183:                return list;
184:            }
185:
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.