Skip to content
Snippets Groups Projects
Commit 888d5935 authored by Samuel Turpin's avatar Samuel Turpin :computer:
Browse files

td10 générique

parent 67371a73
Branches
No related tags found
No related merge requests found
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
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment