KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > ReplaceableString


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2004, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.text;
8
9 import com.ibm.icu.impl.Utility;
10
11 /**
12  * <code>ReplaceableString</code> is an adapter class that implements the
13  * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.
14  *
15  * <p><em>Note:</em> This class does not support attributes and is not
16  * intended for general use. Most clients will need to implement
17  * {@link Replaceable} in their text representation class.
18  *
19  * <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
20  *
21  * @see Replaceable
22  * @author Alan Liu
23  * @stable ICU 2.0
24  */

25 public class ReplaceableString implements Replaceable {
26     private StringBuffer JavaDoc buf;
27
28     private static final String JavaDoc COPYRIGHT =
29         "\u00A9 IBM Corporation 1999. All rights reserved.";
30
31     /**
32      * Construct a new object with the given initial contents.
33      * @param str initial contents
34      * @stable ICU 2.0
35      */

36     public ReplaceableString(String JavaDoc str) {
37         buf = new StringBuffer JavaDoc(str);
38     }
39
40     /**
41      * Construct a new object using <code>buf</code> for internal
42      * storage. The contents of <code>buf</code> at the time of
43      * construction are used as the initial contents. <em>Note!
44      * Modifications to <code>buf</code> will modify this object, and
45      * vice versa.</em>
46      * @param buf object to be used as internal storage
47      * @stable ICU 2.0
48      */

49     public ReplaceableString(StringBuffer JavaDoc buf) {
50         this.buf = buf;
51     }
52
53     /**
54      * Construct a new empty object.
55      * @stable ICU 2.0
56      */

57     public ReplaceableString() {
58         buf = new StringBuffer JavaDoc();
59     }
60
61     /**
62      * Return the contents of this object as a <code>String</code>.
63      * @return string contents of this object
64      * @stable ICU 2.0
65      */

66     public String JavaDoc toString() {
67         return buf.toString();
68     }
69
70     /**
71      * Return a substring of the given string.
72      * @stable ICU 2.0
73      */

74     public String JavaDoc substring(int start, int limit) {
75         return buf.substring(start, limit);
76     }
77
78     /**
79      * Return the number of characters contained in this object.
80      * <code>Replaceable</code> API.
81      * @stable ICU 2.0
82      */

83     public int length() {
84         return buf.length();
85     }
86
87     /**
88      * Return the character at the given position in this object.
89      * <code>Replaceable</code> API.
90      * @param offset offset into the contents, from 0 to
91      * <code>length()</code> - 1
92      * @stable ICU 2.0
93      */

94     public char charAt(int offset) {
95         return buf.charAt(offset);
96     }
97
98     /**
99      * Return the 32-bit code point at the given 16-bit offset into
100      * the text. This assumes the text is stored as 16-bit code units
101      * with surrogate pairs intermixed. If the offset of a leading or
102      * trailing code unit of a surrogate pair is given, return the
103      * code point of the surrogate pair.
104      * @param offset an integer between 0 and <code>length()</code>-1
105      * inclusive
106      * @return 32-bit code point of text at given offset
107      * @stable ICU 2.0
108      */

109     public int char32At(int offset) {
110         return UTF16.charAt(buf, offset);
111     }
112
113     /**
114      * Copies characters from this object into the destination
115      * character array. The first character to be copied is at index
116      * <code>srcStart</code>; the last character to be copied is at
117      * index <code>srcLimit-1</code> (thus the total number of
118      * characters to be copied is <code>srcLimit-srcStart</code>). The
119      * characters are copied into the subarray of <code>dst</code>
120      * starting at index <code>dstStart</code> and ending at index
121      * <code>dstStart + (srcLimit-srcStart) - 1</code>.
122      *
123      * @param srcStart the beginning index to copy, inclusive; <code>0
124      * <= start <= limit</code>.
125      * @param srcLimit the ending index to copy, exclusive;
126      * <code>start <= limit <= length()</code>.
127      * @param dst the destination array.
128      * @param dstStart the start offset in the destination array.
129      * @stable ICU 2.0
130      */

131     public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {
132         Utility.getChars(buf, srcStart, srcLimit, dst, dstStart);
133     }
134
135     /**
136      * Replace zero or more characters with new characters.
137      * <code>Replaceable</code> API.
138      * @param start the beginning index, inclusive; <code>0 <= start
139      * <= limit</code>.
140      * @param limit the ending index, exclusive; <code>start <= limit
141      * <= length()</code>.
142      * @param text new text to replace characters <code>start</code> to
143      * <code>limit - 1</code>
144      * @stable ICU 2.0
145      */

146     public void replace(int start, int limit, String JavaDoc text) {
147         buf.replace(start, limit, text);
148     }
149
150     /**
151      * Replace a substring of this object with the given text.
152      * @param start the beginning index, inclusive; <code>0 <= start
153      * <= limit</code>.
154      * @param limit the ending index, exclusive; <code>start <= limit
155      * <= length()</code>.
156      * @param chars the text to replace characters <code>start</code>
157      * to <code>limit - 1</code>
158      * @param charsStart the beginning index into <code>chars</code>,
159      * inclusive; <code>0 <= start <= limit</code>.
160      * @param charsLen the number of characters of <code>chars</code>.
161      * @stable ICU 2.0
162      */

163     public void replace(int start, int limit, char[] chars,
164                         int charsStart, int charsLen) {
165         buf.delete(start, limit);
166         buf.insert(start, chars, charsStart, charsLen);
167     }
168
169     /**
170      * Copy a substring of this object, retaining attribute (out-of-band)
171      * information. This method is used to duplicate or reorder substrings.
172      * The destination index must not overlap the source range.
173      *
174      * @param start the beginning index, inclusive; <code>0 <= start <=
175      * limit</code>.
176      * @param limit the ending index, exclusive; <code>start <= limit <=
177      * length()</code>.
178      * @param dest the destination index. The characters from
179      * <code>start..limit-1</code> will be copied to <code>dest</code>.
180      * Implementations of this method may assume that <code>dest <= start ||
181      * dest >= limit</code>.
182      * @stable ICU 2.0
183      */

184     public void copy(int start, int limit, int dest) {
185         if (start == limit && start >= 0 && start <= buf.length()) {
186             return;
187         }
188         char[] text = new char[limit - start];
189         getChars(start, limit, text, 0);
190         replace(dest, dest, text, 0, limit - start);
191     }
192     
193     /**
194      * Implements Replaceable
195      * @stable ICU 2.0
196      */

197     public boolean hasMetaData() {
198         return false;
199     }
200 }
201
Popular Tags