From 888d59355b831652815460af844811ce1b6f270c Mon Sep 17 00:00:00 2001
From: Samuel Turpin <samuel.turpin.etu@univ-lille.fr>
Date: Tue, 4 Jun 2024 10:37:30 +0200
Subject: [PATCH] =?UTF-8?q?td10=20g=C3=A9n=C3=A9rique?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/td10/Couple.java |  33 ++++++++++++++
 src/td10/Duo.java    | 102 +++++++++++++++++++++++++++++++++++++++++++
 src/td10/Main.java   |  36 +++++++++++++++
 3 files changed, 171 insertions(+)
 create mode 100644 src/td10/Couple.java
 create mode 100644 src/td10/Duo.java
 create mode 100644 src/td10/Main.java

diff --git a/src/td10/Couple.java b/src/td10/Couple.java
new file mode 100644
index 0000000..923fb0c
--- /dev/null
+++ b/src/td10/Couple.java
@@ -0,0 +1,33 @@
+package td10;
+
+public class Couple<T,S>
+{
+    private T first;
+    private S second;
+
+    public Couple(T t, S s)
+    {
+        this.first = t;
+        this.second = s;
+    }
+
+    public T getFirst()
+    {
+        return this.first;
+    }
+
+    public S getSecond()
+    {
+        return this.second;
+    }
+
+    public boolean sameAs(Couple<T,S> other)
+    {
+        return this.first.equals(other.first) && this.second.equals(other.second);
+    }
+
+    public static<T,S> boolean sameCouples(Couple<T,S> c1, Couple<T,S> c2)
+    {
+        return c1.sameAs(c2);
+    }
+}
\ No newline at end of file
diff --git a/src/td10/Duo.java b/src/td10/Duo.java
new file mode 100644
index 0000000..be30ff1
--- /dev/null
+++ b/src/td10/Duo.java
@@ -0,0 +1,102 @@
+package td10;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Duo
+ */
+public class Duo<E>
+{
+    private E one;
+    private E two;
+
+    public Duo(E e1, E e2)
+    {
+        this.one = e1;
+        this.two = e2;
+    }
+
+    public Duo(E e1)
+    {
+        this(e1, null);
+    }
+
+    public E getFirst()
+    {
+        return this.one;
+    }
+
+    public E getSecond()
+    {
+        return this.two;
+    }
+
+    public void swap()
+    {
+        E newOne = this.two;
+        E newSecond = this.one;
+        this.one = newOne;
+        this.two = newSecond;
+    }
+
+    public boolean identicalElements()
+    {
+        if(this.one == null && this.two == null)
+        {
+            return true;
+        }
+
+        if(this.one == null)
+        {
+            return false;
+        }
+        return this.one.equals(this.two);
+    }
+
+    public boolean orderedEquals(Duo<E> otherDuo)
+    {
+        return this.one.equals(otherDuo.getFirst()) && this.two.equals(otherDuo.getSecond());
+    }
+
+    public boolean reverseEquals(Duo<E> otherDuo)
+    {
+        return this.one.equals(otherDuo.getSecond()) && this.two.equals(otherDuo.getFirst());
+    }
+
+    public boolean nonOrderedEquals(Duo<E> otherDuo)
+    {
+        return this.reverseEquals(otherDuo) || this.orderedEquals(otherDuo);
+    }
+
+    public static<E> void mixDuos(Duo<E> d1, Duo<E> d2)
+    {
+        E tmp = d1.one;
+        d1.one = d2.two;
+        d2.two = tmp;
+    }
+
+    public static<E> List<Duo<E>> randomDuos(List<E> elements)
+    {
+        Collections.shuffle(elements);
+        List<Duo<E>> duos = new ArrayList<Duo<E>>();
+        while (elements.size() > 1)
+        {
+            duos.add(new Duo<E>(elements.remove(0), elements.remove(0)));
+        }
+
+        if(elements.size() == 1)
+        {
+            duos.add(new Duo<E>(elements.remove(0)));
+        }
+        return duos;
+    }
+
+    @Override
+    public String toString()
+    {
+        return this.one + " et " + this.two;
+    }
+}
\ No newline at end of file
diff --git a/src/td10/Main.java b/src/td10/Main.java
new file mode 100644
index 0000000..f4ce554
--- /dev/null
+++ b/src/td10/Main.java
@@ -0,0 +1,36 @@
+package td10;
+
+public class Main
+{
+    public static void main(String[] args)
+    {
+        //testDuos();
+        testCouple();
+    }
+
+    private static void testCouple()
+    {
+        Couple<String, String> c1 = new Couple<String,String>("alice", "bruno");
+        Couple<String, String> c2 = new Couple<String,String>("alice", "polo");
+
+        System.out.println(c1.sameAs(c2));
+        System.out.println(Couple.sameCouples(c1, c2));
+    }
+
+    private static void testDuos()
+    {
+        Duo<String> duoStr = new Duo<String>("bruno", "alice");
+        System.out.println(duoStr);
+        duoStr.swap();
+        System.out.println(duoStr);
+        Duo<Boolean> duoBool = new Duo<Boolean>(false, true);
+        System.out.println(duoBool);
+        duoBool.swap();
+        System.out.println(duoBool);
+
+        Duo<Integer> d1 = new Duo<Integer>(1, 2);
+        Duo<Integer> d2 = new Duo<Integer>(2, 1);
+        System.out.println(d1.orderedEquals(d2));
+        System.out.println(d1.nonOrderedEquals(d2));
+    }
+}
\ No newline at end of file
-- 
GitLab