Home | History | Annotate | Download | only in text
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1996-2016, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.text;
     10 
     11 /**
     12  * <code>Replaceable</code> is an interface representing a
     13  * string of characters that supports the replacement of a range of
     14  * itself with a new string of characters.  It is used by APIs that
     15  * change a piece of text while retaining metadata.  Metadata is data
     16  * other than the Unicode characters returned by char32At().  One
     17  * example of metadata is style attributes; another is an edit
     18  * history, marking each character with an author and revision number.
     19  *
     20  * <p>An implicit aspect of the <code>Replaceable</code> API is that
     21  * during a replace operation, new characters take on the metadata of
     22  * the old characters.  For example, if the string "the <b>bold</b>
     23  * font" has range (4, 8) replaced with "strong", then it becomes "the
     24  * <b>strong</b> font".
     25  *
     26  * <p><code>Replaceable</code> specifies ranges using a start
     27  * offset and a limit offset.  The range of characters thus specified
     28  * includes the characters at offset start..limit-1.  That is, the
     29  * start offset is inclusive, and the limit offset is exclusive.
     30  *
     31  * <p><code>Replaceable</code> also includes API to access characters
     32  * in the string: <code>length()</code>, <code>charAt()</code>,
     33  * <code>char32At()</code>, and <code>extractBetween()</code>.
     34  *
     35  * <p>For a subclass to support metadata, typical behavior of
     36  * <code>replace()</code> is the following:
     37  * <ul>
     38  *   <li>Set the metadata of the new text to the metadata of the first
     39  *   character replaced</li>
     40  *   <li>If no characters are replaced, use the metadata of the
     41  *   previous character</li>
     42  *   <li>If there is no previous character (i.e. start == 0), use the
     43  *   following character</li>
     44  *   <li>If there is no following character (i.e. the replaceable was
     45  *   empty), use default metadata<br>
     46  *   <li>If the code point U+FFFF is seen, it should be interpreted as
     47  *   a special marker having no metadata<li>
     48  *   </li>
     49  * </ul>
     50  * If this is not the behavior, the subclass should document any differences.
     51  *
     52  * @author Alan Liu
     53  * @stable ICU 2.0
     54  */
     55 public interface Replaceable {
     56     /**
     57      * Returns the number of 16-bit code units in the text.
     58      * @return number of 16-bit code units in text
     59      * @stable ICU 2.0
     60      */
     61     int length();
     62 
     63     /**
     64      * Returns the 16-bit code unit at the given offset into the text.
     65      * @param offset an integer between 0 and <code>length()</code>-1
     66      * inclusive
     67      * @return 16-bit code unit of text at given offset
     68      * @stable ICU 2.0
     69      */
     70     char charAt(int offset);
     71 
     72     /**
     73      * Returns the 32-bit code point at the given 16-bit offset into
     74      * the text.  This assumes the text is stored as 16-bit code units
     75      * with surrogate pairs intermixed.  If the offset of a leading or
     76      * trailing code unit of a surrogate pair is given, return the
     77      * code point of the surrogate pair.
     78      *
     79      * <p>Most subclasses can return
     80      * <code>com.ibm.icu.text.UTF16.charAt(this, offset)</code>.
     81      * @param offset an integer between 0 and <code>length()</code>-1
     82      * inclusive
     83      * @return 32-bit code point of text at given offset
     84      * @stable ICU 2.0
     85      */
     86     int char32At(int offset);
     87 
     88     /**
     89      * Copies characters from this object into the destination
     90      * character array.  The first character to be copied is at index
     91      * <code>srcStart</code>; the last character to be copied is at
     92      * index <code>srcLimit-1</code> (thus the total number of
     93      * characters to be copied is <code>srcLimit-srcStart</code>). The
     94      * characters are copied into the subarray of <code>dst</code>
     95      * starting at index <code>dstStart</code> and ending at index
     96      * <code>dstStart + (srcLimit-srcStart) - 1</code>.
     97      *
     98      * @param srcStart the beginning index to copy, inclusive; <code>0
     99      * &lt;= start &lt;= limit</code>.
    100      * @param srcLimit the ending index to copy, exclusive;
    101      * <code>start &lt;= limit &lt;= length()</code>.
    102      * @param dst the destination array.
    103      * @param dstStart the start offset in the destination array.
    104      * @stable ICU 2.0
    105      */
    106     void getChars(int srcStart, int srcLimit, char dst[], int dstStart);
    107 
    108     /**
    109      * Replaces a substring of this object with the given text.
    110      *
    111      * <p>Subclasses must ensure that if the text between start and
    112      * limit is equal to the replacement text, that replace has no
    113      * effect. That is, any metadata
    114      * should be unaffected. In addition, subclasses are encouraged to
    115      * check for initial and trailing identical characters, and make a
    116      * smaller replacement if possible. This will preserve as much
    117      * metadata as possible.
    118      * @param start the beginning index, inclusive; <code>0 &lt;= start
    119      * &lt;= limit</code>.
    120      * @param limit the ending index, exclusive; <code>start &lt;= limit
    121      * &lt;= length()</code>.
    122      * @param text the text to replace characters <code>start</code>
    123      * to <code>limit - 1</code>
    124      * @stable ICU 2.0
    125      */
    126     void replace(int start, int limit, String text);
    127 
    128     /**
    129      * Replaces a substring of this object with the given text.
    130      *
    131      * <p>Subclasses must ensure that if the text between start and
    132      * limit is equal to the replacement text, that replace has no
    133      * effect. That is, any metadata
    134      * should be unaffected. In addition, subclasses are encouraged to
    135      * check for initial and trailing identical characters, and make a
    136      * smaller replacement if possible. This will preserve as much
    137      * metadata as possible.
    138      * @param start the beginning index, inclusive; <code>0 &lt;= start
    139      * &lt;= limit</code>.
    140      * @param limit the ending index, exclusive; <code>start &lt;= limit
    141      * &lt;= length()</code>.
    142      * @param chars the text to replace characters <code>start</code>
    143      * to <code>limit - 1</code>
    144      * @param charsStart the beginning index into <code>chars</code>,
    145      * inclusive; <code>0 &lt;= start &lt;= limit</code>.
    146      * @param charsLen the number of characters of <code>chars</code>.
    147      * @stable ICU 2.0
    148      */
    149     void replace(int start, int limit, char[] chars,
    150                  int charsStart, int charsLen);
    151     // Note: We use length rather than limit to conform to StringBuffer
    152     // and System.arraycopy.
    153 
    154     /**
    155      * Copies a substring of this object, retaining metadata.
    156      * This method is used to duplicate or reorder substrings.
    157      * The destination index must not overlap the source range.
    158      * If <code>hasMetaData()</code> returns false, subclasses
    159      * may use the naive implementation:
    160      *
    161      * <pre> char[] text = new char[limit - start];
    162      * getChars(start, limit, text, 0);
    163      * replace(dest, dest, text, 0, limit - start);</pre>
    164      *
    165      * @param start the beginning index, inclusive; <code>0 &lt;= start &lt;=
    166      * limit</code>.
    167      * @param limit the ending index, exclusive; <code>start &lt;= limit &lt;=
    168      * length()</code>.
    169      * @param dest the destination index.  The characters from
    170      * <code>start..limit-1</code> will be copied to <code>dest</code>.
    171      * Implementations of this method may assume that <code>dest &lt;= start ||
    172      * dest &gt;= limit</code>.
    173      * @stable ICU 2.0
    174      */
    175     void copy(int start, int limit, int dest);
    176 
    177     /**R
    178      * Returns true if this object contains metadata.  If a
    179      * Replaceable object has metadata, calls to the Replaceable API
    180      * must be made so as to preserve metadata.  If it does not, calls
    181      * to the Replaceable API may be optimized to improve performance.
    182      * @return true if this object contains metadata
    183      * @stable ICU 2.2
    184      */
    185     boolean hasMetaData();
    186 }
    187