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

File AbstractExtensionDependency.java

 

Coverage histogram

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

Code metrics

20
52
21
1
285
155
31
0.6
2.48
21
1.48

Classes

Class Line # Actions
AbstractExtensionDependency 40 52 0% 31 17
0.817204381.7%
 

Contributing tests

This file is covered by 112 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.extension;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.apache.commons.lang3.builder.EqualsBuilder;
30    import org.apache.commons.lang3.builder.HashCodeBuilder;
31    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
32    import org.xwiki.extension.version.VersionConstraint;
33   
34    /**
35    * Base class for {@link ExtensionDependency} implementations.
36    *
37    * @version $Id: 96e8c1c752e36d61e6fd81bb39ed400065eed9f7 $
38    * @since 4.0M1
39    */
 
40    public abstract class AbstractExtensionDependency implements ExtensionDependency
41    {
42    /**
43    * @see #getId()
44    */
45    protected String id;
46   
47    /**
48    * @see #getVersionConstraint()
49    */
50    protected VersionConstraint versionConstraint;
51   
52    /**
53    * @see #getRepositories()
54    */
55    protected List<ExtensionRepositoryDescriptor> repositories;
56   
57    /**
58    * @see #getProperties()
59    */
60    protected Map<String, Object> properties = new HashMap<String, Object>();
61   
62    /**
63    * Create new instance by cloning the provided one.
64    *
65    * @param dependency the extension dependency to copy
66    * @since 7.3M1
67    */
 
68  1033 toggle public AbstractExtensionDependency(ExtensionDependency dependency)
69    {
70  1033 this(dependency, null);
71    }
72   
73    /**
74    * Create new instance by cloning the provided one with different version constraint.
75    *
76    * @param dependency the extension dependency to copy
77    * @param versionConstraint the version constraint to set
78    */
 
79  1058 toggle public AbstractExtensionDependency(ExtensionDependency dependency, VersionConstraint versionConstraint)
80    {
81  1058 this(dependency.getId(), versionConstraint != null ? versionConstraint : dependency.getVersionConstraint(),
82    dependency.getProperties());
83    }
84   
85    /**
86    * @param id the id (or feature) of the extension dependency
87    * @param versionConstraint the version constraint of the extension dependency
88    */
 
89  722533 toggle public AbstractExtensionDependency(String id, VersionConstraint versionConstraint)
90    {
91  722533 this(id, versionConstraint, null);
92    }
93   
94    /**
95    * @param id the id (or feature) of the extension dependency
96    * @param versionConstraint the version constraint of the extension dependency
97    * @param properties the custom properties of the extension dependency
98    */
 
99  1314783 toggle public AbstractExtensionDependency(String id, VersionConstraint versionConstraint, Map<String, Object> properties)
100    {
101  1314783 this.id = id;
102  1314783 this.versionConstraint = versionConstraint;
103  1314783 if (properties != null) {
104  591097 this.properties.putAll(properties);
105    }
106    }
107   
 
108  3956714 toggle @Override
109    public String getId()
110    {
111  3956714 return this.id;
112    }
113   
114    /**
115    * @param id the extension id
116    * @see #getId()
117    */
 
118  0 toggle public void setId(String id)
119    {
120  0 this.id = id;
121    }
122   
 
123  3883060 toggle @Override
124    public VersionConstraint getVersionConstraint()
125    {
126  3883060 return this.versionConstraint;
127    }
128   
129    /**
130    * @param versionConstraint the version constraint of the target extension
131    */
 
132  0 toggle public void setVersionConstraint(VersionConstraint versionConstraint)
133    {
134  0 this.versionConstraint = versionConstraint;
135    }
136   
 
137  1101744 toggle @Override
138    public Collection<ExtensionRepositoryDescriptor> getRepositories()
139    {
140  1101744 return this.repositories != null ? this.repositories : Collections.<ExtensionRepositoryDescriptor>emptyList();
141    }
142   
143    /**
144    * @param repositories the custom repositories provided by the extension (usually to resolve dependencies)
145    * @since 7.3M1
146    */
 
147  723451 toggle public void setRepositories(Collection<? extends ExtensionRepositoryDescriptor> repositories)
148    {
149  723451 this.repositories = repositories != null ? Collections.unmodifiableList(new ArrayList<>(repositories)) : null;
150    }
151   
152    /**
153    * Add a new repository to the extension.
154    *
155    * @param repository a repository descriptor
156    * @since 7.3M1
157    */
 
158  42 toggle public void addRepository(ExtensionRepositoryDescriptor repository)
159    {
160  42 List<ExtensionRepositoryDescriptor> newrepositories =
161    new ArrayList<ExtensionRepositoryDescriptor>(getRepositories());
162  42 newrepositories.add(repository);
163   
164  42 this.repositories = Collections.unmodifiableList(newrepositories);
165    }
166   
 
167  8 toggle @Override
168    public boolean isCompatible(Extension extension)
169    {
170  8 if (isCompatible(extension.getId())) {
171  7 return true;
172    }
173   
174  1 for (ExtensionId extensionId : extension.getExtensionFeatures()) {
175  0 if (isCompatible(extensionId)) {
176  0 return true;
177    }
178    }
179   
180  1 return false;
181    }
182   
 
183  8 toggle @Override
184    public boolean isCompatible(ExtensionId extensionId)
185    {
186  8 return getId().equals(extensionId.getId()) && getVersionConstraint().isCompatible(extensionId.getVersion());
187    }
188   
 
189  2778511 toggle @Override
190    public Map<String, Object> getProperties()
191    {
192  2778511 return Collections.unmodifiableMap(this.properties);
193    }
194   
 
195  6475 toggle @Override
196    public Object getProperty(String key)
197    {
198  6475 return this.properties.get(key);
199    }
200   
201    /**
202    * Set a property.
203    *
204    * @param key the property key
205    * @param value the property value
206    * @see #getProperty(String)
207    */
 
208  2168290 toggle public void putProperty(String key, Object value)
209    {
210  2168290 this.properties.put(key, value);
211    }
212   
213    /**
214    * Replace existing properties with provided properties.
215    *
216    * @param properties the properties
217    */
 
218  1 toggle public void setProperties(Map<String, Object> properties)
219    {
220  1 this.properties.clear();
221  1 this.properties.putAll(properties);
222    }
223   
 
224  0 toggle @SuppressWarnings("unchecked")
225    @Override
226    public <T> T getProperty(String key, T def)
227    {
228  0 return this.properties.containsKey(key) ? (T) this.properties.get(key) : def;
229    }
230   
231    // Object
232   
 
233  353 toggle @Override
234    public String toString()
235    {
236  353 StringBuilder str = new StringBuilder();
237   
238  353 str.append(getId());
239   
240  353 if (getVersionConstraint() != null) {
241  353 str.append('-');
242  353 str.append(getVersionConstraint());
243    }
244   
245  353 return str.toString();
246    }
247   
 
248  2085483 toggle @Override
249    public int hashCode()
250    {
251  2085483 HashCodeBuilder builder = new HashCodeBuilder();
252   
253  2085483 builder.append(getId());
254  2085483 builder.append(getVersionConstraint());
255  2085483 builder.append(getProperties());
256   
257  2085483 return builder.toHashCode();
258    }
259   
 
260  549784 toggle @Override
261    public boolean equals(Object obj)
262    {
263  549784 if (this == obj) {
264  0 return true;
265    }
266   
267  549784 boolean equals;
268   
269  549784 if (obj instanceof ExtensionDependency) {
270  549784 ExtensionDependency otherDependency = (ExtensionDependency) obj;
271   
272  549784 EqualsBuilder builder = new EqualsBuilder();
273   
274  549784 builder.append(getId(), otherDependency.getId());
275  549784 builder.append(getVersionConstraint(), otherDependency.getVersionConstraint());
276  549784 builder.append(getRepositories(), otherDependency.getRepositories());
277   
278  549784 equals = builder.isEquals();
279    } else {
280  0 equals = false;
281    }
282   
283  549784 return equals;
284    }
285    }