diff --git a/src/td10/Couple.java b/src/td10/Couple.java new file mode 100644 index 0000000000000000000000000000000000000000..923fb0c58daf18e7255528976cd01a4f72d5d3e6 --- /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 0000000000000000000000000000000000000000..be30ff17636f6193d4ea180543fc96fea68dc71a --- /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 0000000000000000000000000000000000000000..f4ce554af962bcc474aae399f4a09d430c5ba1a9 --- /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