1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.diff.internal

File DefaultChunk.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
76% of files have more coverage

Code metrics

10
24
11
1
140
82
16
0.67
2.18
11
1.45

Classes

Class Line # Actions
DefaultChunk 35 24 0% 16 23
0.488888948.9%
 

Contributing tests

This file is covered by 36 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.diff.internal;
21   
22    import java.util.List;
23    import java.util.Objects;
24   
25    import org.apache.commons.lang3.builder.HashCodeBuilder;
26    import org.xwiki.diff.Chunk;
27    import org.xwiki.diff.PatchException;
28   
29    /**
30    * Default implementation of {@link Chunk}.
31    *
32    * @param <E> the type of compared elements
33    * @version $Id: bd471b28caa894be283846f22191793f5f7a2292 $
34    */
 
35    public class DefaultChunk<E> implements Chunk<E>
36    {
37    /**
38    * @see #getIndex()
39    */
40    private final int index;
41   
42    /**
43    * @see #getElements()
44    */
45    private List<E> elements;
46   
47    /**
48    * @param index the index where to find the provided snippet of elements in the source list
49    * @param elements the snippet of elements
50    */
 
51  302 toggle public DefaultChunk(int index, List<E> elements)
52    {
53  302 this.index = index;
54  302 this.elements = elements;
55    }
56   
57    /**
58    * @param chunk the chunk to convert
59    */
 
60  286 toggle public DefaultChunk(difflib.Chunk<E> chunk)
61    {
62  286 this(chunk.getPosition(), chunk.getLines());
63    }
64   
 
65  0 toggle @Override
66    public void verify(List<E> target) throws PatchException
67    {
68  0 if (getLastIndex() > target.size()) {
69  0 throw new PatchException("Incorrect Chunk: the position of chunk > target size");
70    }
71   
72  0 for (int i = 0; i < size(); i++) {
73  0 if (!target.get(this.index + i).equals(this.elements.get(i))) {
74  0 throw new PatchException("Incorrect Chunk: the chunk content doesn't match the target");
75    }
76    }
77    }
78   
 
79  418 toggle @Override
80    public int getIndex()
81    {
82  418 return this.index;
83    }
84   
 
85  0 toggle @Override
86    public void setElements(List<E> lines)
87    {
88  0 this.elements = lines;
89    }
90   
 
91  175 toggle @Override
92    public List<E> getElements()
93    {
94  175 return this.elements;
95    }
96   
 
97  259 toggle @Override
98    public int size()
99    {
100  259 return this.elements.size();
101    }
102   
 
103  86 toggle @Override
104    public int getLastIndex()
105    {
106  86 return getIndex() + size() - 1;
107    }
108   
 
109  0 toggle @Override
110    public int hashCode()
111    {
112  0 HashCodeBuilder builder = new HashCodeBuilder();
113   
114  0 builder.append(getElements());
115  0 builder.append(getIndex());
116   
117  0 return builder.toHashCode();
118    }
119   
 
120  18 toggle @Override
121    public boolean equals(Object obj)
122    {
123  18 if (this == obj) {
124  0 return true;
125    }
126   
127  18 if (obj instanceof Chunk) {
128  18 Chunk<?> otherChunk = (Chunk<?>) obj;
129  18 return getIndex() == otherChunk.getIndex() && Objects.equals(getElements(), otherChunk.getElements());
130    }
131   
132  0 return false;
133    }
134   
 
135  60 toggle @Override
136    public String toString()
137    {
138  60 return "[position: " + this.index + ", size: " + size() + ", lines: " + this.elements + "]";
139    }
140    }