001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.text;
008:
009: import com.ibm.icu.impl.Utility;
010:
011: /**
012: * <code>ReplaceableString</code> is an adapter class that implements the
013: * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.
014: *
015: * <p><em>Note:</em> This class does not support attributes and is not
016: * intended for general use. Most clients will need to implement
017: * {@link Replaceable} in their text representation class.
018: *
019: * <p>Copyright © IBM Corporation 1999. All rights reserved.
020: *
021: * @see Replaceable
022: * @author Alan Liu
023: * @stable ICU 2.0
024: */
025: public class ReplaceableString implements Replaceable {
026: private StringBuffer buf;
027:
028: private static final String COPYRIGHT = "\u00A9 IBM Corporation 1999. All rights reserved.";
029:
030: /**
031: * Construct a new object with the given initial contents.
032: * @param str initial contents
033: * @stable ICU 2.0
034: */
035: public ReplaceableString(String str) {
036: buf = new StringBuffer(str);
037: }
038:
039: /**
040: * Construct a new object using <code>buf</code> for internal
041: * storage. The contents of <code>buf</code> at the time of
042: * construction are used as the initial contents. <em>Note!
043: * Modifications to <code>buf</code> will modify this object, and
044: * vice versa.</em>
045: * @param buf object to be used as internal storage
046: * @stable ICU 2.0
047: */
048: public ReplaceableString(StringBuffer buf) {
049: this .buf = buf;
050: }
051:
052: /**
053: * Construct a new empty object.
054: * @stable ICU 2.0
055: */
056: public ReplaceableString() {
057: buf = new StringBuffer();
058: }
059:
060: /**
061: * Return the contents of this object as a <code>String</code>.
062: * @return string contents of this object
063: * @stable ICU 2.0
064: */
065: public String toString() {
066: return buf.toString();
067: }
068:
069: /**
070: * Return a substring of the given string.
071: * @stable ICU 2.0
072: */
073: public String substring(int start, int limit) {
074: return buf.substring(start, limit);
075: }
076:
077: /**
078: * Return the number of characters contained in this object.
079: * <code>Replaceable</code> API.
080: * @stable ICU 2.0
081: */
082: public int length() {
083: return buf.length();
084: }
085:
086: /**
087: * Return the character at the given position in this object.
088: * <code>Replaceable</code> API.
089: * @param offset offset into the contents, from 0 to
090: * <code>length()</code> - 1
091: * @stable ICU 2.0
092: */
093: public char charAt(int offset) {
094: return buf.charAt(offset);
095: }
096:
097: /**
098: * Return the 32-bit code point at the given 16-bit offset into
099: * the text. This assumes the text is stored as 16-bit code units
100: * with surrogate pairs intermixed. If the offset of a leading or
101: * trailing code unit of a surrogate pair is given, return the
102: * code point of the surrogate pair.
103: * @param offset an integer between 0 and <code>length()</code>-1
104: * inclusive
105: * @return 32-bit code point of text at given offset
106: * @stable ICU 2.0
107: */
108: public int char32At(int offset) {
109: return UTF16.charAt(buf, offset);
110: }
111:
112: /**
113: * Copies characters from this object into the destination
114: * character array. The first character to be copied is at index
115: * <code>srcStart</code>; the last character to be copied is at
116: * index <code>srcLimit-1</code> (thus the total number of
117: * characters to be copied is <code>srcLimit-srcStart</code>). The
118: * characters are copied into the subarray of <code>dst</code>
119: * starting at index <code>dstStart</code> and ending at index
120: * <code>dstStart + (srcLimit-srcStart) - 1</code>.
121: *
122: * @param srcStart the beginning index to copy, inclusive; <code>0
123: * <= start <= limit</code>.
124: * @param srcLimit the ending index to copy, exclusive;
125: * <code>start <= limit <= length()</code>.
126: * @param dst the destination array.
127: * @param dstStart the start offset in the destination array.
128: * @stable ICU 2.0
129: */
130: public void getChars(int srcStart, int srcLimit, char dst[],
131: 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 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: }
|