diff options
Diffstat (limited to 'common/src')
5 files changed, 108 insertions, 16 deletions
diff --git a/common/src/com/android/inputmethod/latin/common/CodePointUtils.java b/common/src/com/android/inputmethod/latin/common/CodePointUtils.java index 592da5c1f..ec59de850 100644 --- a/common/src/com/android/inputmethod/latin/common/CodePointUtils.java +++ b/common/src/com/android/inputmethod/latin/common/CodePointUtils.java @@ -20,6 +20,8 @@ import com.android.inputmethod.annotations.UsedForTesting; import java.util.Random; +import javax.annotation.Nonnull; + // Utility methods related with code points used for tests. // TODO: Figure out where this class should be. @UsedForTesting @@ -65,17 +67,23 @@ public class CodePointUtils { }; @UsedForTesting - public static int[] generateCodePointSet(final int codePointSetSize, final Random random) { + @Nonnull + public static int[] generateCodePointSet(final int codePointSetSize, + @Nonnull final Random random) { final int[] codePointSet = new int[codePointSetSize]; for (int i = codePointSet.length - 1; i >= 0; ) { final int r = Math.abs(random.nextInt()); - if (r < 0) continue; + if (r < 0) { + continue; + } // Don't insert 0~0x20, but insert any other code point. // Code points are in the range 0~0x10FFFF. final int candidateCodePoint = 0x20 + r % (Character.MAX_CODE_POINT - 0x20); // Code points between MIN_ and MAX_SURROGATE are not valid on their own. if (candidateCodePoint >= Character.MIN_SURROGATE - && candidateCodePoint <= Character.MAX_SURROGATE) continue; + && candidateCodePoint <= Character.MAX_SURROGATE) { + continue; + } codePointSet[i] = candidateCodePoint; --i; } @@ -86,8 +94,10 @@ public class CodePointUtils { * Generates a random word. */ @UsedForTesting - public static String generateWord(final Random random, final int[] codePointSet) { - StringBuilder builder = new StringBuilder(); + @Nonnull + public static String generateWord(@Nonnull final Random random, + @Nonnull final int[] codePointSet) { + final StringBuilder builder = new StringBuilder(); // 8 * 4 = 32 chars max, but we do it the following way so as to bias the random toward // longer words. This should be closer to natural language, and more importantly, it will // exercise the algorithms in dicttool much more. diff --git a/common/src/com/android/inputmethod/latin/common/ComposedData.java b/common/src/com/android/inputmethod/latin/common/ComposedData.java new file mode 100644 index 000000000..7f0966050 --- /dev/null +++ b/common/src/com/android/inputmethod/latin/common/ComposedData.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.common; + +import javax.annotation.Nonnull; + +/** + * An immutable class that encapsulates a snapshot of word composition data. + */ +public class ComposedData { + @Nonnull + public final InputPointers mInputPointers; + public final boolean mIsBatchMode; + @Nonnull + public final String mTypedWord; + + public ComposedData(@Nonnull final InputPointers inputPointers, final boolean isBatchMode, + @Nonnull final String typedWord) { + mInputPointers = inputPointers; + mIsBatchMode = isBatchMode; + mTypedWord = typedWord; + } + + /** + * Copy the code points in the typed word to a destination array of ints. + * + * If the array is too small to hold the code points in the typed word, nothing is copied and + * -1 is returned. + * + * @param destination the array of ints. + * @return the number of copied code points. + */ + public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( + @Nonnull final int[] destination) { + // lastIndex is exclusive + final int lastIndex = mTypedWord.length() + - StringUtils.getTrailingSingleQuotesCount(mTypedWord); + if (lastIndex <= 0) { + // The string is empty or contains only single quotes. + return 0; + } + + // The following function counts the number of code points in the text range which begins + // at index 0 and extends to the character at lastIndex. + final int codePointSize = Character.codePointCount(mTypedWord, 0, lastIndex); + if (codePointSize > destination.length) { + return -1; + } + return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWord, 0, + lastIndex, true /* downCase */); + } +} diff --git a/common/src/com/android/inputmethod/latin/common/Constants.java b/common/src/com/android/inputmethod/latin/common/Constants.java index 8f4a1e50d..abc377a84 100644 --- a/common/src/com/android/inputmethod/latin/common/Constants.java +++ b/common/src/com/android/inputmethod/latin/common/Constants.java @@ -18,6 +18,8 @@ package com.android.inputmethod.latin.common; import com.android.inputmethod.annotations.UsedForTesting; +import javax.annotation.Nonnull; + public final class Constants { public static final class Color { /** @@ -259,6 +261,7 @@ public final class Constants { return code >= CODE_SPACE; } + @Nonnull public static String printableCode(final int code) { switch (code) { case CODE_SHIFT: return "shift"; @@ -286,7 +289,8 @@ public final class Constants { } } - public static String printableCodes(final int[] codes) { + @Nonnull + public static String printableCodes(@Nonnull final int[] codes) { final StringBuilder sb = new StringBuilder(); boolean addDelimiter = false; for (final int code : codes) { diff --git a/common/src/com/android/inputmethod/latin/common/InputPointers.java b/common/src/com/android/inputmethod/latin/common/InputPointers.java index 40131aca4..7beee1536 100644 --- a/common/src/com/android/inputmethod/latin/common/InputPointers.java +++ b/common/src/com/android/inputmethod/latin/common/InputPointers.java @@ -18,6 +18,8 @@ package com.android.inputmethod.latin.common; import com.android.inputmethod.annotations.UsedForTesting; +import javax.annotation.Nonnull; + // TODO: This class is not thread-safe. public final class InputPointers { private static final boolean DEBUG_TIME = false; @@ -28,7 +30,7 @@ public final class InputPointers { private final ResizableIntArray mPointerIds; private final ResizableIntArray mTimes; - public InputPointers(int defaultCapacity) { + public InputPointers(final int defaultCapacity) { mDefaultCapacity = defaultCapacity; mXCoordinates = new ResizableIntArray(defaultCapacity); mYCoordinates = new ResizableIntArray(defaultCapacity); @@ -51,7 +53,8 @@ public final class InputPointers { mTimes.fill(lastTime, fromIndex, fillLength); } - public void addPointerAt(int index, int x, int y, int pointerId, int time) { + public void addPointerAt(final int index, final int x, final int y, final int pointerId, + final int time) { mXCoordinates.addAt(index, x); mYCoordinates.addAt(index, y); mPointerIds.addAt(index, pointerId); @@ -62,21 +65,21 @@ public final class InputPointers { } @UsedForTesting - public void addPointer(int x, int y, int pointerId, int time) { + public void addPointer(final int x, final int y, final int pointerId, final int time) { mXCoordinates.add(x); mYCoordinates.add(y); mPointerIds.add(pointerId); mTimes.add(time); } - public void set(InputPointers ip) { + public void set(@Nonnull final InputPointers ip) { mXCoordinates.set(ip.mXCoordinates); mYCoordinates.set(ip.mYCoordinates); mPointerIds.set(ip.mPointerIds); mTimes.set(ip.mTimes); } - public void copy(InputPointers ip) { + public void copy(@Nonnull final InputPointers ip) { mXCoordinates.copy(ip.mXCoordinates); mYCoordinates.copy(ip.mYCoordinates); mPointerIds.copy(ip.mPointerIds); @@ -93,8 +96,9 @@ public final class InputPointers { * @param startPos the starting index of the data in {@code times} and etc. * @param length the number of data to be appended. */ - public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates, - ResizableIntArray yCoordinates, int startPos, int length) { + public void append(final int pointerId, @Nonnull final ResizableIntArray times, + @Nonnull final ResizableIntArray xCoordinates, + @Nonnull final ResizableIntArray yCoordinates, final int startPos, final int length) { if (length == 0) { return; } @@ -127,14 +131,17 @@ public final class InputPointers { return mXCoordinates.getLength(); } + @Nonnull public int[] getXCoordinates() { return mXCoordinates.getPrimitiveArray(); } + @Nonnull public int[] getYCoordinates() { return mYCoordinates.getPrimitiveArray(); } + @Nonnull public int[] getPointerIds() { return mPointerIds.getPrimitiveArray(); } @@ -145,6 +152,7 @@ public final class InputPointers { * @return The time each point was registered, in milliseconds, relative to the first event in * the sequence. */ + @Nonnull public int[] getTimes() { return mTimes.getPrimitiveArray(); } diff --git a/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java b/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java index ea23d8a33..340abb23e 100644 --- a/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java +++ b/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java @@ -18,8 +18,11 @@ package com.android.inputmethod.latin.common; import java.util.Arrays; +import javax.annotation.Nonnull; + // TODO: This class is not thread-safe. public final class ResizableIntArray { + @Nonnull private int[] mArray; private int mLength; @@ -89,17 +92,18 @@ public final class ResizableIntArray { mLength = 0; } + @Nonnull public int[] getPrimitiveArray() { return mArray; } - public void set(final ResizableIntArray ip) { + public void set(@Nonnull final ResizableIntArray ip) { // TODO: Implement primitive array pool. mArray = ip.mArray; mLength = ip.mLength; } - public void copy(final ResizableIntArray ip) { + public void copy(@Nonnull final ResizableIntArray ip) { final int newCapacity = calculateCapacity(ip.mLength); if (newCapacity > 0) { // TODO: Implement primitive array pool. @@ -109,7 +113,7 @@ public final class ResizableIntArray { mLength = ip.mLength; } - public void append(final ResizableIntArray src, final int startPos, final int length) { + public void append(@Nonnull final ResizableIntArray src, final int startPos, final int length) { if (length == 0) { return; } |