Skip to content

Commit cc8b1e6

Browse files
Boroshjavadev
authored andcommitted
Improve U.fill(list, item), add U.fill(array, item) methods.
1 parent e343a5b commit cc8b1e6

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,20 @@ public List<T> dropRightWhile(final Predicate<T> pred) {
778778
return dropRightWhile(getIterable(), pred);
779779
}
780780

781-
public static List<Object> fill(final List<Object> list, Object value) {
782-
for (int index = 0; index < list.size(); index += 1) {
783-
list.set(index, value);
781+
public static <T> List<T> fill(List<T> list, T item) {
782+
for (int i = 0; i < size(list); i++) {
783+
list.set(i, item);
784784
}
785785
return list;
786786
}
787787

788+
public static <T> T[] fill(T[] array, T item) {
789+
for (int i = 0; i < array.length; i++) {
790+
array[i] = item;
791+
}
792+
return array;
793+
}
794+
788795
@SuppressWarnings("unchecked")
789796
public List<Object> fill(Object value) {
790797
return fill((List<Object>) getIterable(), value);

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,43 @@ public boolean test(Integer n) {
165165
}
166166

167167
/*
168-
var array = [1, 2, 3];
169-
170-
_.fill(array, 'a');
171-
console.log(array);
172-
// → ['a', 'a', 'a']
173-
174-
_.fill(Array(3), 2);
175-
// → [2, 2, 2]
176-
177-
_.fill([4, 6, 8], '*', 1, 2);
178-
// → [4, '*', 8]
168+
_.fill([1, 2, 3], 4)
169+
// → [4, 4, 4]
170+
_.fill(["test1", "test2", "test3"], "res")
171+
// → ["res", "res", "res"]
179172
*/
180173
@SuppressWarnings("unchecked")
181174
@Test
182175
public void fill() {
183-
List<Object> array = new ArrayList<Object>(asList(1, 2, 3));
184-
U.fill(array, "a");
185-
assertEquals("[a, a, a]", array.toString());
176+
assertEquals("[2, 2, 2]", U.fill(new ArrayList<Number>(Collections.nCopies(3, 0)), 2).toString());
177+
List<Object> array = new ArrayList<Object>(asList(4, 6, 8));
178+
U.fill(array, "*", 1, 2);
179+
assertEquals("[4, *, 8]", array.toString());
186180
array = new ArrayList<Object>(asList(1, 2, 3));
187181
new U(array).fill("a");
188182
assertEquals("[a, a, a]", array.toString());
189183
array = new ArrayList<Object>(asList(1, 2, 3));
190184
U.chain(array).fill("a");
191-
assertEquals("[a, a, a]", array.toString());
192-
assertEquals("[2, 2, 2]", U.fill(new ArrayList<Object>(Collections.nCopies(3, 0)), 2).toString());
193-
array = new ArrayList<Object>(asList(4, 6, 8));
194-
U.fill(array, "*", 1, 2);
195-
assertEquals("[4, *, 8]", array.toString());
196185
array = new ArrayList<Object>(asList(4, 6, 8));
197186
new U(array).fill("*", 1, 2);
198187
assertEquals("[4, *, 8]", array.toString());
199188
array = new ArrayList<Object>(asList(4, 6, 8));
200189
U.chain(array).fill("*", 1, 2);
201190
assertEquals("[4, *, 8]", array.toString());
191+
List<Number> list = new ArrayList<Number>();
192+
list.add(1);
193+
list.add(2);
194+
list.add(3);
195+
final List<Number> result1 = U.fill(list, 4);
196+
assertEquals("[4, 4, 4]", result1.toString());
197+
List<String> list1 = new ArrayList<String>();
198+
list1.add("test1");
199+
list1.add("test1");
200+
list1.add("test1");
201+
final List<String> result2 = U.fill(list1, "res");
202+
assertEquals("[res, res, res]", result2.toString());
203+
final Number[] result3 = U.fill(new Number[] {1, 2, 3}, 4);
204+
assertEquals("[4, 4, 4]", asList(result3).toString());
202205
}
203206

204207
/*

0 commit comments

Comments
 (0)