Source Code Cross Referenced for AbstractCompositeDocumentIterator.java in  » Search-Engine » mg4j » it » unimi » dsi » mg4j » search » 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 » Search Engine » mg4j » it.unimi.dsi.mg4j.search 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package it.unimi.dsi.mg4j.search;
002:
003:        /*		 
004:         * MG4J: Managing Gigabytes for Java
005:         *
006:         * Copyright (C) 2006-2007 Sebastiano Vigna 
007:         *
008:         *  This library is free software; you can redistribute it and/or modify it
009:         *  under the terms of the GNU Lesser General Public License as published by the Free
010:         *  Software Foundation; either version 2.1 of the License, or (at your option)
011:         *  any later version.
012:         *
013:         *  This library is distributed in the hope that it will be useful, but
014:         *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015:         *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
016:         *  for more details.
017:         *
018:         *  You should have received a copy of the GNU Lesser General Public License
019:         *  along with this program; if not, write to the Free Software
020:         *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021:         *
022:         */
023:
024:        import it.unimi.dsi.fastutil.ints.IntArrayList;
025:        import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
026:        import it.unimi.dsi.fastutil.objects.ReferenceSet;
027:        import it.unimi.dsi.lang.MutableString;
028:        import it.unimi.dsi.mg4j.index.Index;
029:        import it.unimi.dsi.mg4j.index.IndexIterator;
030:        import it.unimi.dsi.mg4j.search.visitor.DocumentIteratorVisitor;
031:        import it.unimi.dsi.util.Interval;
032:
033:        import java.io.IOException;
034:
035:        /** An abstract iterator on documents, based on a list of component iterators.
036:         * 
037:         * <p>The {@linkplain #AbstractCompositeDocumentIterator(DocumentIterator[]) constructor} caches
038:         * into {@link #documentIterator} the component iterators, and sets up a number of protected
039:         * fields that can be useful to implementors. It also provide abstract member classes that make it
040:         * easier to implement interval iterators.
041:         * 
042:         * <p>Note that this class implementa both {@link #accept(DocumentIteratorVisitor)}
043:         * and {@link #acceptOnTruePaths(DocumentIteratorVisitor)} with a series of recursive
044:         * calls on <em>all</em> component iterator. If you desire a different behaviour
045:         * for {@link #acceptOnTruePaths(DocumentIteratorVisitor)} (see, e.g.,
046:         * {@link it.unimi.dsi.mg4j.search.OrDocumentIterator}, please override it. 
047:         */
048:
049:        public abstract class AbstractCompositeDocumentIterator extends
050:                AbstractDocumentIterator implements  DocumentIterator {
051:            /** The component document iterators. */
052:            protected final DocumentIterator[] documentIterator;
053:            /** The number of component iterators. */
054:            protected final int n;
055:            /** A cached copy of {@link #documentIterator}, if all
056:             * underlying iterators are {@linkplain IndexIterator index iterators}; <code>null</code>, otherwise. */
057:            protected final IndexIterator[] indexIterator;
058:            /** The set of indices involved in this iterator. */
059:            protected final ReferenceArraySet<Index> indices = new ReferenceArraySet<Index>();
060:            /** If not <code>null</code>, the sole index involved in this iterator. */
061:            protected final Index soleIndex;
062:
063:            /** Creates a new composite document iterator using a given list of component document iterators.
064:             * 
065:             *  @param documentIterator the component iterators.
066:             */
067:            public AbstractCompositeDocumentIterator(
068:                    final DocumentIterator... documentIterator) {
069:                this .documentIterator = documentIterator;
070:                this .n = documentIterator.length;
071:
072:                /* Now, for each index involved we build a corresponding interval iterator.
073:                 * Note that the set indices() may contain indices from empty subqueries, too. */
074:                for (int i = n; i-- != 0;)
075:                    indices.addAll(documentIterator[i].indices());
076:                soleIndex = indices.size() == 1 ? indices.iterator().next()
077:                        : null;
078:
079:                int i = n;
080:                while (i-- != 0)
081:                    if (!(documentIterator[i] instanceof  IndexIterator))
082:                        break;
083:                if (i == -1) {
084:                    indexIterator = new IndexIterator[n];
085:                    System.arraycopy(documentIterator, 0, indexIterator, 0, n);
086:                } else
087:                    indexIterator = null;
088:            }
089:
090:            public boolean accept(final DocumentIteratorVisitor visitor)
091:                    throws IOException {
092:                if (!visitor.visitPre(this ))
093:                    return false;
094:                for (int i = 0; i < n; i++)
095:                    if (documentIterator[i] != null
096:                            && !documentIterator[i].accept(visitor))
097:                        return false;
098:                return visitor.visitPost(this );
099:            }
100:
101:            public boolean acceptOnTruePaths(
102:                    final DocumentIteratorVisitor visitor) throws IOException {
103:                if (!visitor.visitPre(this ))
104:                    return false;
105:                for (int i = 0; i < n; i++)
106:                    if (documentIterator[i] != null
107:                            && !documentIterator[i].acceptOnTruePaths(visitor))
108:                        return false;
109:                return visitor.visitPost(this );
110:            }
111:
112:            public ReferenceSet<Index> indices() {
113:                return indices;
114:            }
115:
116:            public IntervalIterator intervalIterator() throws IOException {
117:                if (soleIndex == null)
118:                    throw new IllegalStateException();
119:                return intervalIterator(soleIndex);
120:            }
121:
122:            public void dispose() throws IOException {
123:                for (int i = n; i-- != 0;)
124:                    documentIterator[i].dispose();
125:            }
126:
127:            public String toString() {
128:                StringBuilder res = new StringBuilder();
129:                res.append(this .getClass().getSimpleName()).append("(");
130:                for (int i = 0; i < n; i++)
131:                    res.append(i > 0 ? "," : "").append(documentIterator[i]);
132:                return res.append(")").toString();
133:            }
134:
135:            /** An abstract interval iterator. Provide mainly storage for the {@linkplain #intervalIterator component interval iterators},
136:             *  place for {@linkplain #curr the last interval returned by each iterator} and  {@link #toString()}. */
137:
138:            protected abstract static class AbstractCompositeIntervalIterator
139:                    extends AbstractIntervalIterator {
140:                /** The underlying iterators. */
141:                protected IntervalIterator[] intervalIterator;
142:                /** The last interval returned by each iterator. */
143:                protected Interval[] curr;
144:
145:                public AbstractCompositeIntervalIterator(final int n) {
146:                    // We just set up some internal data, but we perform no initialisation.
147:                    curr = new Interval[n];
148:                    intervalIterator = new IntervalIterator[n];
149:                }
150:
151:                public String toString() {
152:                    MutableString res = new MutableString();
153:                    res.append(this .getClass().getName()).append("(").delete(0,
154:                            res.lastIndexOf('.') + 1);
155:                    for (int i = 0; i < intervalIterator.length; i++)
156:                        res.append(i > 0 ? "," : "")
157:                                .append(intervalIterator[i]);
158:                    return res.append(")").toString();
159:                }
160:            }
161:
162:            /** An abstract {@link IndexIterator}-based interval iterator. The difference with {@link AbstractCompositeIntervalIterator}
163:             * is that this class assumes that all document iterators are actually index iterators.
164:             * The algorithms in this (very common) case can be significantly simplified, obtaining
165:             * a large gain in performance. */
166:
167:            protected abstract static class AbstractCompositeIndexIntervalIterator
168:                    extends AbstractIntervalIterator {
169:                /** The position arrays returned by each index iterator. */
170:                protected int[][] position;
171:                /** The index of current element of {@link #position} for each index iterator. */
172:                protected int[] currPos;
173:                /** At any time, <code>curr[ i ]</code> contains <code>position[ i ][ currPos[i ] ]</code>. */
174:                protected int[] curr;
175:                /** The number of elements of {@link #position} for each index iterator. */
176:                protected int[] count;
177:
178:                public AbstractCompositeIndexIntervalIterator(final int n) {
179:                    // We just set up some internal data, but we perform no initialisation.
180:                    position = new int[n][];
181:                    count = new int[n];
182:                    currPos = new int[n];
183:                    curr = new int[n];
184:                }
185:
186:                public String toString() {
187:                    MutableString res = new MutableString();
188:                    res.append(this .getClass().getName()).append("(").delete(0,
189:                            res.lastIndexOf('.') + 1);
190:                    for (int i = 0; i < position.length; i++)
191:                        res.append(i > 0 ? "," : "").append(
192:                                IntArrayList.wrap(position[i], count[i]));
193:                    return res.append(")").toString();
194:                }
195:            }
196:
197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.