1 | /* |
2 | @license.text@ |
3 | */ |
4 | package com.hammurapi.eventbus.tests.familyties.rules; |
5 | |
6 | import com.hammurapi.eventbus.Handler; |
7 | import com.hammurapi.eventbus.tests.familyties.FamilyTiesEventDispatchContext; |
8 | import com.hammurapi.eventbus.tests.familyties.model.Brother; |
9 | import com.hammurapi.eventbus.tests.familyties.model.Child; |
10 | import com.hammurapi.eventbus.tests.familyties.model.Sibling; |
11 | import com.hammurapi.eventbus.tests.familyties.model.Sister; |
12 | |
13 | public class SiblingRules extends FamilyTiesRules { |
14 | |
15 | /** |
16 | * Male sibling is brother, female sibling is sister. |
17 | * If A is a sibling of B then B is a sibling of A. |
18 | * @param sibling |
19 | * @return Brother or Sister. |
20 | */ |
21 | @Handler(posts={Sibling.class, Brother.class, Sister.class}) |
22 | public Sibling infer(FamilyTiesEventDispatchContext dispatchContext, Sibling sibling) { |
23 | dispatchContext.post(new Sibling(sibling.getObject(), sibling.getSubject())); |
24 | |
25 | if (sibling.getSubject().isMale()) { |
26 | if (!(sibling instanceof Brother)) { |
27 | return new Brother(sibling.getSubject(), sibling.getObject()); |
28 | } |
29 | } else { |
30 | if (!(sibling instanceof Sister)) { |
31 | return new Sister(sibling.getSubject(), sibling.getObject()); |
32 | } |
33 | } |
34 | return null; |
35 | } |
36 | |
37 | /** |
38 | * If two children have common parent then they are siblings. |
39 | * @param child |
40 | * @param anotherChild |
41 | */ |
42 | @Handler(value="java(*)://!child.getSubject().equals(anotherChild.getSubject()) && child.getObject().equals(anotherChild.getObject())", posts=Sibling.class) |
43 | public void infer(FamilyTiesEventDispatchContext dispatchContext, Child child, Child anotherChild) { |
44 | dispatchContext.post(new Sibling(child.getSubject(), anotherChild.getSubject())); |
45 | dispatchContext.post(new Sibling(anotherChild.getSubject(), child.getSubject())); |
46 | } |
47 | } |