
Originally Posted by
Bryan
public abstract class Animals { public static final Integer CATS = 0; public static final Integer DOGS = 1; public static final Integer BIRDS = 2; protected Hashtable<Animal> animals = new Hashtable<Animal>(); //protected!!! public abstract Hashtable<Animal> collect(int specieType); public abstract void add(Animal a); public abstract Animal get(int id); public abstract void edit(Animal a); public abstract boolean remove(Animal a); } public class AnimalCollection extends Animals { private Hashtable<Animal> collection = new Hashtable<Animal>(); public Hashtable<Animal> collect(int specieType) { String specie; switch(specieType) { case 0: specie = "Cat"; break; case 1: specie = "Dog"; break; case 2: specie = "Bird"; break; default: throw new Exception("Unknown type."); } Hashtable<Animal> col = new hashtable<Animal>(); for(Animal a : super.animals) { if(a instanceof specie) { col.put(a.hashCode(), a); } } this.collection = col; //if you want to have the same collection some time later, you load this and dont call the method again(performance) return col; } @override public void add(Animal a) {} @override public Animal get(int id) {} @override public void edit(Animal a) {} @override public boolean remove(Animal a) {} } Call: AnimalCollection aC = new AnimalCollection(); Hashtable dogsCollection = aC.collect(Animals.DOGS); I made this with notepad++ so there may be some syntax errors.
Hmm, that needs a bit more work...
Don't forget that a Hashtable is a keyed collection, so should be declared with two generic type arguments - but a Hashtable won't work here if you want more than one instance of each species in the collection - Hashtable keys are unique.
You could profitably replace the Integer 'specieType' with an enum.
It would simplify things if an Animal knew its specieType - which it should.
Should AnimalCollection really extend Animals, or does this complicate things?
There seems little point in having a Hashtable of a single animal species, ArrayList would be more appropriate (especially if an Animal knows its Species)?
If all that's needed is to extract a particular species from a mixed collection, wouldn't something like this be simpler:
import java.util.ArrayList; import java.util.List; class AnimalFilter { public static void main(String[] args) { List<Animal> animals = new ArrayList<Animal>(); animals.add(new Dog(..)); animals.add(new Cat(..)); animals.add(new Bird(..)); animals.add(new Dog(..)); animals.add(new Bird(..)); AnimalFilter filter = new AnimalFilter(); List<Animal> dogsCollection = filter.collect(animals, Animal.Species.DOGS); ... // do something with Dogs } public List<Animal> collect(List<Animal> animals, Animal.Species species) { List<Animal> selectedAnimals = new ArrayList<Animal>(); for(Animal animal : animals) { if(animal.isSpecies(species)) { selectedAnimals.add(animal); } } return selectedAnimals; } } abstract class Animal { public enum Species { CATS, DOGS, BIRDS } protected Species species; public Species getSpecies() { return species; } public boolean isSpecies(Species s) { return species == s; } } class Dog extends Animal { ... } class Cat extends Animal { ... } class Bird extends Animal { ... } If an explicit Species type id isn't needed, you could even get rid of the Species enum and just use the Animal class type:
class AnimalFilter { public static void main(String[] args) { List<Animal> animals = new ArrayList<Animal>(); animals.add(new Dog(..)); animals.add(new Cat(..)); animals.add(new Bird(..)); animals.add(new Dog(..)); animals.add(new Bird(..)); AnimalFilter filter = new AnimalFilter(); List<Animal> dogsCollection = filter.collect(animals, Dog.class); ... // do something with Dogs } public List<Animal> collect(List<Animal> animals, Class speciesClass) { List<Animal> selectedAnimals = new ArrayList<Animal>(); for(Animal animal : animals) { if(animal.getClass().equals(speciesClass)) { selectedAnimals.add(animal); } } return selectedAnimals; } } abstract class Animal { ... } class Dog extends Animal { ... } class Cat extends Animal { ... } class Bird extends Animal { ... } YMMV.