From 6079210fba625af6a913569fb81f1a841661e2a8 Mon Sep 17 00:00:00 2001
From: Colin DAMON <cdamon@ippon.fr>
Date: Tue, 20 Apr 2021 13:18:28 +0200
Subject: [PATCH] Java puzzles subject

---
 .gitlab-ci.yml                                |   1 +
 java-puzzles/.gitlab-ci.yml                   |  11 ++
 java-puzzles/pom.xml                          |  25 ++++
 java-puzzles/readme.md                        |   9 ++
 .../java/fr/ippon/PlayingWithBigDecimal.java  |  18 +++
 .../java/fr/ippon/PlayingWithFinally.java     |  40 +++++++
 .../fr/ippon/PlayingWithInstanciations.java   |  27 +++++
 .../java/fr/ippon/PlayingWithInteger.java     |  31 +++++
 .../test/java/fr/ippon/PlayingWithString.java |  28 +++++
 .../java/fr/ippon/PlayingWithThreads.java     | 113 ++++++++++++++++++
 readme.md                                     |   1 +
 11 files changed, 304 insertions(+)
 create mode 100644 java-puzzles/.gitlab-ci.yml
 create mode 100644 java-puzzles/pom.xml
 create mode 100644 java-puzzles/readme.md
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithBigDecimal.java
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithFinally.java
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithInstanciations.java
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithInteger.java
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithString.java
 create mode 100644 java-puzzles/src/test/java/fr/ippon/PlayingWithThreads.java

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c46551df..a3769253 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -35,3 +35,4 @@ include:
   - local: "/tennis/greenfield-2/.gitlab-ci.yml"
   - local: "/mastermind/.gitlab-ci.yml"
   - local: "/try-monade/.gitlab-ci.yml"
+  - local: "/java-puzzles/.gitlab-ci.yml"
diff --git a/java-puzzles/.gitlab-ci.yml b/java-puzzles/.gitlab-ci.yml
new file mode 100644
index 00000000..a140bf09
--- /dev/null
+++ b/java-puzzles/.gitlab-ci.yml
@@ -0,0 +1,11 @@
+package-java-puzzles:
+  variables:
+    PROJECT_FOLDER: "java-puzzles"
+  extends: .java
+  only:
+    refs:
+      - master
+      - merge_requests
+    changes:
+      - ".gitlab-common-ci.yml"
+      - "java-puzzles/**/*"
diff --git a/java-puzzles/pom.xml b/java-puzzles/pom.xml
new file mode 100644
index 00000000..78810fa2
--- /dev/null
+++ b/java-puzzles/pom.xml
@@ -0,0 +1,25 @@
+<?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>
+
+  <parent>
+    <version>1.0.0</version>
+    <groupId>fr.ippon.kata</groupId>
+    <artifactId>java-parent</artifactId>
+    <relativePath>../java-parent</relativePath>
+  </parent>
+
+  <version>1.0.0-SNAPSHOT</version>
+  <artifactId>java-puzzles</artifactId>
+
+  <name>JavaPuzzles</name>
+
+  <developers>
+    <developer>
+      <email>cdamon@ippon.fr</email>
+      <name>Colin DAMON</name>
+    </developer>
+  </developers>
+</project>
diff --git a/java-puzzles/readme.md b/java-puzzles/readme.md
new file mode 100644
index 00000000..deb2fb5f
--- /dev/null
+++ b/java-puzzles/readme.md
@@ -0,0 +1,9 @@
+# Java puzzles
+
+Petits exercices de code Java
+
+-   **Auteurs** : Colin DAMON
+-   **Date** : 22/04/2021
+-   **Langage** : Java
+-   **Niveau** : TODO
+-   **Replay** : TODO
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithBigDecimal.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithBigDecimal.java
new file mode 100644
index 00000000..bb76436c
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithBigDecimal.java
@@ -0,0 +1,18 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import java.math.BigDecimal;
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithBigDecimal {
+
+  @Test
+  public void roundingSubstract() {
+    BigDecimal a = new BigDecimal(2.00);
+    BigDecimal b = new BigDecimal(0.99);
+  // assertThat(a.subtract(b).toString()).isEqualTo("1");
+  // assertThat(a.subtract(b).toString()).isEqualTo("1.01");
+  // assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625");
+  }
+}
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithFinally.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithFinally.java
new file mode 100644
index 00000000..2e62acf4
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithFinally.java
@@ -0,0 +1,40 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithFinally {
+
+  @Test
+  public void playingWithReturn() {
+    // assertThat(finallyReturn()).isEqualTo(42);
+  // assertThat(finallyReturn()).isEqualTo(1337);
+  }
+
+  @SuppressWarnings("finally")
+  private int finallyReturn() {
+    try {
+      return 42;
+    } finally {
+      return 1337;
+    }
+  }
+
+  @Test
+  public void playingWithThrow() {
+    // assertThat(finallyThrow()).isEqualTo(42);
+  // assertThatThrownBy(() -> finallyThrow())
+  // .isInstanceOf(IllegalArgumentException.class)
+  // .hasMessageContaining("Damned");
+  }
+
+  @SuppressWarnings("finally")
+  private int finallyThrow() {
+    try {
+      return 42;
+    } finally {
+      throw new IllegalArgumentException("Damned");
+    }
+  }
+}
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithInstanciations.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithInstanciations.java
new file mode 100644
index 00000000..34c32f54
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithInstanciations.java
@@ -0,0 +1,27 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithInstanciations {
+  private static final long instanciation = System.currentTimeMillis();
+
+  @Test
+  public void playingWithInstanciationTime() throws InterruptedException {
+    Thread.sleep(10);
+    long start = System.currentTimeMillis();
+
+    // assertThat(start).isLessThanOrEqualTo(instanciation);
+    // assertThat(start).isGreaterThan(instanciation);
+    MyPrivateClass.doStuff();
+  // assertThat(start).isLessThanOrEqualTo(MyPrivateClass.instanciation);
+  // assertThat(start).isGreaterThan(MyPrivateClass.instanciation);
+  }
+
+  private static class MyPrivateClass {
+    private static final long instanciation = System.currentTimeMillis();
+
+    private static void doStuff() {}
+  }
+}
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithInteger.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithInteger.java
new file mode 100644
index 00000000..547c5670
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithInteger.java
@@ -0,0 +1,31 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithInteger {
+
+  @Test
+  public void comparingUniversaleAnswerIntegerReferences() {
+    Integer a = 42;
+    Integer b = 42;
+  // assertThat(a).isSameAs(b);
+  // assertThat(a).isNotSameAs(b);
+  }
+
+  @Test
+  public void comparingDevilIntegerReferences() {
+    Integer a = 666;
+    Integer b = 666;
+  // assertThat(a).isSameAs(b);
+  // assertThat(a).isNotSameAs(b);
+  }
+
+  @Test
+  public void checkingAbsoluteValue() {
+    // assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE);
+  // assertThat(Math.abs(Integer.MIN_VALUE))
+  // .isEqualTo(Integer.MIN_VALUE);
+  }
+}
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithString.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithString.java
new file mode 100644
index 00000000..1210d5b9
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithString.java
@@ -0,0 +1,28 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithString {
+
+  @Test
+  public void playingWithReferences() {
+    // assertThat("coucou").isSameAs("coucou");
+  // assertThat("coucou").isNotSameAs("coucou");
+  }
+
+  @Test
+  public void playingWithStringObjectsReferences() {
+    // assertThat(new String("coucou")).isSameAs(new String("coucou"));
+  // assertThat(new String("coucou")).isNotSameAs(new String("coucou"));
+  }
+
+  @Test
+  public void playingWithIntern() {
+    // assertThat(new String("coucou").intern())
+  // .isSameAs(new String("coucou").intern());
+  // assertThat(new String("coucou").intern()).isNotSameAs(new
+  // String("coucou").intern());
+  }
+}
diff --git a/java-puzzles/src/test/java/fr/ippon/PlayingWithThreads.java b/java-puzzles/src/test/java/fr/ippon/PlayingWithThreads.java
new file mode 100644
index 00000000..559432d2
--- /dev/null
+++ b/java-puzzles/src/test/java/fr/ippon/PlayingWithThreads.java
@@ -0,0 +1,113 @@
+package fr.ippon;
+
+import static org.assertj.core.api.Assertions.*;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.jupiter.api.Test;
+
+public class PlayingWithThreads {
+  private static boolean threadDoneFlag;
+
+  private static AtomicBoolean threadDoneAtomicFlag = new AtomicBoolean(false);
+
+  @Test
+  public void playingWithSimpleDateFormat() throws InterruptedException {
+    Set<Integer> days = Collections.newSetFromMap(new ConcurrentHashMap<>());
+    ExecutorService executor = Executors.newFixedThreadPool(10);
+
+    AtomicInteger counter = new AtomicInteger(1);
+    for (int i = 1; i < 32; ++i) {
+      executor.submit(
+        () -> {
+          SimpleDateFormat format = new SimpleDateFormat("dd");
+          Date date = new GregorianCalendar(2018, Calendar.JANUARY, counter.getAndIncrement()).getTime();
+          days.add(Integer.parseInt(format.format(date)));
+        }
+      );
+    }
+
+    executor.shutdown();
+    executor.awaitTermination(3, TimeUnit.SECONDS);
+  // assertThat(days).hasSize(31);
+  // assertThat(days.size()).isLessThan(31);
+  }
+
+  @Test
+  public void firstAttemptToStopAThread() throws InterruptedException {
+    AtomicBoolean threadStopped = new AtomicBoolean();
+
+    new Thread(
+      () -> {
+        while (!threadDoneFlag) {}
+
+        threadStopped.set(true);
+      }
+    )
+    .start();
+
+    Thread.sleep(500);
+
+    threadDoneFlag = true;
+
+    Thread.sleep(50);
+  // assertThat(threadStopped.get()).isFalse();
+  // assertThat(threadStopped.get()).isTrue();
+  }
+
+  @Test
+  public void secondAttemptToStopAThread() throws InterruptedException {
+    AtomicBoolean threadStopped = new AtomicBoolean();
+
+    new Thread(
+      () -> {
+        while (!threadDoneFlag) {
+          System.out.println(threadDoneFlag); // Ok, let's just log the flag
+        }
+
+        threadStopped.set(true);
+      }
+    )
+    .start();
+
+    Thread.sleep(500);
+
+    threadDoneFlag = true;
+
+    Thread.sleep(50);
+  // assertThat(threadStopped.get()).isFalse();
+  // assertThat(threadStopped.get()).isTrue();
+  }
+
+  @Test
+  public void thirdAttemptToStopAThread() throws InterruptedException {
+    AtomicBoolean threadStopped = new AtomicBoolean();
+
+    new Thread(
+      () -> {
+        while (!threadDoneAtomicFlag.get()) {}
+
+        threadStopped.set(true);
+      }
+    )
+    .start();
+
+    Thread.sleep(500);
+
+    threadDoneAtomicFlag.set(true);
+
+    Thread.sleep(50);
+  // assertThat(threadStopped.get()).isFalse();
+  // assertThat(threadStopped.get()).isTrue();
+  }
+}
diff --git a/readme.md b/readme.md
index 05d94138..d83bded7 100644
--- a/readme.md
+++ b/readme.md
@@ -43,6 +43,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
 -   [Movie rental](/movie-rental)
 -   [Bowling Game](/bowling-game)
 -   [Tennis Refactoring kata](/tennis/refactoring)
+-   [Puzzles](java-puzzles)
 
 ### Énervé
 
-- 
GitLab