Skip to content

Instantly share code, notes, and snippets.

@Test
public void testSortUnsortedStream() {
Person kenThompson = new Person("Ken", "Thompson");
Person edgerDijkstra = new Person("Edger", "Dijkstra");
Person simonThompson = new Person("Simon", "Thompson");
Set<Person> people = Set.of(
edgerDijkstra,
kenThompson,
simonThompson
);
@maartenl
maartenl / CallByValueOrCallByReferenceTest.java
Created October 20, 2023 13:54
Verifying if java is call-by-reference or call-by-value?
package com.mrbear.parameters;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Parameters in Java are call-by-value. Which means you cannot replace a parameter of a method with another object.
* Sure, you can replace the <i>content/state</i> of an object, but it will remain the same object.
* So, java is call-by-value, and object references provided in parameters are thus <i>also</i> call-by-value.
@maartenl
maartenl / ItemsTest.java
Created April 14, 2023 17:08
compute the most expensive item using streams
@Test
public void testStreamMax() {
Optional<Item> foundItem = items.stream()
.min(Comparator.comparing(Item::getPrice));
assertThat(foundItem).contains(items.get(4));
}
@maartenl
maartenl / ItemsTest.java
Last active April 14, 2023 17:15
determines the most expensive item using a foreach loop.
@Test
public void testForEachLoopMax() {
Optional<Item> foundItem = Optional.empty();
for (Item item: items) {
if (foundItem.isEmpty() ||
foundItem.get().getPrice().isGreaterThan(item.getPrice())) {
foundItem = Optional.of(item);
}
}
assertThat(foundItem).contains(items.get(4));
@maartenl
maartenl / Item.java
Created April 14, 2023 17:05
some source code on food items.
public class Item {
private final String name;
private final MonetaryAmount price;
private Item(String name, MonetaryAmount price) {
this.name = name;
this.price = price;
}
public MonetaryAmount getPrice() {
@maartenl
maartenl / dependencytree.txt
Last active October 15, 2022 07:44
mvn dependency:tree
[INFO] mrbear:portal:war:2.0.11-SNAPSHOT
[INFO] +- mrbear:portalentities:jar:1.0.7-SNAPSHOT:compile
[INFO] | +- javax.xml.bind:jaxb-api:jar:2.3.0:compile
[INFO] | +- com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:jar:20180219.1:compile
[INFO] | \- commons-validator:commons-validator:jar:1.4.0:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO] | +- commons-digester:commons-digester:jar:1.8:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.eclipse.microprofile:microprofile:pom:2.2:provided
[INFO] | +- javax.enterprise:cdi-api:jar:2.0:provided
@maartenl
maartenl / certificate_renew.sh
Created October 10, 2022 20:19
Basically a script that calls other scripts to get things done for LetsEncrypt SSL Certificates.
#!/bin/bash
timevar=`date +%Y-%m-%d_%H.%M.%S`
process_name='java'
echo "$timevar Renew certificates" >> /home/jelastic/crontabs/log.txt
certbot renew
cd /home/jelastic
echo "$timevar Import new certificates" >> /home/jelastic/crontabs/log.txt
./letsencrypt_certificate.sh
@maartenl
maartenl / crontab.txt
Created October 10, 2022 20:17
crontab listing of Letsencrypt SSL Certificate renewals.
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 0 14 03 * /home/jelastic/crontabs/certificate_renew.sh
@maartenl
maartenl / plantuml
Created October 10, 2022 13:41
Plantuml diagram for ER.
@startuml
' hide the spot
hide circle
' avoid problems with angled crows feet
skinparam linetype ortho
entity "Guild" as Guild {
*guildnr : number <<generated>>
@maartenl
maartenl / MutablePropertyCouldHaveChanged4.kt
Last active September 7, 2022 13:07
use ?.<operator> to fix the problem
/**
* use "?.let" to create a lambda in which we're sure there's no problem.
* Is a very nice solution, if possible.
*/
@Test
fun mutablePropertyCouldHaveChanged4() {
name?.let { assertThat(it.length).isNotZero() }
}