Skip to content

Commit 4611e44

Browse files
committed
Add support for the fill().
1 parent ba27aff commit 4611e44

File tree

2 files changed

+67
-2
lines changed
  • lodash-plugin/src

2 files changed

+67
-2
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public Chain<List<T>> dropWhile(final Predicate<T> pred) {
7171
public Chain<List<T>> dropRightWhile(final Predicate<T> pred) {
7272
return new Chain<List<T>>($.dropRightWhile(value(), pred));
7373
}
74+
75+
public Chain<List<Object>> fill(Object value) {
76+
return new Chain<List<Object>>($.fill((List<Object>) value(), value));
77+
}
78+
79+
public Chain<List<Object>> fill(Object value, Integer start, Integer end) {
80+
return new Chain<List<Object>>($.fill((List<Object>) value(), value, start, end));
81+
}
7482
}
7583

7684
public static Chain chain(final String item) {
@@ -145,14 +153,35 @@ public List<T> dropWhile(final Predicate<T> pred) {
145153
}
146154

147155
public static <T> List<T> dropRightWhile(final Iterable<T> iterable, final Predicate<T> pred) {
148-
final List<T> list = reverse(iterable);
149-
return rest(list, findIndex(list, negate(pred)));
156+
return dropWhile(reverse(iterable), pred);
150157
}
151158

152159
public List<T> dropRightWhile(final Predicate<T> pred) {
153160
return dropRightWhile(getIterable(), pred);
154161
}
155162

163+
public static List<Object> fill(final List<Object> list, Object value) {
164+
for (int index = 0; index < list.size(); index += 1) {
165+
list.set(index, value);
166+
}
167+
return list;
168+
}
169+
170+
public List<Object> fill(Object value) {
171+
return fill((List<Object>) getIterable(), value);
172+
}
173+
174+
public static List<Object> fill(final List<Object> list, Object value, Integer start, Integer end) {
175+
for (int index = start; index < end; index += 1) {
176+
list.set(index, value);
177+
}
178+
return list;
179+
}
180+
181+
public List<Object> fill(Object value, Integer start, Integer end) {
182+
return fill((List<Object>) getIterable(), value, start, end);
183+
}
184+
156185
public static void main(String ... args) {
157186
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
158187
+ "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java";

lodash-plugin/src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,42 @@ public Boolean apply(Integer n) {
151151
}).toString());
152152
}
153153

154+
/*
155+
var array = [1, 2, 3];
156+
157+
_.fill(array, 'a');
158+
console.log(array);
159+
// → ['a', 'a', 'a']
160+
161+
_.fill(Array(3), 2);
162+
// → [2, 2, 2]
163+
164+
_.fill([4, 6, 8], '*', 1, 2);
165+
// → [4, '*', 8]
166+
*/
167+
@Test
168+
public void fill() {
169+
List<Object> array = new ArrayList<Object>(asList(1, 2, 3));
170+
$.fill(array, "a");
171+
assertEquals("[a, a, a]", array.toString());
172+
array = new ArrayList<Object>(asList(1, 2, 3));
173+
new $(array).fill("a");
174+
assertEquals("[a, a, a]", array.toString());
175+
array = new ArrayList<Object>(asList(1, 2, 3));
176+
$.chain(array).fill("a");
177+
assertEquals("[a, a, a]", array.toString());
178+
assertEquals("[2, 2, 2]", $.fill(new ArrayList<Object>(Collections.nCopies(3, 0)), 2).toString());
179+
array = new ArrayList<Object>(asList(4, 6, 8));
180+
$.fill(array, "*", 1, 2);
181+
assertEquals("[4, *, 8]", array.toString());
182+
array = new ArrayList<Object>(asList(4, 6, 8));
183+
new $(array).fill("*", 1, 2);
184+
assertEquals("[4, *, 8]", array.toString());
185+
array = new ArrayList<Object>(asList(4, 6, 8));
186+
$.chain(array).fill("*", 1, 2);
187+
assertEquals("[4, *, 8]", array.toString());
188+
}
189+
154190
@Test
155191
public void main() {
156192
$.main(new String[] {});

0 commit comments

Comments
 (0)