Skip to content

Commit d66d9fb

Browse files
author
eugenp
committed
maven fix and small formatting work
1 parent b4ac6c6 commit d66d9fb

11 files changed

Lines changed: 490 additions & 522 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 336 additions & 330 deletions
Large diffs are not rendered by default.

‎core-java/src/main/java/com/baeldung/algorithms/City.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
@Data
66
public class City {
77

8-
private int x;
9-
private int y;
8+
private int x;
9+
private int y;
1010

11-
public City() {
12-
this.x = (int) (Math.random() * 500);
13-
this.y = (int) (Math.random() * 500);
14-
}
11+
public City() {
12+
this.x = (int) (Math.random() * 500);
13+
this.y = (int) (Math.random() * 500);
14+
}
1515

16-
public double distanceToCity(City city) {
17-
int x = Math.abs(getX() - city.getX());
18-
int y = Math.abs(getY() - city.getY());
19-
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
20-
}
16+
public double distanceToCity(City city) {
17+
int x = Math.abs(getX() - city.getX());
18+
int y = Math.abs(getY() - city.getY());
19+
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
20+
}
2121

2222
}

‎core-java/src/main/java/com/baeldung/algorithms/SimulatedAnnealing.java‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22

33
public class SimulatedAnnealing {
44

5-
private static Travel travel = new Travel(10);
6-
7-
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) {
8-
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: "
9-
+ numberOfIterations + " and colling rate: " + coolingRate);
10-
double t = startingTemperature;
11-
travel.generateInitialTravel();
12-
double bestDistance = travel.getDistance();
13-
System.out.println("Initial distance of travel: " + bestDistance);
14-
Travel bestSolution = travel;
15-
Travel currentSolution = bestSolution;
5+
private static Travel travel = new Travel(10);
166

17-
for (int i = 0; i < numberOfIterations; i++) {
18-
if (t > 0.1) {
19-
currentSolution.swapCities();
20-
double currentDistance = currentSolution.getDistance();
21-
if (currentDistance == 0)
22-
continue;
23-
if (currentDistance < bestDistance) {
24-
bestDistance = currentDistance;
25-
} else if (Math.exp((currentDistance - bestDistance) / t) < Math.random()) {
26-
currentSolution.revertSwap();
27-
}
28-
t *= coolingRate;
29-
}
30-
if (i % 100 == 0) {
31-
System.out.println("Iteration #" + i);
32-
}
33-
}
34-
return bestDistance;
35-
}
7+
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) {
8+
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " + numberOfIterations + " and colling rate: " + coolingRate);
9+
double t = startingTemperature;
10+
travel.generateInitialTravel();
11+
double bestDistance = travel.getDistance();
12+
System.out.println("Initial distance of travel: " + bestDistance);
13+
Travel bestSolution = travel;
14+
Travel currentSolution = bestSolution;
3615

37-
public static void main(String[] args) {
38-
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9));
39-
}
16+
for (int i = 0; i < numberOfIterations; i++) {
17+
if (t > 0.1) {
18+
currentSolution.swapCities();
19+
double currentDistance = currentSolution.getDistance();
20+
if (currentDistance == 0)
21+
continue;
22+
if (currentDistance < bestDistance) {
23+
bestDistance = currentDistance;
24+
} else if (Math.exp((currentDistance - bestDistance) / t) < Math.random()) {
25+
currentSolution.revertSwap();
26+
}
27+
t *= coolingRate;
28+
}
29+
if (i % 100 == 0) {
30+
System.out.println("Iteration #" + i);
31+
}
32+
}
33+
return bestDistance;
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9));
38+
}
4039

4140
}

‎core-java/src/main/java/com/baeldung/algorithms/Travel.java‎

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,53 @@
88
@Data
99
public class Travel {
1010

11-
private ArrayList<City> travel = new ArrayList<>();
12-
private ArrayList<City> previousTravel = new ArrayList<>();
13-
14-
public Travel(int numberOfCities) {
15-
for (int i = 0; i < numberOfCities; i++) {
16-
travel.add(new City());
17-
}
18-
}
19-
20-
public void generateInitialTravel() {
21-
if (travel.isEmpty())
22-
new Travel(10);
23-
Collections.shuffle(travel);
24-
}
25-
26-
public void swapCities() {
27-
int a = generateRandomIndex();
28-
int b = generateRandomIndex();
29-
previousTravel = travel;
30-
travel.set(a, travel.get(b));
31-
}
32-
33-
public void revertSwap() {
34-
travel = previousTravel;
35-
}
36-
37-
private int generateRandomIndex() {
38-
return (int) (Math.random() * travel.size());
39-
}
40-
41-
public City getCity(int index) {
42-
return travel.get(index);
43-
}
44-
45-
public int getDistance() {
46-
int distance = 0;
47-
for (int index = 0; index < travel.size(); index++) {
48-
City starting = getCity(index);
49-
City destination;
50-
if (index + 1 < travel.size()) {
51-
destination = getCity(index + 1);
52-
} else {
53-
destination = getCity(0);
54-
}
55-
distance += starting.distanceToCity(destination);
56-
}
57-
return distance;
58-
}
11+
private ArrayList<City> travel = new ArrayList<>();
12+
private ArrayList<City> previousTravel = new ArrayList<>();
13+
14+
public Travel(int numberOfCities) {
15+
for (int i = 0; i < numberOfCities; i++) {
16+
travel.add(new City());
17+
}
18+
}
19+
20+
public void generateInitialTravel() {
21+
if (travel.isEmpty())
22+
new Travel(10);
23+
Collections.shuffle(travel);
24+
}
25+
26+
public void swapCities() {
27+
int a = generateRandomIndex();
28+
int b = generateRandomIndex();
29+
previousTravel = travel;
30+
travel.set(a, travel.get(b));
31+
}
32+
33+
public void revertSwap() {
34+
travel = previousTravel;
35+
}
36+
37+
private int generateRandomIndex() {
38+
return (int) (Math.random() * travel.size());
39+
}
40+
41+
public City getCity(int index) {
42+
return travel.get(index);
43+
}
44+
45+
public int getDistance() {
46+
int distance = 0;
47+
for (int index = 0; index < travel.size(); index++) {
48+
City starting = getCity(index);
49+
City destination;
50+
if (index + 1 < travel.size()) {
51+
destination = getCity(index + 1);
52+
} else {
53+
destination = getCity(0);
54+
}
55+
distance += starting.distanceToCity(destination);
56+
}
57+
return distance;
58+
}
5959

6060
}

‎core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
public class SimulatedAnnealingTest {
77

8-
@Test
9-
public void testSimulateAnnealing() {
10-
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
11-
}
8+
@Test
9+
public void testSimulateAnnealing() {
10+
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
11+
}
1212

1313
}

‎core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class EncoderDecoderUnitTest {
2222
private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
2323
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
2424

25-
2625
private String encodeValue(String value) {
2726
String encoded = null;
2827
try {
@@ -59,9 +58,7 @@ public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
5958
requestParams.put("key2", "value@!$2");
6059
requestParams.put("key3", "value%3");
6160

62-
String encodedURL = requestParams.keySet().stream()
63-
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
64-
.collect(joining("&", "http://www.baeldung.com?", ""));
61+
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
6562

6663
Assert.assertThat(testUrl, is(encodedURL));
6764
}
@@ -103,12 +100,9 @@ public void givenPathAndRequestParam_whenUTF8Scheme_thenEncode() throws Exceptio
103100

104101
String path = "path+1";
105102

106-
String encodedURL = requestParams.keySet().stream()
107-
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
108-
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
103+
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
109104

110105
Assert.assertThat(testUrlWithPath, is(encodedURL));
111106
}
112107

113-
114108
}

‎core-java/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@ public class Java8CollectionCleanupUnitTest {
1717
@Test
1818
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {
1919
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
20-
final List<Integer> listWithoutNulls = list.parallelStream()
21-
.filter(Objects::nonNull)
22-
.collect(Collectors.toList());
20+
final List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
2321

2422
assertThat(listWithoutNulls, hasSize(3));
2523
}
2624

2725
@Test
2826
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
2927
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
30-
final List<Integer> listWithoutNulls = list.stream()
31-
.filter(Objects::nonNull)
32-
.collect(Collectors.toList());
28+
final List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
3329

3430
assertThat(listWithoutNulls, hasSize(3));
3531
}
@@ -45,9 +41,7 @@ public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {
4541
@Test
4642
public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
4743
final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
48-
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream()
49-
.distinct()
50-
.collect(Collectors.toList());
44+
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList());
5145

5246
assertThat(listWithoutDuplicates, hasSize(3));
5347
}

‎core-java/src/test/java/com/baeldung/java8/JavaFileSizeUnitTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public class JavaFileSizeUnitTest {
1818
@Before
1919
public void init() {
2020
final String separator = File.separator;
21-
filePath = String.join(separator, new String[] {"src", "test", "resources", "testFolder", "sample_file_1.in"});
21+
filePath = String.join(separator, new String[] { "src", "test", "resources", "testFolder", "sample_file_1.in" });
2222
}
2323

2424
@Test
2525
public void whenGetFileSize_thenCorrect() {
2626
final File file = new File(filePath);
27-
27+
2828
final long size = getFileSize(file);
2929

3030
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
@@ -34,16 +34,16 @@ public void whenGetFileSize_thenCorrect() {
3434
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
3535
final Path path = Paths.get(this.filePath);
3636
final FileChannel fileChannel = FileChannel.open(path);
37-
37+
3838
final long fileSize = fileChannel.size();
39-
39+
4040
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize);
4141
}
4242

4343
@Test
4444
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
4545
final File file = new File(filePath);
46-
46+
4747
final long size = FileUtils.sizeOf(file);
4848

4949
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
@@ -52,9 +52,9 @@ public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
5252
@Test
5353
public void whenGetReadableFileSize_thenCorrect() {
5454
final File file = new File(filePath);
55-
55+
5656
final long size = getFileSize(file);
57-
57+
5858
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size));
5959
}
6060

‎core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void whenOptionalFilterWorks_thenCorrect() {
9393
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
9494
assertFalse(is2017);
9595
}
96+
9697
@Test
9798
public void whenFiltersWithoutOptional_thenCorrect() {
9899
assertTrue(priceIsInRange1(new Modem(10.0)));
@@ -121,12 +122,9 @@ public boolean priceIsInRange1(Modem modem) {
121122
}
122123

123124
public boolean priceIsInRange2(Modem modem2) {
124-
return Optional.ofNullable(modem2)
125-
.map(Modem::getPrice)
126-
.filter(p -> p >= 10)
127-
.filter(p -> p <= 15)
128-
.isPresent();
125+
return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent();
129126
}
127+
130128
// Transforming Value With map()
131129
@Test
132130
public void givenOptional_whenMapWorks_thenCorrect() {

0 commit comments

Comments
 (0)