forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into stream-foreach-ifelse-logic
- Loading branch information
Showing
87 changed files
with
2,033 additions
and
170 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
algorithms/src/main/java/com/baeldung/algorithms/mergesort/MergeSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.baeldung.algorithms.mergesort; | ||
|
||
public class MergeSort { | ||
|
||
public static void main(String[] args) { | ||
int[] a = { 5, 1, 6, 2, 3, 4 }; | ||
mergeSort(a, a.length); | ||
for (int i = 0; i < a.length; i++) | ||
System.out.println(a[i]); | ||
} | ||
|
||
public static void mergeSort(int[] a, int n) { | ||
if (n < 2) | ||
return; | ||
int mid = n / 2; | ||
int[] l = new int[mid]; | ||
int[] r = new int[n - mid]; | ||
|
||
for (int i = 0; i < mid; i++) { | ||
l[i] = a[i]; | ||
} | ||
for (int i = mid; i < n; i++) { | ||
r[i - mid] = a[i]; | ||
} | ||
mergeSort(l, mid); | ||
mergeSort(r, n - mid); | ||
|
||
merge(a, l, r, mid, n - mid); | ||
} | ||
|
||
public static void merge(int[] a, int[] l, int[] r, int left, int right) { | ||
|
||
int i = 0, j = 0, k = 0; | ||
|
||
while (i < left && j < right) { | ||
|
||
if (l[i] < r[j]) | ||
a[k++] = l[i++]; | ||
else | ||
a[k++] = r[j++]; | ||
|
||
} | ||
|
||
while (i < left) | ||
a[k++] = l[i++]; | ||
|
||
while (j < right) | ||
a[k++] = r[j++]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baeldung.algorithms.mergesort; | ||
|
||
import org.junit.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
public class MergeSortUnitTest { | ||
|
||
@Test | ||
public void positiveTest() { | ||
int[] actual = { 5, 1, 6, 2, 3, 4 }; | ||
int[] expected = { 1, 2, 3, 4, 5, 6 }; | ||
MergeSort.mergeSort(actual, actual.length); | ||
Assert.assertArrayEquals(expected, actual); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.baeldung</groupId> | ||
<artifactId>apache-geode</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<geode.core>1.6.0</geode.core> | ||
</properties> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.geode</groupId> | ||
<artifactId>geode-core</artifactId> | ||
<version>${geode.core}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>RELEASE</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
78 changes: 78 additions & 0 deletions
78
apache-geode/src/main/java/com/baeldung/geode/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.baeldung.geode; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
public class Customer implements Serializable { | ||
|
||
private static final long serialVersionUID = -7482516011038799900L; | ||
|
||
private CustomerKey key; | ||
private String firstName; | ||
private String lastName; | ||
private Integer age; | ||
|
||
public Customer() { | ||
} | ||
|
||
public Customer(String firstName, String lastName, int age) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.age = age; | ||
} | ||
|
||
public Customer(CustomerKey key, String firstName, String lastName, int age) { | ||
this(firstName, lastName, age); | ||
this.key = key; | ||
} | ||
|
||
// setters and getters | ||
|
||
public static long getSerialVersionUID() { | ||
return serialVersionUID; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public void setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public void setLastName(String lastName) { | ||
this.lastName = lastName; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Customer{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age + '}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Customer customer = (Customer) o; | ||
return Objects.equals(firstName, customer.firstName) && Objects.equals(lastName, customer.lastName) && Objects.equals(age, customer.age); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(firstName, lastName, age); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
apache-geode/src/main/java/com/baeldung/geode/CustomerKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.baeldung.geode; | ||
|
||
import java.io.Serializable; | ||
|
||
public class CustomerKey implements Serializable { | ||
|
||
private static final long serialVersionUID = -3529253035303792458L; | ||
private long id; | ||
private String country; | ||
|
||
public CustomerKey(long id) { | ||
this.id = id; | ||
this.country = "USA"; | ||
} | ||
|
||
public CustomerKey(long id, String country) { | ||
this.id = id; | ||
this.country = country; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCountry() { | ||
return country; | ||
} | ||
|
||
public void setCountry(String country) { | ||
this.country = country; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
|
||
CustomerKey that = (CustomerKey) o; | ||
|
||
if (id != that.id) | ||
return false; | ||
return country != null ? country.equals(that.country) : that.country == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = (int) (id ^ (id >>> 32)); | ||
result = 31 * result + (country != null ? country.hashCode() : 0); | ||
return result; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
apache-geode/src/main/java/com/baeldung/geode/functions/UpperCaseNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.geode.functions; | ||
|
||
import com.baeldung.geode.Customer; | ||
import com.baeldung.geode.CustomerKey; | ||
import org.apache.geode.cache.Region; | ||
import org.apache.geode.cache.execute.Function; | ||
import org.apache.geode.cache.execute.FunctionContext; | ||
import org.apache.geode.cache.execute.RegionFunctionContext; | ||
|
||
import java.util.Map; | ||
|
||
public class UpperCaseNames implements Function<Boolean> { | ||
private static final long serialVersionUID = -8946294032165677602L; | ||
|
||
@Override | ||
public void execute(FunctionContext<Boolean> context) { | ||
RegionFunctionContext regionContext = (RegionFunctionContext) context; | ||
Region<CustomerKey, Customer> region = regionContext.getDataSet(); | ||
|
||
for (Map.Entry<CustomerKey, Customer> entry : region.entrySet()) { | ||
Customer customer = entry.getValue(); | ||
customer.setFirstName(customer.getFirstName() | ||
.toUpperCase()); | ||
} | ||
|
||
context.getResultSender() | ||
.lastResult(true); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return getClass().getName(); | ||
} | ||
} |
Oops, something went wrong.