Teste.java
jmud-0.11/src/Teste.java · 2.9 KB
From the archive jmud-0_11.tgz
Download the archive « Back to the archive contents
import util.jgp.algorithm.Searcher;
import util.jgp.algorithm.Sorter;
import util.jgp.algorithm.QuickSorter;
import util.jgp.algorithm.InsertionSorter;
import util.jgp.algorithm.Transformer;
import util.jgp.predicate.UnaryPredicate;
import util.jgp.predicate.BinaryPredicate;
import util.jgp.functor.UnaryComparator;
import util.jgp.functor.UnaryFunction;
import util.jgp.container.Vector;
public class Teste {
public static void main(String args[]) {
Vector v = new Vector();
final int MAX = args.length > 0 ? Integer.parseInt(args[0]) : 1000;
for (int i = MAX; i > 0; --i)
v.insert(new Integer(Rnd.nextInt()));
int val = 30;
IntegerLessThanComparator intLessThan = new IntegerLessThanComparator();
System.out.println("Ordenando");
//Sorter.quickSort(v, intLessThan); // QuickSort
(new QuickSorter(v, intLessThan)).sort(); // improvedQuickSort
//Sorter.insertionSort(v, intLessThan); // insertionSort
//(new InsertionSorter(v, intLessThan)).sort(); // improvedInsertionSort
System.out.println("Pronto");
/*
IntegerEqual intEqual = new IntegerEqual(val);
System.out.println("Procurando por: " + val);
Object o = Searcher.linearSearch(v, intEqual, 1);
if (o == null)
System.out.println("Não encontrado!");
else
System.out.println("Encontrado: " + ((Integer) o).intValue());
for (int i = 0; i < MAX; ++i)
System.out.print(v.getElementAt(i) + " ");
System.out.println();
IntegerComparator intMatch = new IntegerComparator(val);
System.out.println("Procurando por: " + val);
int ind = Searcher.binarySearch(v, intLessThan, intMatch);
if (ind == -1)
System.out.println("Não encontrado!");
else
System.out.println("Encontrado: " + v.getElementAt(ind));
System.out.println("Listando números pares: ");
ListNumber listInt = new ListNumber();
IsEvenNumber isEven = new IsEvenNumber();
Transformer.applyIf(v, listInt, isEven);
*/
System.out.println();
}
}
class IntegerLessThanComparator implements BinaryPredicate {
public boolean execute(Object obj1, Object obj2) {
return ((Integer) obj1).intValue() < ((Integer) obj2).intValue();
}
}
class IntegerEqual implements UnaryPredicate {
private int value;
IntegerEqual(int val) {
value = val;
}
public boolean execute(Object obj) {
return ((Integer) obj).intValue() == value;
}
}
class IntegerComparator implements UnaryComparator {
private int value;
IntegerComparator(int val) {
value = val;
}
public int execute(Object obj) {
System.out.println("[" + obj + "]");
int i = ((Integer) obj).intValue();
if (i < value)
return -1;
else if (i > value)
return 1;
else
return 0;
}
}
class ListNumber implements UnaryFunction {
public Object execute(Object obj) {
System.out.print("(" + ((Integer) obj).intValue() + ") ");
return null;
}
}
class IsEvenNumber implements UnaryPredicate {
public boolean execute(Object obj) {
return ((Integer) obj).intValue() % 2 == 0;
}
}