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

File DefaultPatch.java

 

Coverage histogram

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

Code metrics

4
28
5
1
118
62
10
0.36
5.6
5
2

Classes

Class Line # Actions
DefaultPatch 37 28 0% 10 20
0.4594594545.9%
 

Contributing tests

This file is covered by 59 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.LinkedList;
23    import java.util.List;
24    import java.util.ListIterator;
25   
26    import org.xwiki.diff.Delta;
27    import org.xwiki.diff.DiffException;
28    import org.xwiki.diff.Patch;
29    import org.xwiki.diff.PatchException;
30   
31    /**
32    * Default implementation of {@link Patch}.
33    *
34    * @param <E> the type of compared elements
35    * @version $Id: e5349b0b42b6475a7e3cad1efe05a1c3047b73b8 $
36    */
 
37    public class DefaultPatch<E> extends LinkedList<Delta<E>> implements Patch<E>
38    {
39    /**
40    * Provides an id for serialization.
41    */
42    private static final long serialVersionUID = 1L;
43   
44    /**
45    * Default constructor.
46    */
 
47  149 toggle public DefaultPatch()
48    {
49    }
50   
51    /**
52    * Convert {@link difflib.Patch} into {@link Patch}.
53    *
54    * @param patch the {@link difflib.Patch} to convert
55    * @throws DiffException error when converting the patch
56    */
 
57  161 toggle public DefaultPatch(difflib.Patch<E> patch) throws DiffException
58    {
59  161 for (difflib.Delta<E> delta : patch.getDeltas()) {
60  144 add(toDelta(delta));
61    }
62    }
63   
64    /**
65    * Convert {@link difflib.Delta} into {@link Delta}.
66    *
67    * @param delta the {@link difflib.Delta} to convert
68    * @return the {@link Delta}
69    * @throws DiffException error when converting the delta
70    */
 
71  144 toggle private Delta<E> toDelta(difflib.Delta<E> delta) throws DiffException
72    {
73  144 Delta<E> newDelta;
74   
75  144 switch (delta.getType()) {
76  78 case CHANGE:
77  78 newDelta = new ChangeDelta<E>(delta.getOriginal(), delta.getRevised());
78  78 break;
79  13 case DELETE:
80  13 newDelta = new DeleteDelta<E>(delta.getOriginal(), delta.getRevised());
81  13 break;
82  53 case INSERT:
83  53 newDelta = new InsertDelta<E>(delta.getOriginal(), delta.getRevised());
84  53 break;
85  0 default:
86  0 throw new DiffException(String.format("Failed to convert [%s] info [%s]. Unknown type [%s]", delta
87    .getClass().getName(), Delta.class.getName(), delta.getType().toString()));
88    }
89   
90  144 return newDelta;
91    }
92   
 
93  0 toggle @Override
94    public List<E> apply(List<E> target) throws PatchException
95    {
96  0 List<E> result = new LinkedList<E>(target);
97  0 ListIterator<Delta<E>> it = listIterator(size());
98  0 while (it.hasPrevious()) {
99  0 Delta<E> delta = it.previous();
100  0 delta.apply(result);
101    }
102   
103  0 return result;
104    }
105   
 
106  0 toggle @Override
107    public List<E> restore(List<E> target) throws PatchException
108    {
109  0 List<E> result = new LinkedList<E>(target);
110  0 ListIterator<Delta<E>> it = listIterator(size());
111  0 while (it.hasPrevious()) {
112  0 Delta<E> delta = it.previous();
113  0 delta.restore(result);
114    }
115   
116  0 return result;
117    }
118    }