Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
fixed safe batches for fetchAll and safeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Nov 12, 2019
1 parent 463a524 commit 9029fae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
}
}

version "4.0.4"
version "4.0.5"
group "org.grails.plugins"

apply plugin:"eclipse"
Expand Down Expand Up @@ -52,7 +52,7 @@ dependencies {
provided "org.grails:grails-plugin-domain-class"

// Latest RestFB lib
compile 'com.restfb:restfb:2.25.0'
compile 'com.restfb:restfb:2.26.0'

// Tests
testCompile 'org.grails:grails-web-testing-support:2.0.0'
Expand Down
24 changes: 14 additions & 10 deletions src/main/groovy/grails/plugin/facebooksdk/FacebookExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,26 @@ private static <T> List<List<T>> collate(List<T> selfList, int step) {
return Collections.singletonList(Collections.emptyList());
}

int size = selfList.size();

if (size <= step) {
int total = selfList.size();
if (total <= step) {
return Collections.singletonList(selfList);
}

List<List<T>> answer = new ArrayList<>();
if (step <= 0) throw new IllegalArgumentException("step must be greater than zero");
for (int pos = 0; pos < size && pos > -1; pos += step) {
List<T> element = new ArrayList<>() ;
for (int offs = pos; offs < pos + size && offs < size; offs++) {
List<List<T>> answer = new ArrayList<List<T>>();

if (step == 0) {
throw new IllegalArgumentException("step cannot be zero");
}

for (int pos = 0; pos < total && pos > -1; pos += step) {
List<T> element = new ArrayList<T>();
for (int offs = pos; offs < pos + step && offs < total; offs++) {
element.add(selfList.get(offs));
}
answer.add(element) ;
answer.add(element);
}
return answer ;

return answer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package grails.plugin.facebooksdk

import spock.lang.Specification

class FacebookExtensionsSpec extends Specification {

void 'test collation'() {
when:
List<List<String>> collate40 = FacebookExtensions.collate(['a'] * 2, 10)
then:
collate40.size() == 1
collate40[0].size() == 2

when:
List<List<String>> collate60 = FacebookExtensions.collate(['a'] * 5, 2)
then:
collate60.size() == 3
collate60[0].size() == 2
collate60[1].size() == 2
collate60[2].size() == 1
}

}

0 comments on commit 9029fae

Please sign in to comment.