22
33import java .math .BigDecimal ;
44import java .util .List ;
5- import java .util .Objects ;
65import java .util .Optional ;
76import java .util .stream .Stream ;
87
@@ -14,7 +13,7 @@ public class FibonacciJavaStreams {
1413
1514 public static Optional <BigDecimal > calculate (final BigDecimal index ) {
1615 if (index == null || index .compareTo (BigDecimal .ZERO ) < 0 ) {
17- return Optional . empty ( );
16+ throw new IllegalArgumentException ( "Input index cannot be null or negative!" );
1817 }
1918
2019 if (index .compareTo (BigDecimal .ONE ) < 0 ) {
@@ -30,72 +29,4 @@ public static Optional<BigDecimal> calculate(final BigDecimal index) {
3029
3130 return results .isEmpty () ? Optional .empty () : Optional .of (results .get (results .size () - 1 ));
3231 }
33-
34- public static void assertThat (final Object actual , final Object expected ) {
35- if (!Objects .equals (actual , expected )) {
36- throw new AssertionError (String .format ("expected=%s but was actual=%s" , expected , actual ));
37- }
38- }
39-
40- public static void main (final String [] args ) {
41- {
42- final Optional <BigDecimal > result = calculate (new BigDecimal (-1 ));
43- assertThat (result .isEmpty (), true );
44- }
45- {
46- final Optional <BigDecimal > result = calculate (BigDecimal .ZERO );
47- assertThat (result .isPresent (), true );
48- result .ifPresent (value -> assertThat (value , BigDecimal .ZERO ));
49- }
50- {
51- final Optional <BigDecimal > result = calculate (BigDecimal .ONE );
52- assertThat (result .isPresent (), true );
53- result .ifPresent (value -> assertThat (value , BigDecimal .ONE ));
54- }
55- {
56- final Optional <BigDecimal > result = calculate (new BigDecimal (2 ));
57- assertThat (result .isPresent (), true );
58- result .ifPresent (value -> assertThat (value , BigDecimal .ONE ));
59- }
60- {
61- final Optional <BigDecimal > result = calculate (new BigDecimal (3 ));
62- assertThat (result .isPresent (), true );
63- result .ifPresent (value -> assertThat (value , new BigDecimal (2 )));
64- }
65- {
66- final Optional <BigDecimal > result = calculate (new BigDecimal (10 ));
67- assertThat (result .isPresent (), true );
68- result .ifPresent (value -> assertThat (value , new BigDecimal (55 )));
69- }
70- {
71- final Optional <BigDecimal > result = calculate (new BigDecimal (20 ));
72- assertThat (result .isPresent (), true );
73- result .ifPresent (value -> assertThat (value , new BigDecimal (6765 )));
74- }
75- {
76- final Optional <BigDecimal > result = calculate (new BigDecimal (30 ));
77- assertThat (result .isPresent (), true );
78- result .ifPresent (value -> assertThat (value , new BigDecimal (832040 )));
79- }
80- {
81- final Optional <BigDecimal > result = calculate (new BigDecimal (40 ));
82- assertThat (result .isPresent (), true );
83- result .ifPresent (value -> assertThat (value , new BigDecimal (102334155 )));
84- }
85- {
86- final Optional <BigDecimal > result = calculate (new BigDecimal (50 ));
87- assertThat (result .isPresent (), true );
88- result .ifPresent (value -> assertThat (value , new BigDecimal (12586269025L )));
89- }
90- {
91- final Optional <BigDecimal > result = calculate (new BigDecimal (100 ));
92- assertThat (result .isPresent (), true );
93- result .ifPresent (value -> assertThat (value , new BigDecimal ("354224848179261915075" )));
94- }
95- {
96- final Optional <BigDecimal > result = calculate (new BigDecimal (200 ));
97- assertThat (result .isPresent (), true );
98- result .ifPresent (value -> assertThat (value , new BigDecimal ("280571172992510140037611932413038677189525" )));
99- }
100- }
10132}
0 commit comments