Skip to content

Commit f36a55e

Browse files
committed
Fixed SettingPortRange.coerce when Long value is provided. Added method eachWithIndex to Range to loop on the interval
1 parent c0286fd commit f36a55e

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

logstash-core/lib/logstash/environment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ module Environment
3333
end
3434
end
3535

36-
def as_java_range(r)
37-
org::logstash::settings::Range.new(r.first, r.last)
36+
def self.as_java_range(r)
37+
org::logstash::settings::Range.new(r.first.to_java(java.lang.Integer), r.last.to_java(java.lang.Integer))
3838
end
3939

4040
[

logstash-core/src/main/java/org/logstash/settings/Range.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.logstash.settings;
22

3-
import org.jruby.RubyRange;
4-
import org.jruby.runtime.ThreadContext;
3+
import java.util.function.BiConsumer;
54

6-
public class Range<T extends Comparable<? super T>> {
5+
public class Range<T extends Integer> {
76

87
private final T first;
98
private final T last;
@@ -25,8 +24,20 @@ public T getLast() {
2524
return last;
2625
}
2726

28-
// public static Range<T> fromRubyRange(RubyRange r) {
29-
// ThreadContext context = r.getRuntime().getCurrentContext();
30-
// r.last(context);
31-
// }
27+
// TODO cover with tests
28+
public void eachWithIndex(BiConsumer<Integer, Integer> consumer) {
29+
// In case of a single value range, we should still yield once
30+
if (first.intValue() == last.intValue()) {
31+
consumer.accept(first.intValue(), 0);
32+
return;
33+
}
34+
int index = 0;
35+
for (int value = first.intValue(); first.intValue() < last.intValue(); value++) {
36+
consumer.accept(value, index++);
37+
}
38+
}
39+
40+
public int count() {
41+
return last.intValue() - first.intValue() + 1;
42+
}
3243
}

logstash-core/src/main/java/org/logstash/settings/SettingPortRange.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static boolean isValid(Range<Integer> range) {
1616
return VALID_PORT_RANGE.contains(range);
1717
}
1818

19+
// TODO cover with tests
1920
@Override
2021
public Range<Integer> coerce(Object obj) {
2122
if (obj instanceof Range) {
@@ -27,6 +28,11 @@ public Range<Integer> coerce(Object obj) {
2728
return new Range<>(val, val);
2829
}
2930

31+
if (obj instanceof Long) {
32+
Long val = (Long) obj;
33+
return new Range<>(val.intValue(), val.intValue());
34+
}
35+
3036
if (obj instanceof String) {
3137
String val = (String) obj;
3238
String[] parts = val.split(PORT_SEPARATOR);
@@ -42,10 +48,10 @@ public Range<Integer> coerce(Object obj) {
4248
int last = Integer.parseInt(lastStr);
4349
return new Range<>(first, last);
4450
} catch(NumberFormatException e) {
45-
throw new IllegalArgumentException("Could not coerce " + obj + " into a port range");
51+
throw new IllegalArgumentException("Could not coerce [" + obj + "](type: " + obj.getClass() + ") into a port range");
4652
}
4753
}
48-
throw new IllegalArgumentException("Could not coerce " + obj + " into a port range");
54+
throw new IllegalArgumentException("Could not coerce [" + obj + "](type: " + obj.getClass() + ") into a port range");
4955
}
5056

5157
@Override

0 commit comments

Comments
 (0)