Source Code Cross Referenced for MultiMap.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » tomcat » util » collections » 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 » Sevlet Container » apache tomcat 6.0.14 » org.apache.tomcat.util.collections 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.tomcat.util.collections;
019:
020:        import org.apache.tomcat.util.buf.MessageBytes;
021:
022:        // Originally MimeHeaders
023:
024:        /**
025:         * An efficient representation for certain type of map. The keys 
026:         * can have a single or multi values, but most of the time there are
027:         * single values.
028:         *
029:         * The data is of "MessageBytes" type, meaning bytes[] that can be
030:         * converted to Strings ( if needed, and encoding is lazy-binded ).
031:         *
032:         * This is a base class for MimeHeaders, Parameters and Cookies.
033:         *
034:         * Data structures: each field is a single-valued key/value.
035:         * The fields are allocated when needed, and are recycled.
036:         * The current implementation does linear search, in future we'll
037:         * also use the hashkey.
038:         * 
039:         * @author dac@eng.sun.com
040:         * @author James Todd [gonzo@eng.sun.com]
041:         * @author Costin Manolache
042:         */
043:        public class MultiMap {
044:
045:            protected Field[] fields;
046:            // fields in use
047:            protected int count;
048:
049:            /**
050:             * 
051:             */
052:            public MultiMap(int initial_size) {
053:                fields = new Field[initial_size];
054:            }
055:
056:            /**
057:             * Clears all header fields.
058:             */
059:            public void recycle() {
060:                for (int i = 0; i < count; i++) {
061:                    fields[i].recycle();
062:                }
063:                count = 0;
064:            }
065:
066:            // -------------------- Idx access to headers ----------
067:            // This allows external iterators.
068:
069:            /**
070:             * Returns the current number of header fields.
071:             */
072:            public int size() {
073:                return count;
074:            }
075:
076:            /**
077:             * Returns the Nth header name
078:             * This may be used to iterate through all header fields.
079:             *
080:             * An exception is thrown if the index is not valid ( <0 or >size )
081:             */
082:            public MessageBytes getName(int n) {
083:                // n >= 0 && n < count ? headers[n].getName() : null
084:                return fields[n].name;
085:            }
086:
087:            /**
088:             * Returns the Nth header value
089:             * This may be used to iterate through all header fields.
090:             */
091:            public MessageBytes getValue(int n) {
092:                return fields[n].value;
093:            }
094:
095:            /** Find the index of a field with the given name.
096:             */
097:            public int find(String name, int starting) {
098:                // We can use a hash - but it's not clear how much
099:                // benefit you can get - there is an  overhead 
100:                // and the number of headers is small (4-5 ?)
101:                // Another problem is that we'll pay the overhead
102:                // of constructing the hashtable
103:
104:                // A custom search tree may be better
105:                for (int i = starting; i < count; i++) {
106:                    if (fields[i].name.equals(name)) {
107:                        return i;
108:                    }
109:                }
110:                return -1;
111:            }
112:
113:            /** Find the index of a field with the given name.
114:             */
115:            public int findIgnoreCase(String name, int starting) {
116:                // We can use a hash - but it's not clear how much
117:                // benefit you can get - there is an  overhead 
118:                // and the number of headers is small (4-5 ?)
119:                // Another problem is that we'll pay the overhead
120:                // of constructing the hashtable
121:
122:                // A custom search tree may be better
123:                for (int i = starting; i < count; i++) {
124:                    if (fields[i].name.equalsIgnoreCase(name)) {
125:                        return i;
126:                    }
127:                }
128:                return -1;
129:            }
130:
131:            /**
132:             * Removes the field at the specified position.  
133:             *
134:             * MultiMap will preserve the order of field add unless remove()
135:             * is called. This is not thread-safe, and will invalidate all
136:             * iterators. 
137:             *
138:             * This is not a frequent operation for Headers and Parameters -
139:             * there are better ways ( like adding a "isValid" field )
140:             */
141:            public void remove(int i) {
142:                // reset and swap with last header
143:                Field mh = fields[i];
144:                // reset the field
145:                mh.recycle();
146:
147:                fields[i] = fields[count - 1];
148:                fields[count - 1] = mh;
149:                count--;
150:            }
151:
152:            /** Create a new, unitialized entry. 
153:             */
154:            public int addField() {
155:                int len = fields.length;
156:                int pos = count;
157:                if (count >= len) {
158:                    // expand header list array
159:                    Field tmp[] = new Field[pos * 2];
160:                    System.arraycopy(fields, 0, tmp, 0, len);
161:                    fields = tmp;
162:                }
163:                if (fields[pos] == null) {
164:                    fields[pos] = new Field();
165:                }
166:                count++;
167:                return pos;
168:            }
169:
170:            public MessageBytes get(String name) {
171:                for (int i = 0; i < count; i++) {
172:                    if (fields[i].name.equals(name)) {
173:                        return fields[i].value;
174:                    }
175:                }
176:                return null;
177:            }
178:
179:            public int findFirst(String name) {
180:                for (int i = 0; i < count; i++) {
181:                    if (fields[i].name.equals(name)) {
182:                        return i;
183:                    }
184:                }
185:                return -1;
186:            }
187:
188:            public int findNext(int startPos) {
189:                int next = fields[startPos].nextPos;
190:                if (next != MultiMap.NEED_NEXT) {
191:                    return next;
192:                }
193:
194:                // next==NEED_NEXT, we never searched for this header
195:                MessageBytes name = fields[startPos].name;
196:                for (int i = startPos; i < count; i++) {
197:                    if (fields[i].name.equals(name)) {
198:                        // cache the search result
199:                        fields[startPos].nextPos = i;
200:                        return i;
201:                    }
202:                }
203:                fields[startPos].nextPos = MultiMap.LAST;
204:                return -1;
205:            }
206:
207:            // workaround for JDK1.1.8/solaris
208:            static final int NEED_NEXT = -2;
209:            static final int LAST = -1;
210:
211:            // -------------------- Internal representation --------------------
212:            final class Field {
213:                MessageBytes name;
214:                MessageBytes value;
215:
216:                // Extra info for speed
217:
218:                //  multiple fields with same name - a linked list will
219:                // speed up multiple name enumerations and search.
220:                int nextPos;
221:
222:                // hashkey
223:                int hash;
224:                Field nextSameHash;
225:
226:                Field() {
227:                    nextPos = MultiMap.NEED_NEXT;
228:                }
229:
230:                void recycle() {
231:                    name.recycle();
232:                    value.recycle();
233:                    nextPos = MultiMap.NEED_NEXT;
234:                }
235:            }
236:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.