Skip to content

Commit 9eeb1c0

Browse files
authored
Java goody for generating integer's digits (#362)
Closes #177.
1 parent 0ad30de commit 9eeb1c0

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package digits_int;
2+
3+
import iterable_int_stream.IterableIntStream;
4+
import java.util.stream.IntStream;
5+
6+
public final class AP {
7+
8+
/**
9+
* Get the digits in the given integer. The digits are returned as an
10+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
11+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned.
12+
* @param number Integer of interest.
13+
* @return The {@link IterableIntStream stream} of digits.
14+
*/
15+
public static IterableIntStream digits(int number) {
16+
return digits(number, 10);
17+
}
18+
19+
/**
20+
* Get the digits in the given integer for the given radix value. The digits are returned as an
21+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
22+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned with radix = 10.
23+
* @param number Integer of interest.
24+
* @param radix Radix value of interest.
25+
* @return The {@link IterableIntStream stream} of digits.
26+
*/
27+
public static IterableIntStream digits(int number, int radix) {
28+
if (number < 0) throw new IllegalArgumentException(
29+
"Please provide a positive integer."
30+
);
31+
32+
if (radix < 2) throw new IllegalArgumentException(
33+
"Please provide a radix greater than or equal to 2."
34+
);
35+
36+
return IterableIntStream.from(
37+
number == 0
38+
? IntStream.of(0)
39+
: IntStream.iterate(number, num -> num != 0, num -> num / radix).map(
40+
num -> num % radix
41+
)
42+
);
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package digits_int;
2+
3+
import static digits_int.AP.digits;
4+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
9+
import iterable_int_stream.IterableIntStream;
10+
import java.util.List;
11+
import java.util.stream.IntStream;
12+
import org.junit.jupiter.api.Test;
13+
14+
class DigitsIntTest {
15+
16+
@Test
17+
public void withIntegerGreaterThanZero() {
18+
assertIterableEquals(List.of(4, 8, 9, 4), digits(4984));
19+
}
20+
21+
@Test
22+
public void withZero() {
23+
assertIterableEquals(List.of(0), digits(0));
24+
}
25+
26+
@Test
27+
public void withRadixOtherThan10() {
28+
assertIterableEquals(List.of(4, 6, 6, 0, 5), digits(12345, 7));
29+
}
30+
31+
@Test
32+
public void withRadixLessThan2() {
33+
assertThrows(IllegalArgumentException.class, () -> digits(12345, 1));
34+
}
35+
36+
@Test
37+
public void withNegativeInteger() {
38+
assertThrows(IllegalArgumentException.class, () -> digits(-145));
39+
}
40+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "digits(int)"
3+
}

workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,105 @@ final class VirtualList<T> extends AbstractList<T> {
391391
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
392392
`;
393393

394+
exports[`App can equip single goody: Java digits(int) 1`] = `
395+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
396+
// Adventure Pack commit fake-commit-hash
397+
// Running at: https://example.com/
398+
399+
import java.lang.reflect.Method;
400+
import java.lang.reflect.Proxy;
401+
import java.util.Iterator;
402+
import java.util.Spliterator;
403+
import java.util.function.Consumer;
404+
import java.util.function.IntConsumer;
405+
import java.util.stream.IntStream;
406+
import java.util.stream.Stream;
407+
import java.util.stream.StreamSupport;
408+
409+
@SuppressWarnings("overloads")
410+
interface IterableIntStream extends Iterable<Integer>, IntStream {
411+
@Override
412+
public Spliterator.OfInt spliterator();
413+
414+
public static IterableIntStream from(final Iterable<Integer> iterable) {
415+
return from(iterable.spliterator());
416+
}
417+
418+
public static IterableIntStream from(final Iterator<Integer> iterator) {
419+
return from(() -> iterator);
420+
}
421+
422+
public static IterableIntStream from(final Spliterator<Integer> spliterator) {
423+
return from(StreamSupport.stream(spliterator, false));
424+
}
425+
426+
public static IterableIntStream from(final Spliterator.OfInt spliterator) {
427+
return from(StreamSupport.intStream(spliterator, false));
428+
}
429+
430+
public static IterableIntStream from(final Stream<Integer> stream) {
431+
return from(stream.mapToInt(Integer::intValue));
432+
}
433+
434+
public static IterableIntStream from(final IntStream stream) {
435+
@SuppressWarnings("unchecked")
436+
var proxy = (IterableIntStream) Proxy.newProxyInstance(
437+
ClassLoader.getSystemClassLoader(),
438+
new Class<?>[] { IterableIntStream.class },
439+
(Object _proxy, Method method, Object[] args) ->
440+
IntStream.class.getMethod(
441+
method.getName(),
442+
method.getParameterTypes()
443+
).invoke(stream, args)
444+
);
445+
return proxy;
446+
}
447+
}
448+
449+
final class AP {
450+
private AP() {}
451+
452+
/**
453+
* Get the digits in the given integer. The digits are returned as an
454+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
455+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned.
456+
* @param number Integer of interest.
457+
* @return The {@link IterableIntStream stream} of digits.
458+
*/
459+
public static IterableIntStream digits(int number) {
460+
return digits(number, 10);
461+
}
462+
463+
/**
464+
* Get the digits in the given integer for the given radix value. The digits are returned as an
465+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
466+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned with radix = 10.
467+
* @param number Integer of interest.
468+
* @param radix Radix value of interest.
469+
* @return The {@link IterableIntStream stream} of digits.
470+
*/
471+
public static IterableIntStream digits(int number, int radix) {
472+
if (number < 0) throw new IllegalArgumentException(
473+
"Please provide a positive integer."
474+
);
475+
476+
if (radix < 2) throw new IllegalArgumentException(
477+
"Please provide a radix greater than or equal to 2."
478+
);
479+
480+
return IterableIntStream.from(
481+
number == 0
482+
? IntStream.of(0)
483+
: IntStream.iterate(number, num -> num != 0, num -> num / radix).map(
484+
num -> num % radix
485+
)
486+
);
487+
}
488+
}
489+
490+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
491+
`;
492+
394493
exports[`App can equip single goody: Java gcd(int,int) 1`] = `
395494
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
396495
// Adventure Pack commit fake-commit-hash

workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,55 @@ final class VirtualList<T> extends AbstractList<T> {
359359
}"
360360
`;
361361
362+
exports[`App can render goody: Java digits(int) 1`] = `
363+
"package digits_int;
364+
365+
import iterable_int_stream.IterableIntStream;
366+
367+
import java.util.stream.IntStream;
368+
369+
final class AP {
370+
private AP() {}
371+
372+
/**
373+
* Get the digits in the given integer. The digits are returned as an
374+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
375+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned.
376+
* @param number Integer of interest.
377+
* @return The {@link IterableIntStream stream} of digits.
378+
*/
379+
public static IterableIntStream digits(int number) {
380+
return digits(number, 10);
381+
}
382+
383+
/**
384+
* Get the digits in the given integer for the given radix value. The digits are returned as an
385+
* {@link IterableIntStream integer stream} with the least significant digit as the first element
386+
* in the stream. For example, for integer 27363, [3,6,3,7,2] is returned with radix = 10.
387+
* @param number Integer of interest.
388+
* @param radix Radix value of interest.
389+
* @return The {@link IterableIntStream stream} of digits.
390+
*/
391+
public static IterableIntStream digits(int number, int radix) {
392+
if (number < 0) throw new IllegalArgumentException(
393+
"Please provide a positive integer."
394+
);
395+
396+
if (radix < 2) throw new IllegalArgumentException(
397+
"Please provide a radix greater than or equal to 2."
398+
);
399+
400+
return IterableIntStream.from(
401+
number == 0
402+
? IntStream.of(0)
403+
: IntStream.iterate(number, num -> num != 0, num -> num / radix).map(
404+
num -> num % radix
405+
)
406+
);
407+
}
408+
}"
409+
`;
410+
362411
exports[`App can render goody: Java gcd(int,int) 1`] = `
363412
"package gcd_int_int;
364413

0 commit comments

Comments
 (0)