Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * See the NOTICE file distributed with this work for additional
   * information regarding copyright ownership.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.xwiki.blame.internal;
 
 import java.util.List;
 
 
 import difflib.Chunk;
 import difflib.Delta;
Hold content during blame analysis and provides actual results.

Parameters:
<R> type of the revision object that old metadata about the revision.
<E> type of the element to annotate (ie: String holding a line).
Version:
$Id: e6931ce39e43277786cd099eec9fee76ffdb9697 $
Since:
6.2M2
 
 public class DefaultAnnotatedContent<R, E> implements AnnotatedContent<R, E>
 {
     private final List<R> sourceRevisions;
     private final List<E> initialContent;
     private final List<E> currentRevisionContent;
     private R currentRevision;
     private final List<IntegerelementMap;
     private final int size;
 
     DefaultAnnotatedContent(R revisionList<E> initialContent)
     {
         this. = initialContent.size();
         this. = new ArrayList<R>(this.);
         this. = initialContent;
         this. = new ArrayList<E>(initialContent);
         this. = revision;
         this. = new ArrayList<>(this.);
         for (int i = 0; i < i++) {
             this..add(null);
             this..add(i);
         }
     }
 
     private class AnnotatedContentIterator implements Iterator<AnnotatedElement<R, E>>
     {
         private int index = -1;
 
         @Override
         public boolean hasNext()
         {
             return  < ( - 1);
         }
 
         @Override
         public AnnotatedElement<R, E> next()
         {
              += 1;
             return new DefaultAnnotatedElement<R, E>(.get(), .get());
         }
 
         @Override
         public void remove()
         {
             throw new UnsupportedOperationException();
         }
     }
 
     @Override
     public Iterator<AnnotatedElement<R, E>> iterator()
     {
         return new AnnotatedContentIterator();
     }
 
     @Override
     public R getOldestRevision()
     {
         return ;
     }
    @Override
    public boolean isEntirelyAnnotated()
    {
        for (int i = 0; i < this.i++) {
            if (this..get(i) == null) {
                return false;
            }
        }
        return true;
    }

    
Resolve revision of line to current revision based on given previous content, and prepare for next analysis.

Parameters:
revision the revision of the content provided.
previous the content in a previous revision.
    void analyseRevision(R revisionList<E> previous)
    {
        if ( == null) {
            return;
        }
        if (previous == null || previous.isEmpty()) {
            resolveRemainingToCurrent();
        } else {
            resolveToCurrent(DiffUtils.diff(previous).getDeltas());
            assert .equals(previous) : "Patch application failed";
        }
         = revision;
    }

    
Resolve revision of line to current revision based on given previous content. Thanks to Michael Schierl <schierlm%40gmx.de> for sharing this code on StackOverflow.

Parameters:
deltas the delta to apply to current content to move to previous revision.
    private void resolveToCurrent(List<Delta<E>> deltas)
    {
        int lineOffset = 0;
        for (Delta<E> d : deltas) {
            Chunk<E> original = d.getOriginal();
            Chunk<E> revised = d.getRevised();
            int pos = original.getPosition() + lineOffset;
            // delete lines
            for (int i = 0; i < original.size(); i++) {
                int origLine = .remove(pos);
                .remove(pos);
                if (origLine != -1) {
                    .set(origLine);
                }
            }
            for (int i = 0; i < revised.size(); i++) {
                .add(pos + irevised.getLines().get(i));
                .add(pos + i, -1);
            }
            lineOffset += revised.size() - original.size();
        }
    }

    
Resolve all line remaining without revision to current.
    private void resolveRemainingToCurrent()
    {
        for (int i = 0; i < this.i++) {
            if (.get(i) == null) {
                .set(i);
            }
        }
    }
New to GrepCode? Check out our FAQ X