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

File WikiFormat.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

22
51
19
1
239
137
30
0.59
2.68
19
1.58

Classes

Class Line # Actions
WikiFormat 35 51 0% 30 18
0.804347880.4%
 

Contributing tests

This file is covered by 1358 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.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: 3dc50c0cd99fcafb8de85425c3faef9b4f163c3a $
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  87 toggle public WikiFormat()
48    {
49    }
50   
 
51  4 toggle public WikiFormat(Set<WikiStyle> styles)
52    {
53  4 this(styles, Collections.<WikiParameter>emptyList());
54    }
55   
 
56  13661 toggle public WikiFormat(Set<WikiStyle> styles, Collection<WikiParameter> params)
57    {
58  13661 if (!styles.isEmpty()) {
59  2707 fStyles.addAll(styles);
60    }
61  13661 if (!params.isEmpty()) {
62  9159 fParams = new WikiParameters(params);
63    }
64    }
65   
 
66  4454 toggle public WikiFormat(Collection<WikiParameter> params)
67    {
68  4454 this(Collections.<WikiStyle>emptySet(), params);
69    }
70   
 
71  4 toggle public WikiFormat(WikiStyle style)
72    {
73  4 this(Collections.<WikiStyle>singleton(style));
74    }
75   
 
76  0 toggle public WikiFormat(WikiStyle style, Collection<WikiParameter> params)
77    {
78  0 this(Collections.<WikiStyle>singleton(style), params);
79    }
80   
 
81  0 toggle public WikiFormat(WikiStyle[] styles)
82    {
83  0 super();
84  0 for (WikiStyle style : styles) {
85  0 fStyles.add(style);
86    }
87    }
88   
 
89  5074 toggle public WikiFormat setParameters(Collection<WikiParameter> params)
90    {
91  5074 return new WikiFormat(fStyles, params);
92    }
93   
94    /**
95    * Creates a new style set and adds the given style to it.
96    *
97    * @param style the style to add
98    * @return a new copy of the style set containing the given style
99    */
 
100  139 toggle public WikiFormat addStyle(WikiStyle style)
101    {
102  139 if (fStyles.contains(style)) {
103  3 return this;
104    }
105  136 WikiFormat clone = getClone();
106  136 clone.fStyles.add(style);
107  136 return clone;
108    }
109   
110    /**
111    * @see java.lang.Object#equals(java.lang.Object)
112    */
 
113  560618 toggle @Override
114    public boolean equals(Object obj)
115    {
116  560618 if (obj == this) {
117  539048 return true;
118    }
119  21569 if (!(obj instanceof WikiFormat)) {
120  21570 return false;
121    }
122  0 WikiFormat set = (WikiFormat) obj;
123  0 return fStyles.equals(set.fStyles) && fParams.equals(set.fParams);
124    }
125   
126    /**
127    * @return a new clone of this format object
128    */
 
129  2880 toggle protected WikiFormat getClone()
130    {
131  2880 return new WikiFormat(fStyles, fParams.toList());
132    }
133   
134    /**
135    * Returns opening or closing tags corresponding to the given format(it
136    * depends on the given flag).
137    *
138    * @param open if this flag is <code>true</code> then this method returns
139    * opening tags for this format
140    * @return opening or closing tags corresponding to the given format(it
141    * depends on the given flag)
142    */
 
143  4018 toggle public String getTags(boolean open)
144    {
145  4018 if (fOpeningTags == null) {
146  285 StringBuffer o = new StringBuffer();
147  285 StringBuffer c = new StringBuffer();
148  285 for (WikiStyle style : fStyles) {
149  201 o.append("<").append(style).append(">");
150  201 c.insert(0, ">").insert(0, style).insert(0, "</");
151    }
152  285 fOpeningTags = o.toString().intern();
153  285 fClosingTags = c.toString().intern();
154    }
155  4018 return open ? fOpeningTags : fClosingTags;
156    }
157   
158    /**
159    * @see java.lang.Object#hashCode()
160    */
 
161  0 toggle @Override
162    public int hashCode()
163    {
164    // Random number. See
165    // http://www.geocities.com/technofundo/tech/java/equalhash.html
166    // for the detail of this algorithm.
167  0 int hash = 8;
168  0 hash = 31 * hash + (null == fStyles ? 0 : fStyles.hashCode());
169  0 hash = 31 * hash + (null == fParams ? 0 : fParams.hashCode());
170  0 return hash;
171    }
172   
173    /**
174    * @param style the style to check
175    * @return <code>true</code> if this format has the specified style
176    */
 
177  27 toggle public boolean hasStyle(WikiStyle style)
178    {
179  27 return fStyles.contains(style);
180    }
181   
182    /**
183    * Creates a new style set which does not contain the specified style.
184    *
185    * @param style the style to add
186    * @return a new copy of the style set containing the given style
187    */
 
188  136 toggle public WikiFormat removeStyle(WikiStyle style)
189    {
190  136 if (!fStyles.contains(style)) {
191  2 return this;
192    }
193  134 WikiFormat clone = getClone();
194  134 clone.fStyles.remove(style);
195  134 return clone;
196    }
197   
198    /**
199    * Creates a new format object where the specified style is switched: if
200    * this format contains the given style then the resulting format does not
201    * and vice versa.
202    *
203    * @param wikiStyle the style to switch
204    * @return a format object where the given style is inverted relatively to
205    * this format
206    */
 
207  2610 toggle public WikiFormat switchStyle(WikiStyle wikiStyle)
208    {
209  2610 WikiFormat clone = getClone();
210  2610 if (clone.fStyles.contains(wikiStyle)) {
211  1275 clone.fStyles.remove(wikiStyle);
212    } else {
213  1335 clone.fStyles.add(wikiStyle);
214    }
215  2610 return clone;
216    }
217   
218    /**
219    * @return the list of styles in the order in which they were created
220    */
 
221  50362 toggle public List<WikiStyle> getStyles()
222    {
223  50362 return new ArrayList<WikiStyle>(fStyles);
224    }
225   
 
226  53146 toggle public List<WikiParameter> getParams()
227    {
228  53146 return fParams.toList();
229    }
230   
231    /**
232    * @see java.lang.Object#toString()
233    */
 
234  6 toggle @Override
235    public String toString()
236    {
237  6 return fStyles.toString();
238    }
239    }