Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sushant57 committed Feb 23, 2019
2 parents acfc540 + c2a300b commit 9c8a505
Show file tree
Hide file tree
Showing 66 changed files with 2,817 additions and 1,028 deletions.
3 changes: 3 additions & 0 deletions core-groovy/src/main/groovy/com/baeldung/traits/Car.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung

class Car implements VehicleTrait {}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait UserTrait implements Human {
msg
}

def whoAmI() {
def self() {
return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung

trait VehicleTrait extends WheelTrait {

String showWheels() {
return "Num of Wheels $noOfWheels"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung

trait WheelTrait {

int noOfWheels

}
85 changes: 85 additions & 0 deletions core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.baeldung.map

import static org.junit.Assert.*
import org.junit.Test

class MapUnitTest {

@Test
void whenUsingEach_thenMapIsIterated() {
def map = [
'FF0000' : 'Red',
'00FF00' : 'Lime',
'0000FF' : 'Blue',
'FFFF00' : 'Yellow'
]

map.each { println "Hex Code: $it.key = Color Name: $it.value" }
}

@Test
void whenUsingEachWithEntry_thenMapIsIterated() {
def map = [
'E6E6FA' : 'Lavender',
'D8BFD8' : 'Thistle',
'DDA0DD' : 'Plum',
]

map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
}

@Test
void whenUsingEachWithKeyAndValue_thenMapIsIterated() {
def map = [
'000000' : 'Black',
'FFFFFF' : 'White',
'808080' : 'Gray'
]

map.each { key, val ->
println "Hex Code: $key = Color Name $val"
}
}

@Test
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() {
def map = [
'800080' : 'Purple',
'4B0082' : 'Indigo',
'6A5ACD' : 'Slate Blue'
]

map.eachWithIndex { entry, index ->
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
println "$indent Hex Code: $entry.key = Color Name: $entry.value"
}
}

@Test
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() {
def map = [
'FFA07A' : 'Light Salmon',
'FF7F50' : 'Coral',
'FF6347' : 'Tomato',
'FF4500' : 'Orange Red'
]

map.eachWithIndex { key, val, index ->
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
println "$indent Hex Code: $key = Color Name: $val"
}
}

@Test
void whenUsingForLoop_thenMapIsIterated() {
def map = [
'2E8B57' : 'Seagreen',
'228B22' : 'Forest Green',
'008000' : 'Green'
]

for (entry in map) {
println "Hex Code: $entry.key = Color Name: $entry.value"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TraitsUnitTest extends Specification {

def 'Should return employee instance when using Employee.whoAmI method' () {
when:
def emp = employee.whoAmI()
def emp = employee.self()
then:
emp
emp instanceof Employee
Expand Down
1 change: 1 addition & 0 deletions core-java-collections-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)
11 changes: 11 additions & 0 deletions core-java-collections-list/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.baeldung.list.primitive;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import gnu.trove.list.array.TIntArrayList;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 100000, iterations = 10)
@Warmup(batchSize = 100000, iterations = 10)
@State(Scope.Thread)
public class PrimitivesListPerformance {

private List<Integer> arrayList = new ArrayList<>();
private TIntArrayList tList = new TIntArrayList();
private cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();
private IntArrayList fastUtilList = new IntArrayList();

private int getValue = 10;

@Benchmark
public boolean addArrayList() {
return arrayList.add(getValue);
}

@Benchmark
public boolean addTroveIntList() {
return tList.add(getValue);
}

@Benchmark
public void addColtIntList() {
coltList.add(getValue);
}

@Benchmark
public boolean addFastUtilIntList() {
return fastUtilList.add(getValue);
}

@Benchmark
public int getArrayList() {
return arrayList.get(getValue);
}

@Benchmark
public int getTroveIntList() {
return tList.get(getValue);
}

@Benchmark
public int getColtIntList() {
return coltList.get(getValue);
}

@Benchmark
public int getFastUtilIntList() {
return fastUtilList.getInt(getValue);
}

@Benchmark
public boolean containsArrayList() {
return arrayList.contains(getValue);
}

@Benchmark
public boolean containsTroveIntList() {
return tList.contains(getValue);
}

@Benchmark
public boolean containsColtIntList() {
return coltList.contains(getValue);
}

@Benchmark
public boolean containsFastUtilIntList() {
return fastUtilList.contains(getValue);
}

public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(PrimitivesListPerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.baeldung.gson.conversion;

import com.google.gson.*;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class JsonObjectConversionsUnitTest {

@Test
void whenUsingJsonParser_thenConvertToJsonObject() throws Exception {
// Example 1: Using JsonParser
String json = "{ \"name\": \"Baeldung\", \"java\": true }";

JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();

Assert.assertTrue(jsonObject.isJsonObject());
Assert.assertTrue(jsonObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(jsonObject.get("java").getAsBoolean() == true);
}

@Test
void whenUsingGsonInstanceFromJson_thenConvertToJsonObject() throws Exception {
// Example 2: Using fromJson
String json = "{ \"name\": \"Baeldung\", \"java\": true }";

JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);

Assert.assertTrue(convertedObject.isJsonObject());
Assert.assertTrue(convertedObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(convertedObject.get("java").getAsBoolean() == true);
}

}
7 changes: 0 additions & 7 deletions jackson/src/test/java/com/baeldung/jackson/dtos/Address.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package com.baeldung.jackson.dtos;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class Address {

@JacksonXmlProperty(localName = "street_number")
String streetNumber;

@JacksonXmlProperty(localName = "street_name")
String streetName;

@JacksonXmlProperty(localName = "city")
String city;

public String getStreetNumber() {
Expand Down
6 changes: 1 addition & 5 deletions jackson/src/test/java/com/baeldung/jackson/dtos/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "person")
public final class Person {
private String firstName;
private String lastName;
@JacksonXmlElementWrapper(useWrapping = false)
private List<String> phoneNumbers = new ArrayList<>();
@JacksonXmlElementWrapper(localName = "addresses")
private List<Address> address = new ArrayList<>();

public List<Address> getAddress() {
Expand All @@ -38,6 +35,7 @@ public String getLastName() {
public void setLastName(String lastName) {
this.lastName = lastName;
}

public List<String> getPhoneNumbers() {
return phoneNumbers;
}
Expand All @@ -46,6 +44,4 @@ public void setPhoneNumbers(List<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}



}
Loading

0 comments on commit 9c8a505

Please sign in to comment.