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

File AbstractResourceReference.java

 

Coverage histogram

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

Code metrics

16
36
9
1
152
103
17
0.47
4
9
1.89

Classes

Class Line # Actions
AbstractResourceReference 41 36 0% 17 7
0.885245988.5%
 

Contributing tests

This file is covered by 97 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.resource;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.LinkedHashMap;
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.apache.commons.lang3.builder.ToStringBuilder;
32    import org.xwiki.text.XWikiToStringBuilder;
33   
34    /**
35    * Base XWiki Resource Reference implementation common to all extending classes. Manages XWiki Resource Reference
36    * parameters.
37    *
38    * @version $Id: 0be08c3e0b372a5cb0cd25db77cb4e3e53d70ab7 $
39    * @since 6.1M2
40    */
 
41    public abstract class AbstractResourceReference implements ResourceReference
42    {
43    /**
44    * @see #getParameters()
45    */
46    private final Map<String, List<String>> parameters = new LinkedHashMap<>();
47   
48    /**
49    * @see #getType()
50    */
51    private ResourceType type;
52   
 
53  11500 toggle @Override
54    public ResourceType getType()
55    {
56  11498 return this.type;
57    }
58   
59    /**
60    * @param type see {@link #getType()}
61    */
 
62  48726 toggle public void setType(ResourceType type)
63    {
64  48723 this.type = type;
65    }
66   
 
67  23860 toggle @Override
68    public void addParameter(String name, Object value)
69    {
70  23860 List<String> list = this.parameters.get(name);
71  23862 if (list == null) {
72  23862 list = new ArrayList<>();
73    }
74  23869 if (value != null) {
75    // If the value is a Collection or an Array then add a multivalued parameter!
76  23776 if (value.getClass().isArray()) {
77  4 Object[] objectValues = (Object[]) value;
78  4 for (Object objectValue : objectValues) {
79  8 list.add(objectValue.toString());
80    }
81  23771 } else if (Collection.class.isAssignableFrom(value.getClass())) {
82  14709 Collection<?> collectionValues = (Collection<?>) value;
83  14711 for (Object collectionValue : collectionValues) {
84  14947 list.add(collectionValue.toString());
85    }
86    } else {
87  9058 list.add(value.toString());
88    }
89    }
90  23868 this.parameters.put(name, list);
91    }
92   
 
93  35523 toggle @Override
94    public Map<String, List<String>> getParameters()
95    {
96  35517 return Collections.unmodifiableMap(this.parameters);
97    }
98   
 
99  1 toggle @Override
100    public List<String> getParameterValues(String name)
101    {
102  1 return this.parameters.get(name);
103    }
104   
 
105  3387 toggle @Override
106    public String getParameterValue(String name)
107    {
108  3387 String result = null;
109  3387 List<String> list = this.parameters.get(name);
110  3387 if (list != null) {
111  2414 result = list.get(0);
112    }
113  3387 return result;
114    }
115   
 
116  9506 toggle @Override
117    public String toString()
118    {
119  9511 ToStringBuilder builder = new XWikiToStringBuilder(this);
120  9515 builder.append("type", getType());
121  9497 builder.append("parameters", getParameters());
122  9514 return builder.toString();
123    }
124   
 
125  2 toggle @Override
126    public int hashCode()
127    {
128  2 return new HashCodeBuilder(9, 5)
129    .append(getType())
130    .append(getParameters())
131    .toHashCode();
132    }
133   
 
134  123 toggle @Override
135    public boolean equals(Object object)
136    {
137  123 if (object == null) {
138  0 return false;
139    }
140  123 if (object == this) {
141  0 return true;
142    }
143  123 if (object.getClass() != getClass()) {
144  0 return false;
145    }
146  123 ResourceReference rhs = (ResourceReference) object;
147  123 return new EqualsBuilder()
148    .append(getType(), rhs.getType())
149    .append(getParameters(), rhs.getParameters())
150    .isEquals();
151    }
152    }