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