Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
51   251   30   2.68
22   137   0.59   19
19     1.58  
1    
 
  WikiFormat       Line # 35 51 0% 30 18 80.4% 0.8043478
 
  (847)
 
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.rendering.wikimodel;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.LinkedHashSet;
26    import java.util.List;
27    import java.util.Set;
28   
29    /**
30    * An immutable set of styles.
31    *
32    * @version $Id: 80e4af6cf52247f16a6c0f25ce6cc4bfbcaf7e6c $
33    * @since 4.0M1
34    */
 
35    public class WikiFormat
36    {
37    public static WikiFormat EMPTY = new WikiFormat();
38   
39    private String fClosingTags;
40   
41    private String fOpeningTags;
42   
43    private LinkedHashSet<WikiStyle> fStyles = new LinkedHashSet<WikiStyle>();
44   
45    private WikiParameters fParams = WikiParameters.EMPTY;
46   
47    /**
48    *
49    */
 
50  13 toggle public WikiFormat()
51    {
52    }
53   
54    /**
55    * @param styles
56    */
 
57  2 toggle public WikiFormat(Set<WikiStyle> styles)
58    {
59  2 this(styles, Collections.<WikiParameter>emptyList());
60    }
61   
 
62  1371 toggle public WikiFormat(Set<WikiStyle> styles, Collection<WikiParameter> params)
63    {
64  1371 if (!styles.isEmpty()) {
65  643 fStyles.addAll(styles);
66    }
67  1371 if (!params.isEmpty()) {
68  326 fParams = new WikiParameters(params);
69    }
70    }
71   
 
72  75 toggle public WikiFormat(Collection<WikiParameter> params)
73    {
74  75 this(Collections.<WikiStyle>emptySet(), params);
75    }
76   
77    /**
78    * @param style
79    */
 
80  2 toggle public WikiFormat(WikiStyle style)
81    {
82  2 this(Collections.<WikiStyle>singleton(style));
83    }
84   
 
85  0 toggle public WikiFormat(WikiStyle style, Collection<WikiParameter> params)
86    {
87  0 this(Collections.<WikiStyle>singleton(style), params);
88    }
89   
90    /**
91    * @param styles
92    */
 
93  0 toggle public WikiFormat(WikiStyle[] styles)
94    {
95  0 super();
96  0 for (WikiStyle style : styles) {
97  0 fStyles.add(style);
98    }
99    }
100   
 
101  236 toggle public WikiFormat setParameters(Collection<WikiParameter> params)
102    {
103  236 return new WikiFormat(fStyles, params);
104    }
105   
106    /**
107    * Creates a new style set and adds the given style to it.
108    *
109    * @param style the style to add
110    * @return a new copy of the style set containing the given style
111    */
 
112  90 toggle public WikiFormat addStyle(WikiStyle style)
113    {
114  90 if (fStyles.contains(style)) {
115  3 return this;
116    }
117  87 WikiFormat clone = getClone();
118  87 clone.fStyles.add(style);
119  87 return clone;
120    }
121   
122    /**
123    * @see java.lang.Object#equals(java.lang.Object)
124    */
 
125  14904 toggle @Override
126    public boolean equals(Object obj)
127    {
128  14904 if (obj == this) {
129  10485 return true;
130    }
131  4419 if (!(obj instanceof WikiFormat)) {
132  4419 return false;
133    }
134  0 WikiFormat set = (WikiFormat) obj;
135  0 return fStyles.equals(set.fStyles) && fParams.equals(set.fParams);
136    }
137   
138    /**
139    * @return a new clone of this format object
140    */
 
141  847 toggle protected WikiFormat getClone()
142    {
143  847 return new WikiFormat(fStyles, fParams.toList());
144    }
145   
146    /**
147    * Returns opening or closing tags corresponding to the given format(it
148    * depends on the given flag).
149    *
150    * @param open if this flag is <code>true</code> then this method returns
151    * opening tags for this format
152    * @return opening or closing tags corresponding to the given format(it
153    * depends on the given flag)
154    */
 
155  4384 toggle public String getTags(boolean open)
156    {
157  4384 if (fOpeningTags == null) {
158  317 StringBuffer o = new StringBuffer();
159  317 StringBuffer c = new StringBuffer();
160  317 for (WikiStyle style : fStyles) {
161  223 o.append("<").append(style).append(">");
162  223 c.insert(0, ">").insert(0, style).insert(0, "</");
163    }
164  317 fOpeningTags = o.toString().intern();
165  317 fClosingTags = c.toString().intern();
166    }
167  4384 return open ? fOpeningTags : fClosingTags;
168    }
169   
170    /**
171    * @see java.lang.Object#hashCode()
172    */
 
173  0 toggle @Override
174    public int hashCode()
175    {
176    // Random number. See
177    // http://www.geocities.com/technofundo/tech/java/equalhash.html
178    // for the detail of this algorithm.
179  0 int hash = 8;
180  0 hash = 31 * hash + (null == fStyles ? 0 : fStyles.hashCode());
181  0 hash = 31 * hash + (null == fParams ? 0 : fParams.hashCode());
182  0 return hash;
183    }
184   
185    /**
186    * @param style the style to check
187    * @return <code>true</code> if this format has the specified style
188    */
 
189  25 toggle public boolean hasStyle(WikiStyle style)
190    {
191  25 return fStyles.contains(style);
192    }
193   
194    /**
195    * Creates a new style set which does not contain the specified style.
196    *
197    * @param style the style to add
198    * @return a new copy of the style set containing the given style
199    */
 
200  98 toggle public WikiFormat removeStyle(WikiStyle style)
201    {
202  98 if (!fStyles.contains(style)) {
203  2 return this;
204    }
205  96 WikiFormat clone = getClone();
206  96 clone.fStyles.remove(style);
207  96 return clone;
208    }
209   
210    /**
211    * Creates a new format object where the specified style is switched: if
212    * this format contains the given style then the resulting format does not
213    * and vice versa.
214    *
215    * @param wikiStyle the style to switch
216    * @return a format object where the given style is inverted relatively to
217    * this format
218    */
 
219  664 toggle public WikiFormat switchStyle(WikiStyle wikiStyle)
220    {
221  664 WikiFormat clone = getClone();
222  664 if (clone.fStyles.contains(wikiStyle)) {
223  296 clone.fStyles.remove(wikiStyle);
224    } else {
225  368 clone.fStyles.add(wikiStyle);
226    }
227  664 return clone;
228    }
229   
230    /**
231    * @return the list of styles in the order in which they were created
232    */
 
233  4994 toggle public List<WikiStyle> getStyles()
234    {
235  4994 return new ArrayList<WikiStyle>(fStyles);
236    }
237   
 
238  9182 toggle public List<WikiParameter> getParams()
239    {
240  9182 return fParams.toList();
241    }
242   
243    /**
244    * @see java.lang.Object#toString()
245    */
 
246  6 toggle @Override
247    public String toString()
248    {
249  6 return fStyles.toString();
250    }
251    }