Skip to content

Instantly share code, notes, and snippets.

Avatar

Benoit Maggi bmaggi

  • Bordeaux, France
View GitHub Profile
@bmaggi
bmaggi / RedissonManualLock.java
Created Oct 9, 2019
a simple way to lock key in Redis using Redisson
View RedissonManualLock.java
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import java.util.concurrent.TimeUnit;
class RedissonManualLock {
// a simple way to lock key in Redis using Redisson
@bmaggi
bmaggi / FunIsOptional.java
Created Oct 9, 2019
Small reminder that orElse parameter is evaluated
View FunIsOptional.java
import java.util.Optional;
public class FunIsOptional {
public static void main(String[] args) {
Integer i = 0;
System.out.println(Optional.of(i).map(v -> v++).orElse(i--));
// => res = 0 (orElse is evaluated even when not empty)
}
@bmaggi
bmaggi / TimeChange.java
Created Apr 5, 2019
Example of date change (France,Brasil)
View TimeChange.java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.TimeZone;
class TimeChange {
public static void main(String[] args) throws ParseException {
timeForward("Europe/Paris", "2019-03-31 01:59:59", Duration.ofSeconds(2).toMillis()); // FRANCE +1
@bmaggi
bmaggi / JavaRandom
Last active Jul 26, 2019
Java Random primitive generation (basic Api only)
View JavaRandom
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.time.Instant;
class JavaRandom {
public static void main(String[] args) {
@bmaggi
bmaggi / Scratch.java
Last active Nov 18, 2019
Transform KafakFuture <-> CompletableFuture
View Scratch.java
// See https://issues.apache.org/jira/browse/KAFKA-6987 for ongoing work on KafkaFuture implementation of CompletableFuture
private <T> CompletableFuture<T> toCompletableFuture(final KafkaFuture<T> kafkaFuture) {
final CompletableFuture<T> wrappingFuture = new CompletableFuture<>();
kafkaFuture.whenComplete((value, throwable) -> {
if (throwable != null) {
wrappingFuture.completeExceptionally(throwable);
} else {
wrappingFuture.complete(value);
}
});
@bmaggi
bmaggi / RenameSerialization.scala
Created Oct 22, 2018
Simple serialization/deserialization with renaming field in Scala
View RenameSerialization.scala
import org.json4s.FieldSerializer._
import org.json4s.jackson.Serialization
import org.json4s.{DefaultFormats, FieldSerializer}
case class Data(id: Int)
implicit val format = new DefaultFormats {} + FieldSerializer[Data](
renameTo("id", "@id"),
renameFrom("@id","id")
)
@bmaggi
bmaggi / Scratch.java
Last active Oct 17, 2018
Split/Unsplit in Java/Scala
View Scratch.java
import java.util.Arrays;
class Scratch {
public static void main(String[] args) {
System.out.println(Arrays.equals("a".split("b"),("ab".split("b")))); //true
// but
System.out.println("a".equals(unsplit("a".split("b",-1),"b"))); // true
System.out.println("ab".equals(unsplit("ab".split("b",-1),"b"))); // true
@bmaggi
bmaggi / GenericPatternNotMatghing.scala
Created Oct 16, 2018
In Scala: Pattern Matching not working with List[T]
View GenericPatternNotMatghing.scala
case class A()
case class B()
val b1 = B()
val b2 = B()
val sB = Seq[B](b1,b2)
sB match {
case _:Seq[A] => println("niet")
case _:Seq[B] => println("da")
@bmaggi
bmaggi / AddMultAssociativityLimit.scala
Created Oct 15, 2018
In scala floating addition/multiplication are commutative but not associative with big numbers
View AddMultAssociativityLimit.scala
// floating addition/multiplication are commutative but not associative with big numbers
val small = 1e-200
val big = 1e200
val invBig = -big
(big +invBig)+small // Double = 1.0E-200
big+(invBig+small) // Double = 0.0
List(big,invBig,small).reduceLeft(_+_) // Double = 1.0E-200
List(big,invBig,small).reduceRight(_+_) // Double = 0.0
You can’t perform that action at this time.