Source Code Cross Referenced for FieldPosition.java in  » 6.0-JDK-Modules » j2me » java » text » 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 » 6.0 JDK Modules » j2me » java.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * 
003:         * @(#)FieldPosition.java	1.23 06/10/10
004:         * 
005:         * Portions Copyright  2000-2006 Sun Microsystems, Inc. All Rights
006:         * Reserved.  Use is subject to license terms.
007:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
008:         * 
009:         * This program is free software; you can redistribute it and/or
010:         * modify it under the terms of the GNU General Public License version
011:         * 2 only, as published by the Free Software Foundation.
012:         * 
013:         * This program is distributed in the hope that it will be useful, but
014:         * WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016:         * General Public License version 2 for more details (a copy is
017:         * included at /legal/license.txt).
018:         * 
019:         * You should have received a copy of the GNU General Public License
020:         * version 2 along with this work; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022:         * 02110-1301 USA
023:         * 
024:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
025:         * Clara, CA 95054 or visit www.sun.com if you need additional
026:         * information or have any questions.
027:         */
028:
029:        /*
030:         * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
031:         * (C) Copyright IBM Corp. 1996 - All Rights Reserved
032:         *
033:         *   The original version of this source code and documentation is copyrighted
034:         * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
035:         * materials are provided under terms of a License Agreement between Taligent
036:         * and Sun. This technology is protected by multiple US and International
037:         * patents. This notice and attribution to Taligent may not be removed.
038:         *   Taligent is a registered trademark of Taligent, Inc.
039:         *
040:         */
041:
042:        package java.text;
043:
044:        /**
045:         * <code>FieldPosition</code> is a simple class used by <code>Format</code>
046:         * and its subclasses to identify fields in formatted output. Fields can
047:         * be identified in two ways:
048:         * <ul>
049:         *  <li>By an integer constant, whose names typically end with
050:         *      <code>_FIELD</code>. The constants are defined in the various
051:         *      subclasses of <code>Format</code>.
052:         *  <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>
053:         *      and its friends in <code>DateFormat</code> for an example.
054:         * </ul>
055:         * <p>
056:         * <code>FieldPosition</code> keeps track of the position of the
057:         * field within the formatted output with two indices: the index
058:         * of the first character of the field and the index of the last
059:         * character of the field.
060:         *
061:         * <p>
062:         * One version of the <code>format</code> method in the various
063:         * <code>Format</code> classes requires a <code>FieldPosition</code>
064:         * object as an argument. You use this <code>format</code> method
065:         * to perform partial formatting or to get information about the
066:         * formatted output (such as the position of a field).
067:         *
068:         * <p>
069:         * If you are interested in the positions of all attributes in the
070:         * formatted string use the <code>Format</code> method
071:         * <code>formatToCharacterIterator</code>.
072:         *
073:         * @version     1.23 10/10/06
074:         * @author      Mark Davis
075:         * @see         java.text.Format
076:         */
077:        public class FieldPosition {
078:
079:            /**
080:             * Input: Desired field to determine start and end offsets for.
081:             * The meaning depends on the subclass of Format.
082:             */
083:            int field = 0;
084:
085:            /**
086:             * Output: End offset of field in text.
087:             * If the field does not occur in the text, 0 is returned.
088:             */
089:            int endIndex = 0;
090:
091:            /**
092:             * Output: Start offset of field in text.
093:             * If the field does not occur in the text, 0 is returned.
094:             */
095:            int beginIndex = 0;
096:
097:            /**
098:             * Desired field this FieldPosition is for.
099:             */
100:            private Format.Field attribute;
101:
102:            /**
103:             * Creates a FieldPosition object for the given field.  Fields are
104:             * identified by constants, whose names typically end with _FIELD,
105:             * in the various subclasses of Format.
106:             *
107:             * @see java.text.NumberFormat#INTEGER_FIELD
108:             * @see java.text.NumberFormat#FRACTION_FIELD
109:             * @see java.text.DateFormat#YEAR_FIELD
110:             * @see java.text.DateFormat#MONTH_FIELD
111:             */
112:            public FieldPosition(int field) {
113:                this .field = field;
114:            }
115:
116:            /**
117:             * Creates a FieldPosition object for the given field constant. Fields are
118:             * identified by constants defined in the various <code>Format</code>
119:             * subclasses. This is equivalent to calling
120:             * <code>new FieldPosition(attribute, -1)</code>.
121:             *
122:             * @param attribute Format.Field constant identifying a field
123:             * @since 1.4
124:             */
125:            public FieldPosition(Format.Field attribute) {
126:                this (attribute, -1);
127:            }
128:
129:            /**
130:             * Creates a <code>FieldPosition</code> object for the given field.
131:             * The field is identified by an attribute constant from one of the
132:             * <code>Field</code> subclasses as well as an integer field ID
133:             * defined by the <code>Format</code> subclasses. <code>Format</code>
134:             * subclasses that are aware of <code>Field</code> should give precedence
135:             * to <code>attribute</code> and ignore <code>fieldID</code> if
136:             * <code>attribute</code> is not null. However, older <code>Format</code>
137:             * subclasses may not be aware of <code>Field</code> and rely on
138:             * <code>fieldID</code>. If the field has no corresponding integer
139:             * constant, <code>fieldID</code> should be -1.
140:             *
141:             * @param attribute Format.Field constant identifying a field
142:             * @param fieldID integer constantce identifying a field
143:             * @since 1.4
144:             */
145:            public FieldPosition(Format.Field attribute, int fieldID) {
146:                this .attribute = attribute;
147:                this .field = fieldID;
148:            }
149:
150:            /**
151:             * Returns the field identifier as an attribute constant
152:             * from one of the <code>Field</code> subclasses. May return null if
153:             * the field is specified only by an integer field ID.
154:             *
155:             * @return Identifier for the field
156:             * @since 1.4
157:             */
158:            public Format.Field getFieldAttribute() {
159:                return attribute;
160:            }
161:
162:            /**
163:             * Retrieves the field identifier.
164:             */
165:            public int getField() {
166:                return field;
167:            }
168:
169:            /**
170:             * Retrieves the index of the first character in the requested field.
171:             */
172:            public int getBeginIndex() {
173:                return beginIndex;
174:            }
175:
176:            /**
177:             * Retrieves the index of the character following the last character in the
178:             * requested field.
179:             */
180:            public int getEndIndex() {
181:                return endIndex;
182:            }
183:
184:            /**
185:             * Sets the begin index.  For use by subclasses of Format.
186:             * @since 1.2
187:             */
188:            public void setBeginIndex(int bi) {
189:                beginIndex = bi;
190:            }
191:
192:            /**
193:             * Sets the end index.  For use by subclasses of Format.
194:             * @since 1.2
195:             */
196:            public void setEndIndex(int ei) {
197:                endIndex = ei;
198:            }
199:
200:            /**
201:             * Returns a <code>Format.FieldDelegate</code> instance that is associated
202:             * with the FieldPosition. When the delegate is notified of the same
203:             * field the FieldPosition is associated with, the begin/end will be
204:             * adjusted.
205:             */
206:            Format.FieldDelegate getFieldDelegate() {
207:                return new Delegate();
208:            }
209:
210:            /**
211:             * Overrides equals
212:             */
213:            public boolean equals(Object obj) {
214:                if (obj == null)
215:                    return false;
216:                if (!(obj instanceof  FieldPosition))
217:                    return false;
218:                FieldPosition other = (FieldPosition) obj;
219:                if (attribute == null) {
220:                    if (other.attribute != null) {
221:                        return false;
222:                    }
223:                } else if (!attribute.equals(other.attribute)) {
224:                    return false;
225:                }
226:                return (beginIndex == other.beginIndex
227:                        && endIndex == other.endIndex && field == other.field);
228:            }
229:
230:            /**
231:             * Returns a hash code for this FieldPosition.
232:             * @return a hash code value for this object
233:             */
234:            public int hashCode() {
235:                return (field << 24) | (beginIndex << 16) | endIndex;
236:            }
237:
238:            /**
239:             * Return a string representation of this FieldPosition.
240:             * @return  a string representation of this object
241:             */
242:            public String toString() {
243:                return getClass().getName() + "[field=" + field + ",attribute="
244:                        + attribute + ",beginIndex=" + beginIndex
245:                        + ",endIndex=" + endIndex + ']';
246:            }
247:
248:            /**
249:             * Return true if the receiver wants a <code>Format.Field</code> value and
250:             * <code>attribute</code> is equal to it.
251:             */
252:            private boolean matchesField(Format.Field attribute) {
253:                if (this .attribute != null) {
254:                    return this .attribute.equals(attribute);
255:                }
256:                return false;
257:            }
258:
259:            /**
260:             * Return true if the receiver wants a <code>Format.Field</code> value and
261:             * <code>attribute</code> is equal to it, or true if the receiver
262:             * represents an inteter constant and <code>field</code> equals it.
263:             */
264:            private boolean matchesField(Format.Field attribute, int field) {
265:                if (this .attribute != null) {
266:                    return this .attribute.equals(attribute);
267:                }
268:                return (field == this .field);
269:            }
270:
271:            /**
272:             * An implementation of FieldDelegate that will adjust the begin/end
273:             * of the FieldPosition if the arguments match the field of
274:             * the FieldPosition.
275:             */
276:            private class Delegate implements  Format.FieldDelegate {
277:                /**
278:                 * Indicates whether the field has been  encountered before. If this
279:                 * is true, and <code>formatted</code> is invoked, the begin/end
280:                 * are not updated.
281:                 */
282:                private boolean encounteredField;
283:
284:                public void formatted(Format.Field attr, Object value,
285:                        int start, int end, StringBuffer buffer) {
286:                    if (!encounteredField && matchesField(attr)) {
287:                        setBeginIndex(start);
288:                        setEndIndex(end);
289:                        encounteredField = (start != end);
290:                    }
291:                }
292:
293:                public void formatted(int fieldID, Format.Field attr,
294:                        Object value, int start, int end, StringBuffer buffer) {
295:                    if (!encounteredField && matchesField(attr, fieldID)) {
296:                        setBeginIndex(start);
297:                        setEndIndex(end);
298:                        encounteredField = (start != end);
299:                    }
300:                }
301:            }
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.