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