com.ibm.icu.text.ReplaceableString Maven / Gradle / Ivy
Show all versions of icu4j Show documentation
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 1996-2016, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.text;
/**
* ReplaceableString
is an adapter class that implements the
* Replaceable
API around an ordinary StringBuffer
.
*
* Note: This class does not support attributes and is not
* intended for general use. Most clients will need to implement
* {@link Replaceable} in their text representation class.
*
* @see Replaceable
* @author Alan Liu
* @stable ICU 2.0
*/
public class ReplaceableString implements Replaceable {
private StringBuffer buf;
/**
* Construct a new object with the given initial contents.
* @param str initial contents
* @stable ICU 2.0
*/
public ReplaceableString(String str) {
buf = new StringBuffer(str);
}
/**
* Construct a new object using buf
for internal
* storage. The contents of buf
at the time of
* construction are used as the initial contents. Note!
* Modifications to buf
will modify this object, and
* vice versa.
* @param buf object to be used as internal storage
* @stable ICU 2.0
*/
public ReplaceableString(StringBuffer buf) {
this.buf = buf;
}
/**
* Construct a new empty object.
* @stable ICU 2.0
*/
public ReplaceableString() {
buf = new StringBuffer();
}
/**
* Return the contents of this object as a String
.
* @return string contents of this object
* @stable ICU 2.0
*/
@Override
public String toString() {
return buf.toString();
}
/**
* Return a substring of the given string.
* @stable ICU 2.0
*/
public String substring(int start, int limit) {
return buf.substring(start, limit);
}
/**
* Return the number of characters contained in this object.
* Replaceable
API.
* @stable ICU 2.0
*/
@Override
public int length() {
return buf.length();
}
/**
* Return the character at the given position in this object.
* Replaceable
API.
* @param offset offset into the contents, from 0 to
* length()
- 1
* @stable ICU 2.0
*/
@Override
public char charAt(int offset) {
return buf.charAt(offset);
}
/**
* Return the 32-bit code point at the given 16-bit offset into
* the text. This assumes the text is stored as 16-bit code units
* with surrogate pairs intermixed. If the offset of a leading or
* trailing code unit of a surrogate pair is given, return the
* code point of the surrogate pair.
* @param offset an integer between 0 and length()
-1
* inclusive
* @return 32-bit code point of text at given offset
* @stable ICU 2.0
*/
@Override
public int char32At(int offset) {
return UTF16.charAt(buf, offset);
}
/**
* Copies characters from this object into the destination
* character array. The first character to be copied is at index
* srcStart
; the last character to be copied is at
* index srcLimit-1
(thus the total number of
* characters to be copied is srcLimit-srcStart
). The
* characters are copied into the subarray of dst
* starting at index dstStart
and ending at index
* dstStart + (srcLimit-srcStart) - 1
.
*
* @param srcStart the beginning index to copy, inclusive; 0
* <= start <= limit
.
* @param srcLimit the ending index to copy, exclusive;
* start <= limit <= length()
.
* @param dst the destination array.
* @param dstStart the start offset in the destination array.
* @stable ICU 2.0
*/
@Override
public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {
if (srcStart != srcLimit) {
buf.getChars(srcStart, srcLimit, dst, dstStart);
}
}
/**
* Replace zero or more characters with new characters.
* Replaceable
API.
* @param start the beginning index, inclusive; 0 <= start
* <= limit
.
* @param limit the ending index, exclusive; start <= limit
* <= length()
.
* @param text new text to replace characters start
to
* limit - 1
* @stable ICU 2.0
*/
@Override
public void replace(int start, int limit, String text) {
buf.replace(start, limit, text);
}
/**
* Replace a substring of this object with the given text.
* @param start the beginning index, inclusive; 0 <= start
* <= limit
.
* @param limit the ending index, exclusive; start <= limit
* <= length()
.
* @param chars the text to replace characters start
* to limit - 1
* @param charsStart the beginning index into chars
,
* inclusive; 0 <= start <= limit
.
* @param charsLen the number of characters of chars
.
* @stable ICU 2.0
*/
@Override
public void replace(int start, int limit, char[] chars,
int charsStart, int charsLen) {
buf.delete(start, limit);
buf.insert(start, chars, charsStart, charsLen);
}
/**
* Copy a substring of this object, retaining attribute (out-of-band)
* information. This method is used to duplicate or reorder substrings.
* The destination index must not overlap the source range.
*
* @param start the beginning index, inclusive; 0 <= start <=
* limit
.
* @param limit the ending index, exclusive; start <= limit <=
* length()
.
* @param dest the destination index. The characters from
* start..limit-1
will be copied to dest
.
* Implementations of this method may assume that dest <= start ||
* dest >= limit
.
* @stable ICU 2.0
*/
@Override
public void copy(int start, int limit, int dest) {
if (start == limit && start >= 0 && start <= buf.length()) {
return;
}
char[] text = new char[limit - start];
getChars(start, limit, text, 0);
replace(dest, dest, text, 0, limit - start);
}
/**
* Implements Replaceable
* @stable ICU 2.0
*/
@Override
public boolean hasMetaData() {
return false;
}
}