001    /*
002    @license.text@
003     */
004    package com.hammurapi.eventbus.tests.familyties.rules;
005    
006    import com.hammurapi.eventbus.Handler;
007    import com.hammurapi.eventbus.tests.familyties.FamilyTiesEventDispatchContext;
008    import com.hammurapi.eventbus.tests.familyties.model.Brother;
009    import com.hammurapi.eventbus.tests.familyties.model.Child;
010    import com.hammurapi.eventbus.tests.familyties.model.Sibling;
011    import com.hammurapi.eventbus.tests.familyties.model.Sister;
012    
013    public class SiblingRules extends FamilyTiesRules {     
014            
015            /**
016             * Male sibling is brother, female sibling is sister.
017             * If A is a sibling of B then B is a sibling of A.
018             * @param sibling
019             * @return Brother or Sister.
020             */
021            @Handler(posts={Sibling.class, Brother.class, Sister.class})
022            public Sibling infer(FamilyTiesEventDispatchContext dispatchContext, Sibling sibling) { 
023                    dispatchContext.post(new Sibling(sibling.getObject(), sibling.getSubject()));
024                    
025                    if (sibling.getSubject().isMale()) {
026                            if (!(sibling instanceof Brother)) {
027                                    return new  Brother(sibling.getSubject(), sibling.getObject());
028                            }                       
029                    } else {
030                            if (!(sibling instanceof Sister)) {
031                                    return new Sister(sibling.getSubject(), sibling.getObject());
032                            }                       
033                    }
034                    return null;
035            }
036    
037            /**
038             * If two children have common parent then they are siblings.
039             * @param child
040             * @param anotherChild
041             */
042            @Handler(value="java(*)://!child.getSubject().equals(anotherChild.getSubject()) && child.getObject().equals(anotherChild.getObject())", posts=Sibling.class)
043            public void infer(FamilyTiesEventDispatchContext dispatchContext, Child child, Child anotherChild) {
044                    dispatchContext.post(new Sibling(child.getSubject(), anotherChild.getSubject()));
045                    dispatchContext.post(new Sibling(anotherChild.getSubject(), child.getSubject()));
046            }
047    }