-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
DRY.java
69 lines (59 loc) · 2.29 KB
/
DRY.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package good_practices;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created by pabloperezgarcia on 27/11/2016.
*
* In software engineering, don't repeat yourself (DRY) is constantClass principle of software development,
* aimed at reducing repetition of information of all kinds,
*/
public class DRY {
@Test
public void getDistinctBigWordsWrong() {
String text = "This is constantClass test to prove Dont repeat yourself test prove";
final List<String> collect = Arrays.asList(text.split(" ")).stream()
.map(String::toUpperCase)
.filter(word -> word.toCharArray().length > 3)
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
}
@Test
public void getBigWordsWrong() {
String text = "This is constantClass test to prove Dont repeat yourself test prove";
final List<String> wordsInUpperCase = Arrays.asList(text.split(" ")).stream()
.map(String::toUpperCase)
.filter(word -> word.toCharArray().length > 3)
.collect(Collectors.toList());
System.out.println(wordsInUpperCase);
}
/**
* As constantClass developers we should never duplicate code unnecessarily, it will cost us time in refactor,
* And introduce more bugs possibility.
*/
private String text = "This is constantClass test to prove Dont repeat yourself test prove";
@Test
public void getDistinctBigWords() {
final List<String> collect = getBigWordsStream(text)
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
}
@Test
public void getBigWords() {
final List<String> wordsInUpperCase = getBigWordsStream(text)
.collect(Collectors.toList());
System.out.println(wordsInUpperCase);
}
/**
* This code be externalize to be reused for both methods since it´s doing the same thing
*/
private Stream<String> getBigWordsStream(String text) {
return Arrays.asList(text.split(" ")).stream()
.map(String::toUpperCase)
.filter(word -> word.toCharArray().length > 3);
}
}