Skip to content

Commit 8bc65bb

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 89e2736 + dffd5fd commit 8bc65bb

79 files changed

Lines changed: 1635 additions & 823 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎algorithms/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
1717
- [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance)
1818
- [How to Find the Kth Largest Element in Java](http://www.baeldung.com/java-kth-largest-element)
19+
- [Multi-Swarm Optimization Algorithm in Java](http://www.baeldung.com/java-multi-swarm-algorithm)

‎algorithms/pom.xml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
1010
<lombok.version>1.16.12</lombok.version>
1111
<commons-math3.version>3.6.1</commons-math3.version>
12+
<tradukisto.version>1.0.1</tradukisto.version>
1213
</properties>
1314

1415
<parent>
@@ -39,14 +40,18 @@
3940
<artifactId>jgrapht-core</artifactId>
4041
<version>1.0.1</version>
4142
</dependency>
43+
<dependency>
44+
<groupId>pl.allegro.finance</groupId>
45+
<artifactId>tradukisto</artifactId>
46+
<version>${tradukisto.version}</version>
47+
</dependency>
4248
<dependency>
4349
<groupId>org.assertj</groupId>
4450
<artifactId>assertj-core</artifactId>
4551
<version>3.9.0</version>
4652
<scope>test</scope>
4753
</dependency>
4854
</dependencies>
49-
5055
<build>
5156
<pluginManagement>
5257
<plugins>
@@ -77,4 +82,4 @@
7782
</plugin>
7883
</plugins>
7984
</reporting>
80-
</project>
85+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.algorithms.numberwordconverter;
2+
3+
import java.math.BigDecimal;
4+
5+
import pl.allegro.finance.tradukisto.MoneyConverters;
6+
7+
public class NumberWordConverter {
8+
9+
public static final String INVALID_INPUT_GIVEN = "Invalid input given";
10+
11+
public static final String[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
12+
13+
public static final String[] tens = {
14+
"", // 0
15+
"", // 1
16+
"twenty", // 2
17+
"thirty", // 3
18+
"forty", // 4
19+
"fifty", // 5
20+
"sixty", // 6
21+
"seventy", // 7
22+
"eighty", // 8
23+
"ninety" // 9
24+
};
25+
26+
public static String getMoneyIntoWords(String input) {
27+
MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE;
28+
return converter.asWords(new BigDecimal(input));
29+
}
30+
31+
public static String getMoneyIntoWords(final double money) {
32+
long dollar = (long) money;
33+
long cents = Math.round((money - dollar) * 100);
34+
if (money == 0D) {
35+
return "";
36+
}
37+
if (money < 0) {
38+
return INVALID_INPUT_GIVEN;
39+
}
40+
String dollarPart = "";
41+
if (dollar > 0) {
42+
dollarPart = convert(dollar) + " dollar" + (dollar == 1 ? "" : "s");
43+
}
44+
String centsPart = "";
45+
if (cents > 0) {
46+
if (dollarPart.length() > 0) {
47+
centsPart = " and ";
48+
}
49+
centsPart += convert(cents) + " cent" + (cents == 1 ? "" : "s");
50+
}
51+
return dollarPart + centsPart;
52+
}
53+
54+
private static String convert(final long n) {
55+
if (n < 0) {
56+
return INVALID_INPUT_GIVEN;
57+
}
58+
if (n < 20) {
59+
return ones[(int) n];
60+
}
61+
if (n < 100) {
62+
return tens[(int) n / 10] + ((n % 10 != 0) ? " " : "") + ones[(int) n % 10];
63+
}
64+
if (n < 1000) {
65+
return ones[(int) n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
66+
}
67+
if (n < 1_000_000) {
68+
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
69+
}
70+
if (n < 1_000_000_000) {
71+
return convert(n / 1_000_000) + " million" + ((n % 1_000_000 != 0) ? " " : "") + convert(n % 1_000_000);
72+
}
73+
return convert(n / 1_000_000_000) + " billion" + ((n % 1_000_000_000 != 0) ? " " : "") + convert(n % 1_000_000_000);
74+
}
75+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.algorithms.moneywords;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.algorithms.numberwordconverter.NumberWordConverter;
8+
9+
public class NumberWordConverterTest {
10+
11+
@Test
12+
public void whenMoneyNegative_thenReturnInvalidInput() {
13+
assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13));
14+
}
15+
16+
@Test
17+
public void whenZeroDollarsGiven_thenReturnEmptyString() {
18+
assertEquals("", NumberWordConverter.getMoneyIntoWords(0));
19+
}
20+
21+
@Test
22+
public void whenOnlyDollarsGiven_thenReturnWords() {
23+
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
24+
}
25+
26+
@Test
27+
public void whenOnlyCentsGiven_thenReturnWords() {
28+
assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6));
29+
}
30+
31+
@Test
32+
public void whenAlmostAMillioDollarsGiven_thenReturnWords() {
33+
String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars";
34+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999));
35+
}
36+
37+
@Test
38+
public void whenThirtyMillionDollarsGiven_thenReturnWords() {
39+
String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars";
40+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978));
41+
}
42+
43+
@Test
44+
public void whenTwoBillionDollarsGiven_thenReturnWords() {
45+
String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars";
46+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810));
47+
}
48+
49+
@Test
50+
public void whenGivenDollarsAndCents_thenReturnWords() {
51+
String expectedResult = "nine hundred twenty four dollars and sixty cents";
52+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6));
53+
}
54+
55+
@Test
56+
public void whenOneDollarAndNoCents_thenReturnDollarSingular() {
57+
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
58+
}
59+
60+
@Test
61+
public void whenNoDollarsAndOneCent_thenReturnCentSingular() {
62+
assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01));
63+
}
64+
65+
@Test
66+
public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
67+
assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02));
68+
}
69+
70+
@Test
71+
public void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
72+
assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99));
73+
}
74+
75+
@Test
76+
public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
77+
assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959));
78+
}
79+
80+
@Test
81+
public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
82+
assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310"));
83+
}
84+
}

‎apache-curator/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<!-- testing -->
2020
<assertj.version>3.6.1</assertj.version>
21+
<avaitility.version>1.7.0</avaitility.version>
2122

2223
</properties>
2324

@@ -64,5 +65,12 @@
6465
<version>${assertj.version}</version>
6566
<scope>test</scope>
6667
</dependency>
68+
69+
<dependency>
70+
<groupId>com.jayway.awaitility</groupId>
71+
<artifactId>awaitility</artifactId>
72+
<version>${avaitility.version}</version>
73+
<scope>test</scope>
74+
</dependency>
6775
</dependencies>
6876
</project>
Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.apache.curator.configuration;
22

3+
import static com.jayway.awaitility.Awaitility.await;
34
import static org.assertj.core.api.Assertions.assertThat;
45

56
import java.util.ArrayList;
@@ -26,73 +27,63 @@ public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
2627
String expected = "my_value";
2728

2829
// Create key nodes structure
29-
client
30-
.create()
31-
.forPath(key);
30+
client.create()
31+
.forPath(key);
3232

3333
// Set data value for our key
34-
async
35-
.setData()
36-
.forPath(key, expected.getBytes());
34+
async.setData()
35+
.forPath(key, expected.getBytes());
3736

3837
// Get data value
3938
AtomicBoolean isEquals = new AtomicBoolean();
40-
async
41-
.getData()
42-
.forPath(key)
43-
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
39+
async.getData()
40+
.forPath(key)
41+
.thenAccept(
42+
data -> isEquals.set(new String(data).equals(expected)));
4443

45-
Thread.sleep(1000);
46-
47-
assertThat(isEquals.get()).isTrue();
44+
await().until(() -> assertThat(isEquals.get()).isTrue());
4845
}
4946
}
5047

5148
@Test
52-
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
49+
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered()
50+
throws Exception {
5351
try (CuratorFramework client = newClient()) {
5452
client.start();
5553
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
5654
String key = getKey();
5755
String expected = "my_value";
5856

5957
// Create key structure
60-
async
61-
.create()
62-
.forPath(key);
58+
async.create()
59+
.forPath(key);
6360

6461
List<String> changes = new ArrayList<>();
6562

6663
// Watch data value
67-
async
68-
.watched()
69-
.getData()
70-
.forPath(key)
71-
.event()
72-
.thenAccept(watchedEvent -> {
73-
try {
74-
changes.add(new String(client
75-
.getData()
76-
.forPath(watchedEvent.getPath())));
77-
} catch (Exception e) {
78-
// fail ...
79-
}
80-
});
64+
async.watched()
65+
.getData()
66+
.forPath(key)
67+
.event()
68+
.thenAccept(watchedEvent -> {
69+
try {
70+
changes.add(new String(client.getData()
71+
.forPath(watchedEvent.getPath())));
72+
} catch (Exception e) {
73+
// fail ...
74+
}
75+
});
8176

8277
// Set data value for our key
83-
async
84-
.setData()
85-
.forPath(key, expected.getBytes());
86-
87-
Thread.sleep(1000);
78+
async.setData()
79+
.forPath(key, expected.getBytes());
8880

89-
assertThat(changes.size() > 0).isTrue();
81+
await().until(() -> assertThat(changes.size() > 0).isTrue());
9082
}
9183
}
9284

9385
private String getKey() {
94-
return String.format(KEY_FORMAT, UUID
95-
.randomUUID()
96-
.toString());
86+
return String.format(KEY_FORMAT, UUID.randomUUID()
87+
.toString());
9788
}
9889
}

0 commit comments

Comments
 (0)