1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications

File CompositeEvent.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

8
38
13
1
195
102
19
0.5
2.92
13
1.46

Classes

Class Line # Actions
CompositeEvent 40 38 0% 19 19
0.677966167.8%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.notifications;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.Date;
25    import java.util.HashSet;
26    import java.util.Iterator;
27    import java.util.List;
28    import java.util.Set;
29   
30    import org.xwiki.eventstream.Event;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.text.StringUtils;
33   
34    /**
35    * A group of similar events that compose a "composite" event.
36    *
37    * @version $Id: 1f037d86ae777e275d936d0a8e88b0cb0b428990 $
38    * @since 9.4RC1
39    */
 
40    public class CompositeEvent
41    {
42    private List<Event> events = new ArrayList<>();
43   
44    private int similarityBetweenEvents;
45   
46    /**
47    * Construct a CompositeEvent.
48    * @param event the first event of the composite event
49    */
 
50  2635 toggle public CompositeEvent(Event event)
51    {
52  2635 events.add(event);
53    }
54   
55    /**
56    * Copy constructor of {@link CompositeEvent}.
57    *
58    * @param compositeEvent the composite event to copy
59    */
 
60  0 toggle public CompositeEvent(CompositeEvent compositeEvent)
61    {
62  0 this.events = new ArrayList<>(compositeEvent.events);
63  0 this.similarityBetweenEvents = compositeEvent.similarityBetweenEvents;
64    }
65   
66    /**
67    * @return the list of the ids of the events
68    */
 
69  1561 toggle public List<String> getEventIds()
70    {
71  1561 ArrayList results = new ArrayList(events.size());
72  1561 for (Event event : events) {
73  3386 results.add(event.getId());
74    }
75  1561 return results;
76    }
77   
78    /**
79    * Add an event to the composite event.
80    * @param event the event to add
81    * @param similarity the similarity between the event to add and the events of the composite events
82    * @throws NotificationException if the addition is illegal (lower similarity for example)
83    */
 
84  2539 toggle public void add(Event event, int similarity) throws NotificationException
85    {
86  2539 if (similarity < similarityBetweenEvents) {
87  0 throw new NotificationException("Invalid addition of an event inside a CompositeEvent");
88    }
89  2539 similarityBetweenEvents = similarity;
90  2539 events.add(event);
91    // Ensure the events are always sorted by date (more recent firsts)
92  2539 Collections.sort(events, (e1, e2) -> e2.getDate().compareTo(e1.getDate()));
93    }
94   
95    /**
96    * @return the greatest similarity between events of the composite event
97    */
 
98  53025 toggle public int getSimilarityBetweenEvents()
99    {
100  53025 return similarityBetweenEvents;
101    }
102   
103    /**
104    * @return the events that compose the current object
105    */
 
106  101443 toggle public List<Event> getEvents()
107    {
108  101443 return events;
109    }
110   
111    /**
112    * @return the type of the events that compose the current object
113    */
 
114  7822 toggle public String getType()
115    {
116    // Fallback type
117  7822 String type = events.get(0).getType();
118  7822 for (Event event : events) {
119    // We are most interested in "advanced" event that we are in "core" events such as "create" or "update",
120    // which often are the technical consequences of the real event (ex: a comment has been added).
121  16958 if (StringUtils.isNotBlank(event.getType()) && !"create".equals(event.getType())
122    && !"update".equals(event.getType())) {
123  2075 type = event.getType();
124    }
125    }
126  7822 return type;
127    }
128   
129    /**
130    * @return the groupId of the first event of the current object
131    */
 
132  0 toggle public String getGroupId()
133    {
134  0 return events.get(0).getGroupId();
135    }
136   
137    /**
138    * @return the document of the first event of the current object
139    */
 
140  9396 toggle public DocumentReference getDocument()
141    {
142  9396 return events.get(0).getDocument();
143    }
144   
145    /**
146    * @return the users who performed the events
147    */
 
148  4687 toggle public Set<DocumentReference> getUsers()
149    {
150  4687 Set<DocumentReference> users = new HashSet();
151  4687 for (Event event : events) {
152  10162 users.add(event.getUser());
153    }
154  4687 return users;
155    }
156   
157    /**
158    * @return the date of the most recent event
159    * @since 9.11RC1
160    */
 
161  0 toggle public Date getDate()
162    {
163  0 Iterator<Event> it = events.iterator();
164  0 Event chosenEvent = it.next();
165  0 while (it.hasNext()) {
166  0 Event event = it.next();
167  0 if (event.getDate().compareTo(chosenEvent.getDate()) > 0) {
168  0 chosenEvent = event;
169    }
170    }
171  0 return chosenEvent.getDate();
172    }
173   
174    /**
175    * @return the dates of the events, sorted by descending order
176    */
 
177  3126 toggle public List<Date> getDates()
178    {
179  3126 List<Date> dates = new ArrayList<>();
180  3126 for (Event event : events) {
181  6776 dates.add(event.getDate());
182    }
183  3126 Collections.sort(dates, Collections.reverseOrder());
184  3126 return dates;
185    }
186   
187    /**
188    * Remove an event from the current object.
189    * @param event the event to remove
190    */
 
191  5 toggle public void remove(Event event)
192    {
193  5 events.remove(event);
194    }
195    }