Skip to content

Commit aeae3eb

Browse files
committed
Rewrite addCodePointsToSet as toCodePointSet
by overloading with a version that takes a collection
1 parent 35f836a commit aeae3eb

4 files changed

Lines changed: 35 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
## [Unreleased]
55

6+
- Rewrote `addCodePointsToSet` as `toCodePointSet`
67
- Tweaked Help > Miscellaneous explanations
78
- Put Function column before Action column in table
89
- Moved 'Retract keyboard' explanation into table

app/src/main/java/io/github/yawnoc/strokeinput/StrokeInputService.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@ private List<String> computeCandidateList(final String strokeDigitSequence)
878878
exactMatchCandidateList = Collections.emptyList();
879879
}
880880

881-
final Set<Integer> prefixMatchCodePointSet = new HashSet<>();
882881
final Collection<String> prefixMatchCharactersCollection =
883882
charactersFromStrokeDigitSequence
884883
.subMap(
@@ -888,10 +887,7 @@ private List<String> computeCandidateList(final String strokeDigitSequence)
888887
.values();
889888

890889
final long addCodePointsStartMilliseconds = System.currentTimeMillis();
891-
for (final String characters : prefixMatchCharactersCollection)
892-
{
893-
Stringy.addCodePointsToSet(characters, prefixMatchCodePointSet);
894-
}
890+
final Set<Integer> prefixMatchCodePointSet = Stringy.toCodePointSet(prefixMatchCharactersCollection);
895891
final long addCodePointsEndMilliseconds = System.currentTimeMillis();
896892
if (BuildConfig.DEBUG)
897893
{

app/src/main/java/io/github/yawnoc/utilities/Stringy.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package io.github.yawnoc.utilities;
99

1010
import java.util.ArrayList;
11+
import java.util.Collection;
12+
import java.util.HashSet;
1113
import java.util.List;
1214
import java.util.Set;
1315
import java.util.regex.Pattern;
@@ -66,17 +68,36 @@ public static List<Integer> toCodePointList(final String string)
6668
}
6769

6870
/*
69-
Add the (unicode) code points of a string to a set
71+
Convert a string to a set of (unicode) code points.
7072
*/
71-
public static void addCodePointsToSet(final String string, final Set<Integer> set)
73+
public static Set<Integer> toCodePointSet(final String string)
7274
{
75+
final Set<Integer> codePointSet = new HashSet<>();
76+
7377
final int charCount = string.length();
7478
for (int charIndex = 0; charIndex < charCount;)
7579
{
7680
final int codePoint = string.codePointAt(charIndex);
77-
set.add(codePoint);
81+
codePointSet.add(codePoint);
7882
charIndex += Character.charCount(codePoint);
7983
}
84+
85+
return codePointSet;
86+
}
87+
88+
/*
89+
Convert a collection of strings to a set of (unicode) code points.
90+
*/
91+
public static Set<Integer> toCodePointSet(final Collection<String> stringCollection)
92+
{
93+
final Set<Integer> codePointSet = new HashSet<>();
94+
95+
for (final String string : stringCollection)
96+
{
97+
codePointSet.addAll(toCodePointSet(string));
98+
}
99+
100+
return codePointSet;
80101
}
81102

82103
/*

app/src/test/java/io/github/yawnoc/utilities/StringyTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import org.junit.Test;
1313

1414
import java.util.Arrays;
15-
import java.util.Collections;
1615
import java.util.HashSet;
1716
import java.util.List;
18-
import java.util.Set;
1917
import java.util.stream.Collectors;
2018
import java.util.stream.IntStream;
2119

@@ -87,18 +85,17 @@ public void toCodePointList_isCorrect()
8785
}
8886

8987
@Test
90-
public void addCodePointsToSet_isCorrect()
88+
public void toCodePointSet_isCorrect()
9189
{
92-
final int SOME_CODE_POINT = 0x1234;
93-
final Set<Integer> commonsCodePointSet = new HashSet<>(Collections.singletonList(SOME_CODE_POINT));
94-
Stringy.addCodePointsToSet("天下為公", commonsCodePointSet);
95-
assertEquals(commonsCodePointSet, new HashSet<>(Arrays.asList(SOME_CODE_POINT, 0x5929, 0x4E0B, 0x70BA, 0x516C)));
96-
97-
final Set<Integer> asciiFullCodePointSet = new HashSet<>();
98-
Stringy.addCodePointsToSet(ASCII_FULL_STRING, asciiFullCodePointSet);
99-
assertEquals(asciiFullCodePointSet, new HashSet<>(ASCII_CODE_POINT_RANGE));
90+
assertEquals(Stringy.toCodePointSet(ASCII_FULL_STRING), new HashSet<>(ASCII_CODE_POINT_RANGE));
91+
assertEquals(Stringy.toCodePointSet("天下為公"), new HashSet<>(Arrays.asList(0x5929, 0x4E0B, 0x70BA, 0x516C)));
92+
93+
assertEquals(
94+
Stringy.toCodePointSet(Arrays.asList("ABC", "天地玄黃", "BCD")),
95+
new HashSet<>(Arrays.asList(0x41, 0x42, 0x43, 0x5929, 0x5730, 0x7384, 0x9EC3, 0x44))
96+
);
10097
}
101-
98+
10299
@Test
103100
public void toString_isCorrect()
104101
{

0 commit comments

Comments
 (0)