Skip to content

Commit aabf84b

Browse files
authored
Convert Ruby Integer and PositiveInteger settings classes to Java (#17460)
Convert the Ruby settings classes Integer and PositiveInteger to Java, renaming them to SettingInteger and SettingPositiveInteger.
1 parent 5cad436 commit aabf84b

File tree

7 files changed

+129
-36
lines changed

7 files changed

+129
-36
lines changed

logstash-core/lib/logstash/environment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module Environment
4747
Setting::Boolean.new("metric.collect", true),
4848
Setting::SettingString.new("pipeline.id", "main"),
4949
Setting::Boolean.new("pipeline.system", false),
50-
Setting::PositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum),
51-
Setting::PositiveInteger.new("pipeline.batch.size", 125),
50+
Setting::SettingPositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum),
51+
Setting::SettingPositiveInteger.new("pipeline.batch.size", 125),
5252
Setting::SettingNumeric.new("pipeline.batch.delay", 50), # in milliseconds
5353
Setting::Boolean.new("pipeline.unsafe_shutdown", false),
5454
Setting::Boolean.new("pipeline.reloadable", true),

logstash-core/lib/logstash/settings.rb

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -415,37 +415,11 @@ def coerce(value)
415415
java_import org.logstash.settings.Boolean
416416
java_import org.logstash.settings.SettingNumeric
417417

418-
class Integer < Coercible
419-
def initialize(name, default = nil, strict = true)
420-
super(name, ::Integer, default, strict)
421-
end
422-
423-
def coerce(value)
424-
return value unless value.is_a?(::String)
425-
426-
coerced_value = Integer(value) rescue nil
427-
428-
if coerced_value.nil?
429-
raise ArgumentError.new("Failed to coerce value to Integer. Received #{value} (#{value.class})")
430-
else
431-
coerced_value
432-
end
433-
end
434-
end
418+
java_import org.logstash.settings.SettingInteger
435419

436-
class PositiveInteger < Integer
437-
def initialize(name, default = nil, strict = true)
438-
super(name, default, strict) do |v|
439-
if v > 0
440-
true
441-
else
442-
raise ArgumentError.new("Number must be bigger than 0. Received: #{v}")
443-
end
444-
end
445-
end
446-
end
447-
448-
class Port < Integer
420+
java_import org.logstash.settings.SettingPositiveInteger
421+
422+
class Port < SettingInteger
449423
VALID_PORT_RANGE = 1..65535
450424

451425
def initialize(name, default = nil, strict = true)

logstash-core/spec/logstash/queue_factory_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
LogStash::Setting::SettingNumeric.new("queue.checkpoint.writes", 1024),
3232
LogStash::Setting::Boolean.new("queue.checkpoint.retry", false),
3333
LogStash::Setting::SettingString.new("pipeline.id", pipeline_id),
34-
LogStash::Setting::PositiveInteger.new("pipeline.batch.size", 125),
35-
LogStash::Setting::PositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum)
34+
LogStash::Setting::SettingPositiveInteger.new("pipeline.batch.size", 125),
35+
LogStash::Setting::SettingPositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum)
3636
]
3737
end
3838

logstash-core/spec/logstash/settings/integer_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
require "spec_helper"
1919
require "logstash/settings"
2020

21-
describe LogStash::Setting::Integer do
21+
describe LogStash::Setting::SettingInteger do
2222
subject { described_class.new("a number", nil, false) }
2323
describe "#set" do
2424
context "when giving a number which is not an integer" do
2525
it "should raise an exception" do
26-
expect { subject.set(1.1) }.to raise_error(ArgumentError)
26+
expect { subject.set(1.1) }.to raise_error(java.lang.IllegalArgumentException)
2727
end
2828
end
2929
context "when giving a number which is an integer" do
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.logstash.settings;
2+
3+
import java.util.function.Predicate;
4+
5+
public class SettingInteger extends Coercible<Integer> {
6+
7+
public SettingInteger(String name, Integer defaultValue) {
8+
super(name, defaultValue, true, noValidator());
9+
}
10+
11+
// constructor used only in tests, but needs to be public to be used in Ruby spec
12+
public SettingInteger(String name, Integer defaultValue, boolean strict) {
13+
super(name, defaultValue, strict, noValidator());
14+
}
15+
16+
// Exposed to be redefined in subclasses
17+
protected SettingInteger(String name, Integer defaultValue, boolean strict, Predicate<Integer> validator) {
18+
super(name, defaultValue, strict, validator);
19+
}
20+
21+
@Override
22+
public Integer coerce(Object obj) {
23+
if (!(obj instanceof String)) {
24+
// it's an Integer and cast
25+
if (obj instanceof Integer) {
26+
return (Integer) obj;
27+
}
28+
// JRuby bridge convert ints to Long
29+
if (obj instanceof Long) {
30+
return ((Long) obj).intValue();
31+
}
32+
} else {
33+
// try to parse string to int
34+
try {
35+
return Integer.parseInt(obj.toString());
36+
} catch (NumberFormatException e) {
37+
// ugly flow control
38+
}
39+
}
40+
41+
// invalid coercion
42+
throw new IllegalArgumentException(coercionFailureMessage(obj));
43+
}
44+
45+
private String coercionFailureMessage(Object obj) {
46+
return String.format("Failed to coerce value to SettingInteger. Received %s (%s)", obj, obj.getClass());
47+
}
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.logstash.settings;
2+
3+
import java.util.function.Predicate;
4+
5+
public class SettingPositiveInteger extends SettingInteger {
6+
7+
public SettingPositiveInteger(String name, Integer defaultValue) {
8+
super(name, defaultValue, true, new Predicate<Integer>() {
9+
@Override
10+
public boolean test(Integer v) {
11+
if (v <= 0) {
12+
throw new IllegalArgumentException("Number must be bigger than 0. Received: " + v);
13+
}
14+
return true;
15+
}
16+
});
17+
}
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.logstash.settings;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class SettingIntegerTest {
9+
10+
private SettingInteger sut;
11+
12+
@Before
13+
public void setUp() {
14+
sut = new SettingInteger("a number", null, false);
15+
}
16+
17+
@Test(expected = IllegalArgumentException.class)
18+
public void givenNumberWhichIsNotIntegerWhenSetIsInvokedThrowsException() {
19+
sut.set(1.1);
20+
}
21+
22+
@Test
23+
public void givenNumberWhichIsIntegerWhenSetIsInvokedThenShouldSetTheNumber() {
24+
sut.set(100);
25+
26+
assertEquals(Integer.valueOf(100), sut.value());
27+
}
28+
29+
@Test
30+
public void givenStringWhichIsIntegerThenCoerceCastIntoInteger() {
31+
assertEquals(Integer.valueOf(100), sut.coerce("100"));
32+
}
33+
34+
@Test
35+
public void givenIntegerInstanceThenCoerceCastIntoInteger() {
36+
assertEquals(Integer.valueOf(100), sut.coerce(100));
37+
}
38+
39+
@Test
40+
public void givenLongInstanceThenCoerceCastIntoInteger() {
41+
assertEquals(Integer.valueOf(100), sut.coerce(100L));
42+
}
43+
44+
@Test(expected = IllegalArgumentException.class)
45+
public void givenDoubleInstanceThenCoerceThrowsException() {
46+
sut.coerce(1.1);
47+
}
48+
49+
@Test(expected = IllegalArgumentException.class)
50+
public void givenObjectInstanceThenCoerceThrowsException() {
51+
sut.coerce(new Object());
52+
}
53+
}

0 commit comments

Comments
 (0)