001package systems.dmx.core.impl; 002 003import systems.dmx.core.Association; 004import systems.dmx.core.DMXObject; 005import systems.dmx.core.Topic; 006import systems.dmx.core.model.AssociationModel; 007import systems.dmx.core.model.DMXObjectModel; 008import systems.dmx.core.model.TopicModel; 009 010import java.util.Iterator; 011 012 013 014/** 015 * An iterable over all topics stored in the DB. 016 */ 017class TopicIterable implements Iterable<Topic> { 018 019 private Iterator<Topic> topics; 020 021 TopicIterable(PersistenceLayer pl) { 022 this.topics = new TopicIterator(pl); 023 } 024 025 @Override 026 public Iterator<Topic> iterator() { 027 return topics; 028 } 029} 030 031/** 032 * An iterable over all associations stored in the DB. 033 */ 034class AssociationIterable implements Iterable<Association> { 035 036 private Iterator<Association> assocs; 037 038 AssociationIterable(PersistenceLayer pl) { 039 this.assocs = new AssociationIterator(pl); 040 } 041 042 @Override 043 public Iterator<Association> iterator() { 044 return assocs; 045 } 046} 047 048 049 050// === 051 052 053 054class TopicIterator extends ObjectIterator<Topic, TopicModelImpl> { 055 056 TopicIterator(PersistenceLayer pl) { 057 super(pl); 058 } 059 060 @Override 061 Iterator<TopicModelImpl> fetchObjects() { 062 return pl.fetchAllTopics(); 063 } 064 065 @Override 066 Topic instantiateObject(TopicModelImpl model) { 067 return pl.checkReadAccessAndInstantiate(model); 068 } 069} 070 071 072 073class AssociationIterator extends ObjectIterator<Association, AssociationModelImpl> { 074 075 AssociationIterator(PersistenceLayer pl) { 076 super(pl); 077 } 078 079 @Override 080 Iterator<AssociationModelImpl> fetchObjects() { 081 return pl.fetchAllAssociations(); 082 } 083 084 @Override 085 Association instantiateObject(AssociationModelImpl model) { 086 return pl.checkReadAccessAndInstantiate(model); 087 } 088} 089 090 091 092abstract class ObjectIterator<O extends DMXObject, M extends DMXObjectModelImpl> implements Iterator<O> { 093 094 // ---------------------------------------------------------------------------------------------- Instance Variables 095 096 protected PersistenceLayer pl; 097 private Iterator<M> objects; 098 099 // ---------------------------------------------------------------------------------------------------- Constructors 100 101 ObjectIterator(PersistenceLayer pl) { 102 this.pl = pl; 103 this.objects = fetchObjects(); 104 } 105 106 // -------------------------------------------------------------------------------------------------- Public Methods 107 108 @Override 109 public boolean hasNext() { 110 return objects.hasNext(); 111 } 112 113 @Override 114 public O next() { 115 return instantiateObject(objects.next()); 116 } 117 118 @Override 119 public void remove() { 120 objects.remove(); 121 } 122 123 // ----------------------------------------------------------------------------------------- Package Private Methods 124 125 abstract Iterator<M> fetchObjects(); 126 127 abstract O instantiateObject(M model); 128}