1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.event

File AbstractComponentDescriptorEvent.java

 

Coverage histogram

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

Code metrics

10
26
12
1
165
89
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 137 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: 109bc1aa900f3db78bc1a63cd15dd1ce49dcecc3 $
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  207 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    * @deprecated since 4.4RC1 use {@link #AbstractComponentDescriptorEvent(Type)} instead
57    */
 
58  112 toggle @Deprecated
59    public AbstractComponentDescriptorEvent(Class<?> role)
60    {
61  112 this((Type) role);
62    }
63   
64    /**
65    * @param roleType the component role type to watch (all components matching this role will trigger this event)
66    * @since 4.4RC1
67    */
 
68  118 toggle public AbstractComponentDescriptorEvent(Type roleType)
69    {
70  118 this.roleType = roleType;
71    }
72   
73    /**
74    * @param role the component role to watch
75    * @param roleHint the component rolehint to watch
76    * @deprecated since 4.4RC1 use {@link #AbstractComponentDescriptorEvent(Type, String)} instead
77    */
 
78  0 toggle @Deprecated
79    public AbstractComponentDescriptorEvent(Class<?> role, String roleHint)
80    {
81  0 this((Type) role, roleHint);
82    }
83   
84    /**
85    * @param roleType the component role to watch
86    * @param roleHint the component rolehint to watch
87    * @since 4.4RC1
88    */
 
89  15413 toggle public AbstractComponentDescriptorEvent(Type roleType, String roleHint)
90    {
91  15413 this.roleType = roleType;
92  15413 this.roleHint = roleHint;
93    }
94   
 
95  1903 toggle @Override
96    public Class<?> getRole()
97    {
98  1903 return ReflectionUtils.getTypeClass(getRoleType());
99    }
100   
 
101  5469 toggle @Override
102    public Type getRoleType()
103    {
104  5469 return this.roleType;
105    }
106   
 
107  53 toggle @Override
108    public String getRoleHint()
109    {
110  53 return this.roleHint;
111    }
112   
 
113  1903 toggle @Override
114    public boolean matches(Object otherEvent)
115    {
116  1903 boolean result = false;
117   
118  1903 if (otherEvent instanceof AbstractComponentDescriptorEvent) {
119    // If we're watching all roles return a match
120  1903 if (getRole() == null) {
121  685 result = true;
122    } else {
123  1218 ComponentDescriptorEvent event = (ComponentDescriptorEvent) otherEvent;
124   
125  1218 if (getRoleType().equals(event.getRoleType())) {
126  2 result = getRoleHint() == null || getRoleHint().equals(event.getRoleHint());
127    }
128    }
129    }
130   
131  1903 return result;
132    }
133   
 
134  11 toggle @Override
135    public boolean equals(Object obj)
136    {
137  11 if (obj == this) {
138  0 return true;
139    }
140   
141  11 if (obj != null && obj.getClass() == getClass()) {
142  7 return Objects.equals(getRoleType(), ((ComponentDescriptorEvent) obj).getRoleType())
143    && StringUtils.equals(getRoleHint(), ((ComponentDescriptorEvent) obj).getRoleHint());
144    }
145   
146  4 return false;
147    }
148   
 
149  6 toggle @Override
150    public int hashCode()
151    {
152  6 HashCodeBuilder builder = new HashCodeBuilder();
153   
154  6 builder.append(getRoleType());
155  6 builder.append(getRoleHint());
156   
157  6 return builder.toHashCode();
158    }
159   
 
160  0 toggle @Override
161    public String toString()
162    {
163  0 return getRoleType() + ":" + getRoleHint();
164    }
165    }