345449.vhj5l3oj7.asiaDynaTrash.java2001-12-26T16:00:00Z2001-12-26T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:dynatrash:DynaTrash.java
// Using a Map of Lists and RTTI
// to automatically sort trash into
// ArrayLists. This solution, despite the
// use of RTTI, is extensible.
import c12.trash.*;
import java.util.*;
import com.bruceeckel.test.*;
// Generic TypeMap works in any situation:
class TypeMap {
private Map t = new HashMap();
public void add(Object o) {
Class type = o.getClass();
if(t.containsKey(type))
((List)t.get(type)).add(o);
else {
List v = new ArrayList();
v.add(o);
t.put(type,v);
}
}
public List get(Class type) {
return (List)t.get(type);
}
public Iterator keys() {
return t.keySet().iterator();
}
}
// Adapter class to allow callbacks
// from ParseTrash.fillBin():
class TypeMapAdapter implements Fillable {
TypeMap map;
public TypeMapAdapter(TypeMap tm) { map = tm; }
public void addTrash(Trash t) { map.add(t); }
}
public class DynaTrash extends UnitTest {
TypeMap bin = new TypeMap();
public DynaTrash() {
ParseTrash.fillBin("../trash/Trash.dat",
new TypeMapAdapter(bin));
}
public void test() {
Iterator keys = bin.keys();
while(keys.hasNext())
Trash.sumValue(
bin.get((Class)keys.next()).iterator());
}
public static void main(String args[]) {
new DynaTrash().test();
}
} ///:~
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2001-12-26T16:00:00Zmakefile2001-12-26T16:00:00Z2001-12-26T16:00:00Z<br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel
# At http://www.BruceEckel.com
# (c)2001 Bruce Eckel
# Copyright notice in Copyright.txt
# Automatically-generated MAKEFILE
# For examples in directory .\c12\dynatrash
# using the JDK 1.3 compiler
# Invoke with: make
HOME := ../../
ifndef MAKECMDGOALS
MAKECMDGOALS := javac
endif
# Command.com is too weak to build this under Windows NT/2000:
ifeq ($(OS),Windows_NT)
COMSPEC=$(SYSTEMROOT)\system32\cmd.exe
endif
ifneq ($(MAKECMDGOALS),clean)
include $(HOME)/$(MAKECMDGOALS).mac
endif
.SUFFIXES : .class .java
.java.class :
$(JVC) $(JVCFLAGS) $<
javac: \
DynaTrash.class
jikes: \
DynaTrash.class
clean:
ifeq ($(notdir $(SHELL)),COMMAND.COM)
del *.class
else
rm -f *.class
endif
DynaTrash.class: DynaTrash.java
$(JVC) $(JVCFLAGS) $<
java com.bruceeckel.test.RunUnitTests DynaTrash
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2001-12-26T16:00:00Z