]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/ReplaceableString.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / ReplaceableString.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2009, 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 /**\r
10  * <code>ReplaceableString</code> is an adapter class that implements the\r
11  * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.\r
12  *\r
13  * <p><em>Note:</em> This class does not support attributes and is not\r
14  * intended for general use.  Most clients will need to implement\r
15  * {@link Replaceable} in their text representation class.\r
16  *\r
17  * <p>Copyright &copy; IBM Corporation 1999.  All rights reserved.\r
18  *\r
19  * @see Replaceable\r
20  * @author Alan Liu\r
21  * @stable ICU 2.0\r
22  */\r
23 public class ReplaceableString implements Replaceable {\r
24     private StringBuffer buf;\r
25 \r
26     /**\r
27      * Construct a new object with the given initial contents.\r
28      * @param str initial contents\r
29      * @stable ICU 2.0\r
30      */\r
31     public ReplaceableString(String str) {\r
32         buf = new StringBuffer(str);\r
33     }\r
34 \r
35     /**\r
36      * Construct a new object using <code>buf</code> for internal\r
37      * storage.  The contents of <code>buf</code> at the time of\r
38      * construction are used as the initial contents.  <em>Note!\r
39      * Modifications to <code>buf</code> will modify this object, and\r
40      * vice versa.</em>\r
41      * @param buf object to be used as internal storage\r
42      * @stable ICU 2.0\r
43      */\r
44     public ReplaceableString(StringBuffer buf) {\r
45         this.buf = buf;\r
46     }\r
47 \r
48     /**\r
49      * Construct a new empty object.\r
50      * @stable ICU 2.0\r
51      */\r
52     public ReplaceableString() {\r
53         buf = new StringBuffer();\r
54     }\r
55 \r
56     /**\r
57      * Return the contents of this object as a <code>String</code>.\r
58      * @return string contents of this object\r
59      * @stable ICU 2.0\r
60      */\r
61     public String toString() {\r
62         return buf.toString();\r
63     }\r
64 \r
65     /**\r
66      * Return a substring of the given string.\r
67      * @stable ICU 2.0\r
68      */\r
69     public String substring(int start, int limit) {\r
70         return buf.substring(start, limit);\r
71     }\r
72 \r
73     /**\r
74      * Return the number of characters contained in this object.\r
75      * <code>Replaceable</code> API.\r
76      * @stable ICU 2.0\r
77      */ \r
78     public int length() {\r
79         return buf.length();\r
80     }\r
81 \r
82     /**\r
83      * Return the character at the given position in this object.\r
84      * <code>Replaceable</code> API.\r
85      * @param offset offset into the contents, from 0 to\r
86      * <code>length()</code> - 1\r
87      * @stable ICU 2.0\r
88      */\r
89     public char charAt(int offset) {\r
90         return buf.charAt(offset);\r
91     }\r
92 \r
93     /**\r
94      * Return the 32-bit code point at the given 16-bit offset into\r
95      * the text.  This assumes the text is stored as 16-bit code units\r
96      * with surrogate pairs intermixed.  If the offset of a leading or\r
97      * trailing code unit of a surrogate pair is given, return the\r
98      * code point of the surrogate pair.\r
99      * @param offset an integer between 0 and <code>length()</code>-1\r
100      * inclusive\r
101      * @return 32-bit code point of text at given offset\r
102      * @stable ICU 2.0\r
103      */\r
104     public int char32At(int offset) {\r
105         return UTF16.charAt(buf, offset);\r
106     }\r
107 \r
108     /**\r
109      * Copies characters from this object into the destination\r
110      * character array.  The first character to be copied is at index\r
111      * <code>srcStart</code>; the last character to be copied is at\r
112      * index <code>srcLimit-1</code> (thus the total number of\r
113      * characters to be copied is <code>srcLimit-srcStart</code>). The\r
114      * characters are copied into the subarray of <code>dst</code>\r
115      * starting at index <code>dstStart</code> and ending at index\r
116      * <code>dstStart + (srcLimit-srcStart) - 1</code>.\r
117      *\r
118      * @param srcStart the beginning index to copy, inclusive; <code>0\r
119      * <= start <= limit</code>.\r
120      * @param srcLimit the ending index to copy, exclusive;\r
121      * <code>start <= limit <= length()</code>.\r
122      * @param dst the destination array.\r
123      * @param dstStart the start offset in the destination array.\r
124      * @stable ICU 2.0\r
125      */\r
126     public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {\r
127         if (srcStart != srcLimit) {\r
128             buf.getChars(srcStart, srcLimit, dst, dstStart);\r
129         }\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