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

File AbstractComponentDescriptorEvent.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

10
26
12
1
161
87
18
0.69
2.17
12
1.5

Classes

Class Line # Actions
AbstractComponentDescriptorEvent 35 26 0% 18 7
0.854166785.4%
 

Contributing tests

This file is covered by 139 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.component.event;
21   
22    import java.lang.reflect.Type;
23    import java.util.Objects;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.apache.commons.lang3.builder.HashCodeBuilder;
27    import org.xwiki.component.util.ReflectionUtils;
28   
29    /**
30    * Base class for events about components descriptors.
31    *
32    * @version $Id: 8e9a39490da8f42700549301bd630d71ee7bc7b0 $
33    * @since 2.6RC2
34    */
 
35    public abstract class AbstractComponentDescriptorEvent implements ComponentDescriptorEvent
36    {
37    /**
38    * Component role.
39    */
40    private Type roleType;
41   
42    /**
43    * Component role hint.
44    */
45    private String roleHint;
46   
47    /**
48    * Watches all roles (whenever a component is added it'll trigger this event).
49    */
 
50  407 toggle public AbstractComponentDescriptorEvent()
51    {
52    }
53   
54    /**
55    * @param role the component role to watch (all components matching this role will trigger this event)
56    */
 
57  295 toggle public AbstractComponentDescriptorEvent(Class<?> role)
58    {
59  295 this((Type) role);
60    }
61   
62    /**
63    * @param roleType the component role type to watch (all components matching this role will trigger this event)
64    * @since 4.4RC1
65    */
 
66  301 toggle public AbstractComponentDescriptorEvent(Type roleType)
67    {
68  301 this.roleType = roleType;
69    }
70   
71    /**
72    * @param role the component role to watch
73    * @param roleHint the component rolehint to watch
74    */
 
75  0 toggle public AbstractComponentDescriptorEvent(Class<?> role, String roleHint)
76    {
77  0 this((Type) role, roleHint);
78    }
79   
80    /**
81    * @param roleType the component role to watch
82    * @param roleHint the component rolehint to watch
83    * @since 4.4RC1
84    */
 
85  22124 toggle public AbstractComponentDescriptorEvent(Type roleType, String roleHint)
86    {
87  22124 this.roleType = roleType;
88  22124 this.roleHint = roleHint;
89    }
90   
 
91  30286 toggle @Override
92    public Class<?> getRole()
93    {
94  30286 return ReflectionUtils.getTypeClass(getRoleType());
95    }
96   
 
97  81338 toggle @Override
98    public Type getRoleType()
99    {
100  81338 return this.roleType;
101    }
102   
 
103  7951 toggle @Override
104    public String getRoleHint()
105    {
106  7951 return this.roleHint;
107    }
108   
 
109  30286 toggle @Override
110    public boolean matches(Object otherEvent)
111    {
112  30286 boolean result = false;
113   
114  30286 if (otherEvent instanceof AbstractComponentDescriptorEvent) {
115    // If we're watching all roles return a match
116  30286 if (getRole() == null) {
117  12905 result = true;
118    } else {
119  17381 ComponentDescriptorEvent event = (ComponentDescriptorEvent) otherEvent;
120   
121  17381 if (getRoleType().equals(event.getRoleType())) {
122  2 result = getRoleHint() == null || getRoleHint().equals(event.getRoleHint());
123    }
124    }
125    }
126   
127  30286 return result;
128    }
129   
 
130  11 toggle @Override
131    public boolean equals(Object obj)
132    {
133  11 if (obj == this) {
134  0 return true;
135    }
136   
137  11 if (obj != null && obj.getClass() == getClass()) {
138  7 return Objects.equals(getRoleType(), ((ComponentDescriptorEvent) obj).getRoleType())
139    && StringUtils.equals(getRoleHint(), ((ComponentDescriptorEvent) obj).getRoleHint());
140    }
141   
142  4 return false;
143    }
144   
 
145  6 toggle @Override
146    public int hashCode()
147    {
148  6 HashCodeBuilder builder = new HashCodeBuilder();
149   
150  6 builder.append(getRoleType());
151  6 builder.append(getRoleHint());
152   
153  6 return builder.toHashCode();
154    }
155   
 
156  0 toggle @Override
157    public String toString()
158    {
159  0 return getRoleType() + ":" + getRoleHint();
160    }
161    }