All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.collections.diff.DefaultDifference Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections.diff;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.javagimmicks.collections.decorators.AbstractUnmodifiableListDecorator;

/**
 * A default implementation of {@link Difference}.
 */
public class DefaultDifference implements Difference
{
   protected int _deleteStartIndex;
   protected int _deleteEndIndex;
   protected List _fromList;

   protected int _addStartIndex;
   protected int _addEndIndex;
   protected List _toList;

   /**
    * Creates a new instance with all necessary information.
    * 
    * @param deleteStartIndex
    *           the first index of the delete {@link Difference.Range}
    * @param deleteEndIndex
    *           the last index of the delete {@link Difference.Range}
    * @param addStartIndex
    *           the first index of the add {@link Difference.Range}
    * @param addEndIndex
    *           the last index of the add {@link Difference.Range}
    * @param fromList
    *           the from {@link List} ("left side") of the comparison
    * @param toList
    *           the to {@link List} ("right side") of the comparison
    */
   public DefaultDifference(
         final int deleteStartIndex, final int deleteEndIndex,
         final int addStartIndex, final int addEndIndex,
         final List fromList, final List toList)
   {
      _deleteStartIndex = deleteStartIndex;
      _deleteEndIndex = deleteEndIndex;

      _addStartIndex = addStartIndex;
      _addEndIndex = addEndIndex;

      _fromList = fromList;
      _toList = toList;
   }

   DefaultDifference()
   {
      this(0, NONE, 0, NONE, new ArrayList(), new ArrayList());
   }

   @Override
   public Range deleteRange()
   {
      return new DeleteRange();
   }

   @Override
   public Range addRange()
   {
      return new AddRange();
   }

   @Override
   public Difference invert()
   {
      return DifferenceUtils.getInvertedDifference(this);
   }

   @Override
   public String toString()
   {
      return DifferenceUtils.toString(this);
   }

   void setDeleteStartIndex(final int startIndex)
   {
      _deleteStartIndex = startIndex;
   }

   void setDeleteEndIndex(final int endIndex)
   {
      _deleteEndIndex = endIndex;
   }

   void setAddStartIndex(final int startIndex)
   {
      _addStartIndex = startIndex;
   }

   void setAddEndIndex(final int endIndex)
   {
      _addEndIndex = endIndex;
   }

   void setFromList(final List list)
   {
      _fromList = list;
   }

   void setToList(final List list)
   {
      _toList = list;
   }

   private class DeleteRange extends AbstractUnmodifiableListDecorator implements Range
   {
      private static final long serialVersionUID = 5152362545955680166L;

      public DeleteRange()
      {
         super(_deleteEndIndex != NONE ? _fromList.subList(_deleteStartIndex, _deleteEndIndex + 1) : Collections
               . emptyList());
      }

      @Override
      public int getStartIndex()
      {
         return _deleteStartIndex;
      }

      @Override
      public int getEndIndex()
      {
         return _deleteEndIndex;
      }

      @Override
      public boolean exists()
      {
         return _deleteEndIndex != NONE;
      }
   }

   private class AddRange extends AbstractUnmodifiableListDecorator implements Range
   {
      private static final long serialVersionUID = 196165493918974027L;

      public AddRange()
      {
         super(_addEndIndex != NONE ? _toList.subList(_addStartIndex, _addEndIndex + 1) : Collections. emptyList());
      }

      @Override
      public int getStartIndex()
      {
         return _addStartIndex;
      }

      @Override
      public int getEndIndex()
      {
         return _addEndIndex;
      }

      @Override
      public boolean exists()
      {
         return _addEndIndex != NONE;
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy