• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.text;
18 
19 /**
20  * An AlteredCharSequence is a CharSequence that is largely mirrored from
21  * another CharSequence, except that a specified range of characters are
22  * mirrored from a different char array instead.
23  *
24  * @deprecated The functionality this class offers is easily implemented outside the framework.
25  */
26 @Deprecated
27 public class AlteredCharSequence
28 implements CharSequence, GetChars
29 {
30     /**
31      * Create an AlteredCharSequence whose text (and possibly spans)
32      * are mirrored from <code>source</code>, except that the range of
33      * offsets <code>substart</code> inclusive to <code>subend</code> exclusive
34      * are mirrored instead from <code>sub</code>, beginning at offset 0.
35      */
make(CharSequence source, char[] sub, int substart, int subend)36     public static AlteredCharSequence make(CharSequence source, char[] sub,
37                                            int substart, int subend) {
38         if (source instanceof Spanned)
39             return new AlteredSpanned(source, sub, substart, subend);
40         else
41             return new AlteredCharSequence(source, sub, substart, subend);
42     }
43 
AlteredCharSequence(CharSequence source, char[] sub, int substart, int subend)44     private AlteredCharSequence(CharSequence source, char[] sub,
45                                 int substart, int subend) {
46         mSource = source;
47         mChars = sub;
48         mStart = substart;
49         mEnd = subend;
50     }
51 
update(char[] sub, int substart, int subend)52     /* package */ void update(char[] sub, int substart, int subend) {
53         mChars = sub;
54         mStart = substart;
55         mEnd = subend;
56     }
57 
58     private static class AlteredSpanned
59     extends AlteredCharSequence
60     implements Spanned
61     {
AlteredSpanned(CharSequence source, char[] sub, int substart, int subend)62         private AlteredSpanned(CharSequence source, char[] sub,
63                                int substart, int subend) {
64             super(source, sub, substart, subend);
65             mSpanned = (Spanned) source;
66         }
67 
getSpans(int start, int end, Class<T> kind)68         public <T> T[] getSpans(int start, int end, Class<T> kind) {
69             return mSpanned.getSpans(start, end, kind);
70         }
71 
getSpanStart(Object span)72         public int getSpanStart(Object span) {
73             return mSpanned.getSpanStart(span);
74         }
75 
getSpanEnd(Object span)76         public int getSpanEnd(Object span) {
77             return mSpanned.getSpanEnd(span);
78         }
79 
getSpanFlags(Object span)80         public int getSpanFlags(Object span) {
81             return mSpanned.getSpanFlags(span);
82         }
83 
nextSpanTransition(int start, int end, Class kind)84         public int nextSpanTransition(int start, int end, Class kind) {
85             return mSpanned.nextSpanTransition(start, end, kind);
86         }
87 
88         private Spanned mSpanned;
89     }
90 
charAt(int off)91     public char charAt(int off) {
92         if (off >= mStart && off < mEnd)
93             return mChars[off - mStart];
94         else
95             return mSource.charAt(off);
96     }
97 
length()98     public int length() {
99         return mSource.length();
100     }
101 
subSequence(int start, int end)102     public CharSequence subSequence(int start, int end) {
103         return AlteredCharSequence.make(mSource.subSequence(start, end),
104                                         mChars, mStart - start, mEnd - start);
105     }
106 
getChars(int start, int end, char[] dest, int off)107     public void getChars(int start, int end, char[] dest, int off) {
108         TextUtils.getChars(mSource, start, end, dest, off);
109 
110         start = Math.max(mStart, start);
111         end = Math.min(mEnd, end);
112 
113         if (start > end)
114             System.arraycopy(mChars, start - mStart, dest, off, end - start);
115     }
116 
toString()117     public String toString() {
118         int len = length();
119 
120         char[] ret = new char[len];
121         getChars(0, len, ret, 0);
122         return String.valueOf(ret);
123     }
124 
125     private int mStart;
126     private int mEnd;
127     private char[] mChars;
128     private CharSequence mSource;
129 }
130