Skip to content

Commit 820803d

Browse files
committed
adjust construct cell api
1 parent 9a82de5 commit 820803d

File tree

2 files changed

+86
-37
lines changed

2 files changed

+86
-37
lines changed

src/main/java/com/uber/h3core/H3Core.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ public boolean isValidIndex(String h3Address) {
124124
}
125125

126126
/** Construct a cell index from component parts */
127-
public long constructCell(int res, int baseCellNumber, List<Integer> digits) {
127+
public long constructCell(int baseCellNumber, List<Integer> digits, int res) {
128128
int[] digitsArray = digits.stream().mapToInt(Integer::intValue).toArray();
129-
if (digitsArray.length < res) {
129+
if (digitsArray.length != res) {
130130
throw new IllegalArgumentException(
131131
String.format(
132-
"Number of provided digits is too few, must be at least %d, was %d",
133-
res - 1, digitsArray.length));
132+
"Number of provided digits is incorrect, must be %d, was %d",
133+
res, digitsArray.length));
134134
}
135135
if (digitsArray.length > 15) {
136136
throw new IllegalArgumentException(
@@ -142,8 +142,18 @@ public long constructCell(int res, int baseCellNumber, List<Integer> digits) {
142142
}
143143

144144
/** Construct a cell index from component parts */
145-
public String constructCellAddress(int res, int baseCellNumber, List<Integer> digits) {
146-
return h3ToString(constructCell(res, baseCellNumber, digits));
145+
public long constructCell(int baseCellNumber, List<Integer> digits) {
146+
return constructCell(baseCellNumber, digits, digits.size());
147+
}
148+
149+
/** Construct a cell index from component parts */
150+
public String constructCellAddress(int baseCellNumber, List<Integer> digits) {
151+
return h3ToString(constructCell(baseCellNumber, digits, digits.size()));
152+
}
153+
154+
/** Construct a cell index from component parts */
155+
public String constructCellAddress(int baseCellNumber, List<Integer> digits, int res) {
156+
return h3ToString(constructCell(baseCellNumber, digits, res));
147157
}
148158

149159
/** Returns the base cell number for this index. */

src/test/java/com/uber/h3core/TestInspection.java

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,96 +77,135 @@ void getIndexDigit() {
7777

7878
@Test
7979
void constructCell() {
80-
assertEquals(h3.constructCell(0, 0, ImmutableList.of()), 576495936675512319L);
80+
assertEquals(h3.constructCell(0, ImmutableList.of()), 576495936675512319L);
81+
assertEquals(h3.constructCell(0, ImmutableList.of(), 0), 576495936675512319L);
8182
assertThrows(
82-
H3Exception.class,
83+
IllegalArgumentException.class,
8384
() ->
8485
// Invalid res
85-
h3.constructCell(-1, 0, ImmutableList.of()));
86+
h3.constructCell(0, ImmutableList.of(), -1));
87+
assertThrows(
88+
H3Exception.class,
89+
() ->
90+
// Invalid base cell
91+
h3.constructCell(-1, ImmutableList.of()));
8692
assertThrows(
8793
H3Exception.class,
8894
() ->
8995
// Invalid base cell
90-
h3.constructCell(0, -1, ImmutableList.of()));
96+
h3.constructCell(-1, ImmutableList.of(), 0));
9197
assertThrows(
9298
IllegalArgumentException.class,
9399
() ->
94100
// Invalid digit list
95-
h3.constructCell(1, 5, ImmutableList.of()));
96-
// Invalid digit list -- ignored
97-
h3.constructCell(1, 5, ImmutableList.of(1, 2));
98-
assertEquals(h3.constructCell(1, 5, ImmutableList.of(1)), 581149069884260351L);
101+
h3.constructCell(5, ImmutableList.of(), 1));
102+
assertThrows(
103+
IllegalArgumentException.class,
104+
() ->
105+
// Invalid digit list
106+
h3.constructCell(5, ImmutableList.of(1, 2), 1));
107+
assertEquals(h3.constructCell(5, ImmutableList.of(1)), 581149069884260351L);
108+
assertEquals(h3.constructCell(5, ImmutableList.of(1), 1), 581149069884260351L);
109+
assertThrows(
110+
H3Exception.class,
111+
() ->
112+
// Deleted subsequence
113+
h3.constructCell(4, ImmutableList.of(1)));
99114
assertThrows(
100115
H3Exception.class,
101116
() ->
102117
// Deleted subsequence
103-
h3.constructCell(1, 4, ImmutableList.of(1)));
118+
h3.constructCell(4, ImmutableList.of(1), 1));
104119
assertThrows(
105120
IllegalArgumentException.class,
106121
() ->
107122
// Too few digits
108-
h3.constructCell(7, 20, ImmutableList.of(1, 2, 3, 4, 5, 6)));
123+
h3.constructCell(20, ImmutableList.of(1, 2, 3, 4, 5, 6), 7));
109124
assertEquals(
110-
h3.constructCell(15, 100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3)),
125+
h3.constructCell(100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3)),
126+
0x8fc8539714e5c53L);
127+
assertEquals(
128+
h3.constructCell(100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3), 15),
111129
0x8fc8539714e5c53L);
112130
assertThrows(
113131
IllegalArgumentException.class,
114132
() ->
115133
// Too many digits
116134
h3.constructCell(
117-
15, 20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0)));
135+
20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0)));
136+
assertThrows(
137+
IllegalArgumentException.class,
138+
() ->
139+
// Too many digits
140+
h3.constructCell(
141+
20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0), 15));
118142
}
119143

120144
@Test
121145
void constructCellAddress() {
122-
assertEquals(h3.constructCellAddress(0, 0, ImmutableList.of()), "8001fffffffffff");
146+
assertEquals(h3.constructCellAddress(0, ImmutableList.of()), "8001fffffffffff");
147+
assertEquals(h3.constructCellAddress(0, ImmutableList.of(), 0), "8001fffffffffff");
123148
assertThrows(
124-
H3Exception.class,
149+
IllegalArgumentException.class,
125150
() ->
126151
// Invalid res
127-
h3.constructCellAddress(-1, 0, ImmutableList.of()));
152+
h3.constructCellAddress(0, ImmutableList.of(), -1));
153+
assertThrows(
154+
H3Exception.class,
155+
() ->
156+
// Invalid base cell
157+
h3.constructCellAddress(-1, ImmutableList.of()));
128158
assertThrows(
129159
H3Exception.class,
130160
() ->
131161
// Invalid base cell
132-
h3.constructCellAddress(0, -1, ImmutableList.of()));
162+
h3.constructCellAddress(-1, ImmutableList.of(), 0));
133163
assertThrows(
134164
IllegalArgumentException.class,
135165
() ->
136166
// Invalid digit list
137-
h3.constructCellAddress(1, 5, ImmutableList.of()));
138-
// Invalid digit list -- ignored
139-
h3.constructCellAddress(1, 5, ImmutableList.of(1, 2));
140-
assertEquals(h3.constructCellAddress(1, 5, ImmutableList.of(1)), "810a7ffffffffff");
167+
h3.constructCellAddress(5, ImmutableList.of(), 1));
168+
assertThrows(
169+
IllegalArgumentException.class,
170+
() ->
171+
// Invalid digit list
172+
h3.constructCellAddress(5, ImmutableList.of(1, 2), 1));
173+
assertEquals(h3.constructCellAddress(5, ImmutableList.of(1)), "810a7ffffffffff");
174+
assertEquals(h3.constructCellAddress(5, ImmutableList.of(1), 1), "810a7ffffffffff");
175+
assertThrows(
176+
H3Exception.class,
177+
() ->
178+
// Deleted subsequence
179+
h3.constructCellAddress(4, ImmutableList.of(1)));
141180
assertThrows(
142181
H3Exception.class,
143182
() ->
144183
// Deleted subsequence
145-
h3.constructCellAddress(1, 4, ImmutableList.of(1)));
184+
h3.constructCellAddress(4, ImmutableList.of(1), 1));
146185
assertThrows(
147186
IllegalArgumentException.class,
148187
() ->
149188
// Too few digits
150-
h3.constructCellAddress(7, 20, ImmutableList.of(1, 2, 3, 4, 5, 6)));
189+
h3.constructCellAddress(20, ImmutableList.of(1, 2, 3, 4, 5, 6), 7));
190+
assertEquals(
191+
h3.constructCellAddress(100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3)),
192+
"8fc8539714e5c53");
151193
assertEquals(
152194
h3.constructCellAddress(
153-
15, 100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3)),
195+
100, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3), 15),
154196
"8fc8539714e5c53");
155197
assertThrows(
156198
IllegalArgumentException.class,
157199
() ->
158200
// Too many digits
159201
h3.constructCellAddress(
160-
15, 20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0)));
161-
}
162-
163-
@Test
164-
void constructCellErrors() {
202+
20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0)));
165203
assertThrows(
166-
H3Exception.class,
204+
IllegalArgumentException.class,
167205
() ->
168-
// Invalid index
169-
h3.constructCell(1, 4, ImmutableList.of(1)));
206+
// Too many digits
207+
h3.constructCellAddress(
208+
20, ImmutableList.of(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 0, 0, 0), 15));
170209
}
171210

172211
@Test

0 commit comments

Comments
 (0)