Source Code Cross Referenced for FieldDocSortedHitQueue.java in  » Net » lucene-connector » org » apache » lucene » 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 » Net » lucene connector » org.apache.lucene.search 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.search;
002:
003:        /**
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import org.apache.lucene.util.PriorityQueue;
021:
022:        import java.text.Collator;
023:        import java.util.Locale;
024:
025:        /**
026:         * Expert: Collects sorted results from Searchable's and collates them.
027:         * The elements put into this queue must be of type FieldDoc.
028:         *
029:         * <p>Created: Feb 11, 2004 2:04:21 PM
030:         *
031:         * @author  Tim Jones (Nacimiento Software)
032:         * @since   lucene 1.4
033:         * @version $Id: FieldDocSortedHitQueue.java 590530 2007-10-31 01:28:25Z gsingers $
034:         */
035:        class FieldDocSortedHitQueue extends PriorityQueue {
036:
037:            // this cannot contain AUTO fields - any AUTO fields should
038:            // have been resolved by the time this class is used.
039:            volatile SortField[] fields;
040:
041:            // used in the case where the fields are sorted by locale
042:            // based strings
043:            volatile Collator[] collators;
044:
045:            /**
046:             * Creates a hit queue sorted by the given list of fields.
047:             * @param fields Fieldable names, in priority order (highest priority first).
048:             * @param size  The number of hits to retain.  Must be greater than zero.
049:             */
050:            FieldDocSortedHitQueue(SortField[] fields, int size) {
051:                this .fields = fields;
052:                this .collators = hasCollators(fields);
053:                initialize(size);
054:            }
055:
056:            /**
057:             * Allows redefinition of sort fields if they are <code>null</code>.
058:             * This is to handle the case using ParallelMultiSearcher where the
059:             * original list contains AUTO and we don't know the actual sort
060:             * type until the values come back.  The fields can only be set once.
061:             * This method is thread safe.
062:             * @param fields
063:             */
064:            synchronized void setFields(SortField[] fields) {
065:                if (this .fields == null) {
066:                    this .fields = fields;
067:                    this .collators = hasCollators(fields);
068:                }
069:            }
070:
071:            /** Returns the fields being used to sort. */
072:            SortField[] getFields() {
073:                return fields;
074:            }
075:
076:            /** Returns an array of collators, possibly <code>null</code>.  The collators
077:             * correspond to any SortFields which were given a specific locale.
078:             * @param fields Array of sort fields.
079:             * @return Array, possibly <code>null</code>.
080:             */
081:            private Collator[] hasCollators(final SortField[] fields) {
082:                if (fields == null)
083:                    return null;
084:                Collator[] ret = new Collator[fields.length];
085:                for (int i = 0; i < fields.length; ++i) {
086:                    Locale locale = fields[i].getLocale();
087:                    if (locale != null)
088:                        ret[i] = Collator.getInstance(locale);
089:                }
090:                return ret;
091:            }
092:
093:            /**
094:             * Returns whether <code>a</code> is less relevant than <code>b</code>.
095:             * @param a ScoreDoc
096:             * @param b ScoreDoc
097:             * @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.
098:             */
099:            protected final boolean lessThan(final Object a, final Object b) {
100:                final FieldDoc docA = (FieldDoc) a;
101:                final FieldDoc docB = (FieldDoc) b;
102:                final int n = fields.length;
103:                int c = 0;
104:                for (int i = 0; i < n && c == 0; ++i) {
105:                    final int type = fields[i].getType();
106:                    switch (type) {
107:                    case SortField.SCORE: {
108:                        float r1 = ((Float) docA.fields[i]).floatValue();
109:                        float r2 = ((Float) docB.fields[i]).floatValue();
110:                        if (r1 > r2)
111:                            c = -1;
112:                        if (r1 < r2)
113:                            c = 1;
114:                        break;
115:                    }
116:                    case SortField.DOC:
117:                    case SortField.INT: {
118:                        int i1 = ((Integer) docA.fields[i]).intValue();
119:                        int i2 = ((Integer) docB.fields[i]).intValue();
120:                        if (i1 < i2)
121:                            c = -1;
122:                        if (i1 > i2)
123:                            c = 1;
124:                        break;
125:                    }
126:                    case SortField.LONG: {
127:                        long l1 = ((Long) docA.fields[i]).longValue();
128:                        long l2 = ((Long) docB.fields[i]).longValue();
129:                        if (l1 < l2)
130:                            c = -1;
131:                        if (l1 > l2)
132:                            c = 1;
133:                        break;
134:                    }
135:                    case SortField.STRING: {
136:                        String s1 = (String) docA.fields[i];
137:                        String s2 = (String) docB.fields[i];
138:                        // null values need to be sorted first, because of how FieldCache.getStringIndex()
139:                        // works - in that routine, any documents without a value in the given field are
140:                        // put first.  If both are null, the next SortField is used
141:                        if (s1 == null)
142:                            c = (s2 == null) ? 0 : -1;
143:                        else if (s2 == null)
144:                            c = 1; // 
145:                        else if (fields[i].getLocale() == null) {
146:                            c = s1.compareTo(s2);
147:                        } else {
148:                            c = collators[i].compare(s1, s2);
149:                        }
150:                        break;
151:                    }
152:                    case SortField.FLOAT: {
153:                        float f1 = ((Float) docA.fields[i]).floatValue();
154:                        float f2 = ((Float) docB.fields[i]).floatValue();
155:                        if (f1 < f2)
156:                            c = -1;
157:                        if (f1 > f2)
158:                            c = 1;
159:                        break;
160:                    }
161:                    case SortField.DOUBLE: {
162:                        double d1 = ((Double) docA.fields[i]).doubleValue();
163:                        double d2 = ((Double) docB.fields[i]).doubleValue();
164:                        if (d1 < d2)
165:                            c = -1;
166:                        if (d1 > d2)
167:                            c = 1;
168:                        break;
169:                    }
170:                    case SortField.CUSTOM: {
171:                        c = docA.fields[i].compareTo(docB.fields[i]);
172:                        break;
173:                    }
174:                    case SortField.AUTO: {
175:                        // we cannot handle this - even if we determine the type of object (Float or
176:                        // Integer), we don't necessarily know how to compare them (both SCORE and
177:                        // FLOAT contain floats, but are sorted opposite of each other). Before
178:                        // we get here, each AUTO should have been replaced with its actual value.
179:                        throw new RuntimeException(
180:                                "FieldDocSortedHitQueue cannot use an AUTO SortField");
181:                    }
182:                    default: {
183:                        throw new RuntimeException("invalid SortField type: "
184:                                + type);
185:                    }
186:                    }
187:                    if (fields[i].getReverse()) {
188:                        c = -c;
189:                    }
190:                }
191:
192:                // avoid random sort order that could lead to duplicates (bug #31241):
193:                if (c == 0)
194:                    return docA.doc > docB.doc;
195:
196:                return c > 0;
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.