package com.indeed.proctor.store.utils;

import com.indeed.proctor.store.Revision;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

public class HistoryUtil {
    public static <T> List<T> selectHistorySet(
            @Nullable final List<T> histories,
            final int start,
            final int limit
    ) {
        if ((histories == null) || (start >= histories.size()) || (limit < 1)) {
            return Collections.emptyList();
        }
        final int s = Math.max(start, 0);
        final int l = Math.min(limit, histories.size() - s); /* to avoid overflow */
        return histories.subList(s, s + l);
    }

    public static List<Revision> selectRevisionHistorySetFrom(
            @Nullable final List<Revision> histories,
            final String from,
            final int start,
            final int limit
    ) {
        if (histories == null) {
            return Collections.emptyList();
        }

        int i = 0;
        for (final Revision rev : histories) {
            if (rev.getRevision().equals(from)) {
                break;
            }
            i++;
        }
        return selectHistorySet(histories, start + i, limit);
    }
}

@@ -274,35 +274,33 @@
 l List%3CT%3E histor
-ies
+y
 ,%0A            fi
@@ -365,27 +365,25 @@
  if ((histor
-ies
+y
  == null) %7C%7C
@@ -395,27 +395,25 @@
 rt %3E= histor
-ies
+y
 .size()) %7C%7C 
@@ -567,19 +567,17 @@
 , histor
-ies
+y
 .size() 
@@ -627,19 +627,17 @@
 n histor
-ies
+y
 .subList
@@ -767,19 +767,17 @@
 %3E histor
-ies
+y
 ,%0A      
@@ -884,19 +884,17 @@
  (histor
-ies
+y
  == null
@@ -1011,19 +1011,17 @@
 : histor
-ies
+y
 ) %7B%0A    
@@ -1172,11 +1172,9 @@
 stor
-ies
+y
 , st

Fix parameter name