From: <lh...@us...> - 2008-04-25 20:11:44
|
Revision: 37 http://tinytim.svn.sourceforge.net/tinytim/?rev=37&view=rev Author: lheuer Date: 2008-04-25 13:09:10 -0700 (Fri, 25 Apr 2008) Log Message: ----------- - Small modifications - Event system simplified - Bugs in ScopedIndex fixed Modified Paths: -------------- tinytim/trunk/src/main/java/org/tinytim/Construct.java tinytim/trunk/src/main/java/org/tinytim/IdentityManager.java tinytim/trunk/src/main/java/org/tinytim/MergeUtils.java tinytim/trunk/src/main/java/org/tinytim/Property.java tinytim/trunk/src/main/java/org/tinytim/Scoped.java tinytim/trunk/src/main/java/org/tinytim/TopicMapImpl.java tinytim/trunk/src/main/java/org/tinytim/TopicUtils.java tinytim/trunk/src/main/java/org/tinytim/index/TypeInstanceIndex.java tinytim/trunk/src/test/java/org/tinytim/TestTMAPICore.java tinytim/trunk/src/test/java/org/tinytim/TestTMAPIIndex.java tinytim/trunk/src/test/java/org/tinytim/TestTopicMapMerge.java Modified: tinytim/trunk/src/main/java/org/tinytim/Construct.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/Construct.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/Construct.java 2008-04-25 20:09:10 UTC (rev 37) @@ -125,18 +125,18 @@ * @see java.lang.Object#equals(java.lang.Object) */ @Override - public boolean equals(Object obj) { + public final boolean equals(Object obj) { if (this == obj) { return true; } - return (obj instanceof Construct) && _id.equals(((Construct) obj)._id); + return (obj instanceof Construct) && _id.equals(((Construct) obj).getObjectId()); } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override - public int hashCode() { + public final int hashCode() { return _id.hashCode(); } Modified: tinytim/trunk/src/main/java/org/tinytim/IdentityManager.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/IdentityManager.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/IdentityManager.java 2008-04-25 20:09:10 UTC (rev 37) @@ -53,6 +53,11 @@ _register(tm); } + /** + * Subscribes itself to the specified event publisher. + * + * @param publisher The publisher to subscribe to. + */ private void _subscribe(IEventPublisher publisher) { IEventHandler handler = new TopicMapsConstructAddHandler(); publisher.subscribe(Event.ADD_TOPIC, handler); @@ -84,32 +89,66 @@ publisher.subscribe(Event.SET_REIFIER, handler); } + /** + * Registeres a Topic Maps construct and, if necessary, gives it an id. + * + * @param construct The construct to register. + */ private void _register(IConstruct construct) { Construct c = (Construct) construct; if (c._id == null) { c._id = "" + _nextId++; } - if (!_id2Construct.containsKey(c)) { + if (!_id2Construct.containsKey(c._id)) { _id2Construct.put(c._id, c); } } + /** + * Unregisteres the specified <code>construct</code>. + * + * @param construct The Topic Maps construct to unregister. + */ private void _unregister(IConstruct construct) { _id2Construct.remove(((Construct) construct)._id); } + /** + * Returns a Topic Maps construct by its identifier. + * + * @param id The identifier. + * @return A Topic Maps construct with the <code>id</code> or <code>null</code>. + */ public IConstruct getConstructById(String id) { return _id2Construct.get(id); } + /** + * Returns a topic by its subject identifier. + * + * @param sid The subject identifier. + * @return A topic with the <code>sid</code> or <code>null</code>. + */ public Topic getTopicBySubjectIdentifier(Locator sid) { return _sid2Topic.get(sid); } + /** + * Returns a topic by its subject locator. + * + * @param slo The subject locator. + * @return A topic with the <code>slo</code> or <code>null</code>. + */ public Topic getTopicBySubjectLocator(Locator slo) { return _slo2Topic.get(slo); } + /** + * Returns a Topic Maps construct by its item identifier. + * + * @param iid The item identifier. + * @return A Topic Maps construct with the <code>iid</code> or <code>null</code>. + */ public IConstruct getConstructByItemIdentifier(Locator iid) { return _iid2Construct.get(iid); } @@ -199,8 +238,7 @@ private class RemoveSubjectIdentifierHandler implements IEventHandler { public void handleEvent(Event evt, IConstruct sender, Object oldValue, Object newValue) { - Locator slo = (Locator) oldValue; - _sid2Topic.remove(slo); + _sid2Topic.remove(oldValue); } } @@ -227,8 +265,7 @@ private class RemoveSubjectLocatorHandler implements IEventHandler { public void handleEvent(Event evt, IConstruct sender, Object oldValue, Object newValue) { - Locator slo = (Locator) oldValue; - _slo2Topic.remove(slo); + _slo2Topic.remove(oldValue); } } Modified: tinytim/trunk/src/main/java/org/tinytim/MergeUtils.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/MergeUtils.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/MergeUtils.java 2008-04-25 20:09:10 UTC (rev 37) @@ -41,7 +41,7 @@ import org.tmapi.core.Variant; /** - * This class does provides functions to merge topic maps and topics. + * This class provides functions to merge topic maps and topics. * * This class relies on the implementation of tinyTiM, if the implementation * changes, check the <code>==</code> comparisons. Modified: tinytim/trunk/src/main/java/org/tinytim/Property.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/Property.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/Property.java 2008-04-25 20:09:10 UTC (rev 37) @@ -43,16 +43,16 @@ public static final String COLLECTION_FACTORY = "org.tinytim.CollectionFactory"; /** - * Property which indicates if the "old" XTM 1.0 reification mechansm should - * be used. + * Property which indicates if the "old" XTM 1.0 reification mechanism + * should be used. * * For backwards compatibilty and to support TMAPI 1.0 this property is * set to "true" by default. * * Note, that this property is likely to be removed in a future version and * that only the TMDM way of reification will be supported. - * Maybe you'll be able to use {@link org.tinytim.ReificationUtils} to support - * the XTM 1.0 reification mechanism. + * Maybe you'll be able to use {@link org.tinytim.ReificationUtils} to + * support the XTM 1.0 reification mechanism. */ public static final String XTM10_REIFICATION = "org.tinytim.XTM10Reification"; Modified: tinytim/trunk/src/main/java/org/tinytim/Scoped.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/Scoped.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/Scoped.java 2008-04-25 20:09:10 UTC (rev 37) @@ -27,8 +27,8 @@ import org.tmapi.core.Topic; /** - * Class that provides a "scope" property and sends events if that - * scope property changes. + * Class that provides a "scope" property and sends events if that property + * changes. * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ Modified: tinytim/trunk/src/main/java/org/tinytim/TopicMapImpl.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/TopicMapImpl.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/TopicMapImpl.java 2008-04-25 20:09:10 UTC (rev 37) @@ -422,16 +422,16 @@ switch (evt) { case ADD_TOPIC: _topicAdd((TopicImpl)newValue); break; case ADD_ASSOCIATION: _associationAdd((AssociationImpl)newValue); break; - case ADD_ROLE: _roleAdd((AssociationRoleImpl)newValue); break; - case ADD_OCCURRENCE: _occurrenceAdd((OccurrenceImpl)newValue); break; case ADD_NAME: _nameAdd((TopicNameImpl)newValue); break; - case ADD_VARIANT: _variantAdd((VariantImpl)newValue); break; + case ADD_ROLE: + case ADD_OCCURRENCE: + case ADD_VARIANT: _constructAdd((IConstruct)newValue); break; case REMOVE_TOPIC: _topicRemove((TopicImpl) oldValue); break; case REMOVE_ASSOCIATION: _associationRemove((AssociationImpl) oldValue); break; - case REMOVE_ROLE: _roleRemove((AssociationRoleImpl) oldValue); break; - case REMOVE_OCCURRENCE: _occurrenceRemove((OccurrenceImpl) oldValue); break; - case REMOVE_NAME: _nameRemove((TopicNameImpl) oldValue); break; - case REMOVE_VARIANT: _variantRemove((VariantImpl) oldValue); break; + case REMOVE_NAME: _nameRemove((TopicNameImpl)oldValue); break; + case REMOVE_ROLE: + case REMOVE_OCCURRENCE: + case REMOVE_VARIANT: _constructRemove((IConstruct) oldValue); break; } } @@ -456,62 +456,18 @@ private void _associationAdd(AssociationImpl sender) { _constructAdd(sender); - _typedAdd(sender); - _scopedAdd(sender); for (AssociationRole role: sender.getAssociationRoles()) { _handler.handleEvent(Event.ADD_ROLE, sender, null, role); } } - private void _roleAdd(AssociationRoleImpl sender) { - _constructAdd(sender); - _typedAdd(sender); - _handler.handleEvent(Event.SET_PLAYER, sender, null, sender.getPlayer()); - } - - private void _occurrenceAdd(OccurrenceImpl sender) { - _constructAdd(sender); - _typedAdd(sender); - _scopedAdd(sender); - if (sender.getValue() != null) { - _handler.handleEvent(Event.SET_VALUE, sender, null, sender.getValue()); - } - else if (sender.getResource() != null) { - _handler.handleEvent(Event.SET_LOCATOR, sender, null, sender.getResource()); - } - } - private void _nameAdd(TopicNameImpl sender) { _constructAdd(sender); - _typedAdd(sender); - _scopedAdd(sender); - _handler.handleEvent(Event.SET_VALUE, sender, null, sender.getValue()); for (Variant variant: sender.getVariants()) { _handler.handleEvent(Event.ADD_VARIANT, sender, null, variant); } } - private void _variantAdd(VariantImpl sender) { - _constructAdd(sender); - _scopedAdd(sender); - if (sender.getValue() != null) { - _handler.handleEvent(Event.SET_VALUE, sender, null, sender.getValue()); - } - else if (sender.getResource() != null) { - _handler.handleEvent(Event.SET_LOCATOR, sender, null, sender.getResource()); - } - } - - private void _typedAdd(ITyped typed) { - _handler.handleEvent(Event.SET_TYPE, typed, null, typed.getType()); - } - - private void _scopedAdd(IScoped scoped) { - for (Topic theme: scoped.getScope()) { - _handler.handleEvent(Event.ADD_THEME, scoped, null, theme); - } - } - private void _constructAdd(IConstruct construct) { for (Locator iid: construct.getItemIdentifiers()) { _handler.handleEvent(Event.ADD_IID, construct, null, iid); @@ -550,14 +506,6 @@ } } - private void _roleRemove(AssociationRoleImpl sender) { - _constructRemove(sender); - } - - private void _occurrenceRemove(OccurrenceImpl sender) { - _constructRemove(sender); - } - private void _nameRemove(TopicNameImpl sender) { _constructRemove(sender); for (Variant variant: sender.getVariants()) { @@ -565,8 +513,5 @@ } } - private void _variantRemove(VariantImpl sender) { - _constructRemove(sender); - } } } Modified: tinytim/trunk/src/main/java/org/tinytim/TopicUtils.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/TopicUtils.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/TopicUtils.java 2008-04-25 20:09:10 UTC (rev 37) @@ -70,7 +70,7 @@ * otherwise. */ public static boolean isRemovable(Topic topic, boolean includeReified) { - if (includeReified && ((TopicImpl) topic)._reified != null) { + if (includeReified && ((TopicImpl) topic).getReifiedConstruct() != null) { return false; } if (!topic.getRolesPlayed().isEmpty()) { Modified: tinytim/trunk/src/main/java/org/tinytim/index/TypeInstanceIndex.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/index/TypeInstanceIndex.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/main/java/org/tinytim/index/TypeInstanceIndex.java 2008-04-25 20:09:10 UTC (rev 37) @@ -69,6 +69,11 @@ publisher.subscribe(Event.REMOVE_TOPIC, handler); handler = new TypeHandler(); publisher.subscribe(Event.SET_TYPE, handler); + handler = new AddTypedHandler(); + publisher.subscribe(Event.ADD_ASSOCIATION, handler); + publisher.subscribe(Event.ADD_ROLE, handler); + publisher.subscribe(Event.ADD_OCCURRENCE, handler); + publisher.subscribe(Event.ADD_NAME, handler); handler = new RemoveTypedHandler(); publisher.subscribe(Event.REMOVE_ASSOCIATION, handler); publisher.subscribe(Event.REMOVE_ROLE, handler); @@ -192,14 +197,26 @@ } } - private boolean _unindex(List<ITyped> objects, Object obj) { - if (objects == null) { - return false; + private void _index(Map<Topic, List<ITyped>> type2Typed, Topic type, ITyped typed) { + List<ITyped> list = type2Typed.get(type); + if (list == null) { + list = new ArrayList<ITyped>(); + type2Typed.put(type, list); } - objects.remove(obj); - return objects.isEmpty(); + list.add(typed); } + private void _unindex(Map<Topic, List<ITyped>> type2Typed, Topic type, ITyped typed) { + List<ITyped> list = type2Typed.get(type); + if (list == null) { + return; + } + list.remove(typed); + if (list.isEmpty()) { + type2Typed.remove(type); + } + } + /* (non-Javadoc) * @see org.tinytim.index.IIndex#close() */ @@ -308,64 +325,57 @@ } } - private final class TypeHandler implements IEventHandler { + private abstract class _EvtHandler implements IEventHandler { @SuppressWarnings("unchecked") - public void handleEvent(Event evt, IConstruct sender, Object oldValue, - Object newValue) { + Map<Topic, List<ITyped>> getMap(ITyped typed) { Map<Topic, ?> type2Typed = null; - if (sender instanceof AssociationImpl) { + if (typed instanceof AssociationImpl) { type2Typed = _type2Assocs; } - else if (sender instanceof AssociationRoleImpl) { + else if (typed instanceof AssociationRoleImpl) { type2Typed = _type2Roles; } - else if (sender instanceof OccurrenceImpl) { + else if (typed instanceof OccurrenceImpl) { type2Typed = _type2Occs; } - else if (sender instanceof TopicNameImpl) { + else if (typed instanceof TopicNameImpl) { type2Typed = _type2Names; } - if (_unindex((List<ITyped>)type2Typed.get((Topic)oldValue), sender)) { - type2Typed.remove(oldValue); - } - _index((Map<Topic, List<ITyped>>) type2Typed, (Topic) newValue, (ITyped) sender); + return (Map<Topic, List<ITyped>>) type2Typed; } + } - private void _index(Map<Topic, List<ITyped>> type2Typed, - Topic newValue, ITyped sender) { - List<ITyped> typedConstructs = type2Typed.get(newValue); - if (typedConstructs == null) { - typedConstructs = new ArrayList<ITyped>(); - type2Typed.put(newValue, typedConstructs); - } - typedConstructs.add(sender); + private final class TypeHandler extends _EvtHandler { + @SuppressWarnings("unchecked") + public void handleEvent(Event evt, IConstruct sender, Object oldValue, + Object newValue) { + ITyped typed = (ITyped) sender; + Map<Topic, List<ITyped>> map = getMap(typed); + _unindex(map, (Topic) oldValue, typed); + _index(map, (Topic) newValue, typed); } } - private final class RemoveTypedHandler implements IEventHandler { + private final class AddTypedHandler extends _EvtHandler { @SuppressWarnings("unchecked") public void handleEvent(Event evt, IConstruct sender, Object oldValue, Object newValue) { - Map<Topic, ?> type2Typed = null; - if (oldValue instanceof AssociationImpl) { - type2Typed = _type2Assocs; - } - else if (oldValue instanceof AssociationRoleImpl) { - type2Typed = _type2Roles; - } - else if (oldValue instanceof OccurrenceImpl) { - type2Typed = _type2Occs; - } - else if (oldValue instanceof TopicNameImpl) { - type2Typed = _type2Names; - } - Topic type = ((ITyped) oldValue).getType(); - if (_unindex((List<ITyped>)type2Typed.get(type), oldValue)) { - type2Typed.remove(type); - } + ITyped typed = (ITyped) newValue; + Map<Topic, List<ITyped>> map = getMap(typed); + _index(map, typed.getType(), typed); } } + private final class RemoveTypedHandler extends _EvtHandler { + @SuppressWarnings("unchecked") + public void handleEvent(Event evt, IConstruct sender, Object oldValue, + Object newValue) { + ITyped typed = (ITyped) oldValue; + Map<Topic, List<ITyped>> map = getMap(typed); + _unindex(map, typed.getType(), typed); + } + } + void clear() { _type2Topics.clear(); _type2Topics = null; Modified: tinytim/trunk/src/test/java/org/tinytim/TestTMAPICore.java =================================================================== --- tinytim/trunk/src/test/java/org/tinytim/TestTMAPICore.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/test/java/org/tinytim/TestTMAPICore.java 2008-04-25 20:09:10 UTC (rev 37) @@ -23,7 +23,6 @@ import org.tmapi.core.test.AllTMAPITests; import junit.framework.Test; -import junit.framework.TestSuite; /** * Runs the TMAPI core test suite against tinyTiM. @@ -31,15 +30,13 @@ * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ */ -public class TestTMAPICore extends TMAPITestCase { +public class TestTMAPICore extends AbstractTMAPITestSuite { public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { - TestSuite suite = new TestSuite(); - suite.addTest(AllTMAPITests.suite()); - return suite; + return AllTMAPITests.suite(); } } Modified: tinytim/trunk/src/test/java/org/tinytim/TestTMAPIIndex.java =================================================================== --- tinytim/trunk/src/test/java/org/tinytim/TestTMAPIIndex.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/test/java/org/tinytim/TestTMAPIIndex.java 2008-04-25 20:09:10 UTC (rev 37) @@ -23,7 +23,6 @@ import org.tmapi.index.core.test.AllTMAPIIndexTests; import junit.framework.Test; -import junit.framework.TestSuite; /** * Runs the TMAPI index test suite against tinyTiM. @@ -31,15 +30,13 @@ * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ */ -public class TestTMAPIIndex extends TMAPITestCase { +public class TestTMAPIIndex extends AbstractTMAPITestSuite { public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { - TestSuite suite = new TestSuite(); - suite.addTest(AllTMAPIIndexTests.suite()); - return suite; + return AllTMAPIIndexTests.suite(); } } Modified: tinytim/trunk/src/test/java/org/tinytim/TestTopicMapMerge.java =================================================================== --- tinytim/trunk/src/test/java/org/tinytim/TestTopicMapMerge.java 2008-04-24 13:43:47 UTC (rev 36) +++ tinytim/trunk/src/test/java/org/tinytim/TestTopicMapMerge.java 2008-04-25 20:09:10 UTC (rev 37) @@ -28,7 +28,7 @@ * Tests merging of topic maps. * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev:$ - $Date:$ + * @version $Rev$ - $Date$ */ public class TestTopicMapMerge extends TinyTimTestCase { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |