Skip to content

Commit

Permalink
Merge branch 'master' into stream-foreach-ifelse-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RoscoeLotriet committed Oct 1, 2018
2 parents 379eab8 + 037c3f4 commit 1a8b799
Show file tree
Hide file tree
Showing 87 changed files with 2,033 additions and 170 deletions.
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++];
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
/**
* Unit test for simple App.
*/
public class AppTest
public class AppUnitTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
public AppUnitTest( String testName )
{
super( testName );
}
Expand All @@ -25,7 +25,7 @@ public AppTest( String testName )
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
return new TestSuite( AppUnitTest.class );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion apache-avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.baeldung</groupId>
<artifactId>apache-avro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Apache Avro</version>
<name>Apache Avro</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static org.junit.Assert.*;

public class AvroSerealizerDeSerealizerTest {
public class AvroSerealizerDeSerealizerUnitTest {

AvroSerealizer serealizer;
AvroDeSerealizer deSerealizer;
Expand Down
46 changes: 46 additions & 0 deletions apache-geode/pom.xml
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 apache-geode/src/main/java/com/baeldung/geode/Customer.java
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 apache-geode/src/main/java/com/baeldung/geode/CustomerKey.java
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;
}
}
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();
}
}
Loading

0 comments on commit 1a8b799

Please sign in to comment.