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 remote-tracking branch 'upstream/master'
- Loading branch information
Showing
66 changed files
with
2,817 additions
and
1,028 deletions.
There are no files selected for viewing
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,3 @@ | ||
package com.baeldung | ||
|
||
class Car implements VehicleTrait {} |
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ trait UserTrait implements Human { | |
msg | ||
} | ||
|
||
def whoAmI() { | ||
def self() { | ||
return this | ||
} | ||
|
||
|
9 changes: 9 additions & 0 deletions
9
core-groovy/src/main/groovy/com/baeldung/traits/VehicleTrait.groovy
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,9 @@ | ||
package com.baeldung | ||
|
||
trait VehicleTrait extends WheelTrait { | ||
|
||
String showWheels() { | ||
return "Num of Wheels $noOfWheels" | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
core-groovy/src/main/groovy/com/baeldung/traits/WheelTrait.groovy
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,7 @@ | ||
package com.baeldung | ||
|
||
trait WheelTrait { | ||
|
||
int noOfWheels | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy
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,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" | ||
} | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
...collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.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,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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
gson/src/test/java/org/baeldung/gson/conversion/JsonObjectConversionsUnitTest.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,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); | ||
} | ||
|
||
} |
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
Oops, something went wrong.