]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/ReplaceableString.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / ReplaceableString.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2007, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 \r
9 import com.ibm.icu.impl.Utility;\r
10 \r
11 /**\r
12  * <code>ReplaceableString</code> is an adapter class that implements the\r
13  * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.\r
14  *\r
15  * <p><em>Note:</em> This class does not support attributes and is not\r
16  * intended for general use.  Most clients will need to implement\r
17  * {@link Replaceable} in their text representation class.\r
18  *\r
19  * <p>Copyright &copy; IBM Corporation 1999.  All rights reserved.\r
20  *\r
21  * @see Replaceable\r
22  * @author Alan Liu\r
23  * @stable ICU 2.0\r
24  */\r
25 public class ReplaceableString implements Replaceable {\r
26     private StringBuffer buf;\r
27 \r
28     /**\r
29      * Construct a new object with the given initial contents.\r
30      * @param str initial contents\r
31      * @stable ICU 2.0\r
32      */\r
33     public ReplaceableString(String str) {\r
34         buf = new StringBuffer(str);\r
35     }\r
36 \r
37     /**\r
38      * Construct a new object using <code>buf</code> for internal\r
39      * storage.  The contents of <code>buf</code> at the time of\r
40      * construction are used as the initial contents.  <em>Note!\r
41      * Modifications to <code>buf</code> will modify this object, and\r
42      * vice versa.</em>\r
43      * @param buf object to be used as internal storage\r
44      * @stable ICU 2.0\r
45      */\r
46     public ReplaceableString(StringBuffer buf) {\r
47         this.buf = buf;\r
48     }\r
49 \r
50     /**\r
51      * Construct a new empty object.\r
52      * @stable ICU 2.0\r
53      */\r
54     public ReplaceableString() {\r
55         buf = new StringBuffer();\r
56     }\r
57 \r
58     /**\r
59      * Return the contents of this object as a <code>String</code>.\r
60      * @return string contents of this object\r
61      * @stable ICU 2.0\r
62      */\r
63     public String toString() {\r
64         return buf.toString();\r
65     }\r
66 \r
67     /**\r
68      * Return a substring of the given string.\r
69      * @stable ICU 2.0\r
70      */\r
71     public String substring(int start, int limit) {\r
72         return buf.substring(start, limit);\r
73     }\r
74 \r
75     /**\r
76      * Return the number of characters contained in this object.\r
77      * <code>Replaceable</code> API.\r
78      * @stable ICU 2.0\r
79      */ \r
80     public int length() {\r
81         return buf.length();\r
82     }\r
83 \r
84     /**\r
85      * Return the character at the given position in this object.\r
86      * <code>Replaceable</code> API.\r
87      * @param offset offset into the contents, from 0 to\r
88      * <code>length()</code> - 1\r
89      * @stable ICU 2.0\r
90      */\r
91     public char charAt(int offset) {\r
92         return buf.charAt(offset);\r
93     }\r
94 \r
95     /**\r
96      * Return the 32-bit code point at the given 16-bit offset into\r
97      * the text.  This assumes the text is stored as 16-bit code units\r
98      * with surrogate pairs intermixed.  If the offset of a leading or\r
99      * trailing code unit of a surrogate pair is given, return the\r
100      * code point of the surrogate pair.\r
101      * @param offset an integer between 0 and <code>length()</code>-1\r
102      * inclusive\r
103      * @return 32-bit code point of text at given offset\r
104      * @stable ICU 2.0\r
105      */\r
106     public int char32At(int offset) {\r
107         return UTF16.charAt(buf, offset);\r
108     }\r
109 \r
110     /**\r
111      * Copies characters from this object into the destination\r
112      * character array.  The first character to be copied is at index\r
113      * <code>srcStart</code>; the last character to be copied is at\r
114      * index <code>srcLimit-1</code> (thus the total number of\r
115      * characters to be copied is <code>srcLimit-srcStart</code>). The\r
116      * characters are copied into the subarray of <code>dst</code>\r
117      * starting at index <code>dstStart</code> and ending at index\r
118      * <code>dstStart + (srcLimit-srcStart) - 1</code>.\r
119      *\r
120      * @param srcStart the beginning index to copy, inclusive; <code>0\r
121      * <= start <= limit</code>.\r
122      * @param srcLimit the ending index to copy, exclusive;\r
123      * <code>start <= limit <= length()</code>.\r
124      * @param dst the destination array.\r
125      * @param dstStart the start offset in the destination array.\r
126      * @stable ICU 2.0\r
127      */\r
128     public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {\r
129         Utility.getChars(buf, srcStart, srcLimit, dst, dstStart);\r
130     }\r
131 \r
132     /**\r
133      * Replace zero or more characters with new characters.\r
134      * <code>Replaceable</code> API.\r
135      * @param start the beginning index, inclusive; <code>0 <= start\r
136      * <= limit</code>.\r
137      * @param limit the ending index, exclusive; <code>start <= limit\r
138      * <= length()</code>.\r
139      * @param text new text to replace characters <code>start</code> to\r
140      * <code>limit - 1</code>\r
141      * @stable ICU 2.0\r
142      */\r
143     public void replace(int start, int limit, String text) {\r
144         buf.replace(start, limit, text);\r
145     }\r
146 \r
147     /**\r
148      * Replace a substring of this object with the given text.\r
149      * @param start the beginning index, inclusive; <code>0 <= start\r
150      * <= limit</code>.\r
151      * @param limit the ending index, exclusive; <code>start <= limit\r
152      * <= length()</code>.\r
153      * @param chars the text to replace characters <code>start</code>\r
154      * to <code>limit - 1</code>\r
155      * @param charsStart the beginning index into <code>chars</code>,\r
156      * inclusive; <code>0 <= start <= limit</code>.\r
157      * @param charsLen the number of characters of <code>chars</code>.\r
158      * @stable ICU 2.0\r
159      */\r
160     public void replace(int start, int limit, char[] chars,\r
161                         int charsStart, int charsLen) {\r
162         buf.delete(start, limit);\r
163         buf.insert(start, chars, charsStart, charsLen);\r
164     }\r
165 \r
166     /**\r
167      * Copy a substring of this object, retaining attribute (out-of-band)\r
168      * information.  This method is used to duplicate or reorder substrings.\r
169      * The destination index must not overlap the source range.\r
170      * \r
171      * @param start the beginning index, inclusive; <code>0 <= start <=\r
172      * limit</code>.\r
173      * @param limit the ending index, exclusive; <code>start <= limit <=\r
174      * length()</code>.\r
175      * @param dest the destination index.  The characters from\r
176      * <code>start..limit-1</code> will be copied to <code>dest</code>.\r
177      * Implementations of this method may assume that <code>dest <= start ||\r
178      * dest >= limit</code>.\r
179      * @stable ICU 2.0\r
180      */\r
181     public void copy(int start, int limit, int dest) {\r
182         if (start == limit && start >= 0 && start <= buf.length()) {\r
183             return;\r
184         }\r
185         char[] text = new char[limit - start];\r
186         getChars(start, limit, text, 0);\r
187         replace(dest, dest, text, 0, limit - start);\r
188     }\r
189     \r
190     /**\r
191      * Implements Replaceable\r
192      * @stable ICU 2.0\r
193      */\r
194     public boolean hasMetaData() {\r
195         return false;\r
196     }\r
197 }\r