Skip to content

Commit ee264f0

Browse files
committed
Converted Numeric Ruby setting class to Java
1 parent dcae6f5 commit ee264f0

File tree

4 files changed

+134
-20
lines changed

4 files changed

+134
-20
lines changed

logstash-core/lib/logstash/settings.rb

+20-19
Original file line numberDiff line numberDiff line change
@@ -413,26 +413,27 @@ def coerce(value)
413413
### Specific settings #####
414414

415415
java_import org.logstash.settings.Boolean
416+
java_import org.logstash.settings.Numeric
416417

417-
class Numeric < Coercible
418-
def initialize(name, default = nil, strict = true)
419-
super(name, ::Numeric, default, strict)
420-
end
421-
422-
def coerce(v)
423-
return v if v.is_a?(::Numeric)
424-
425-
# I hate these "exceptions as control flow" idioms
426-
# but Ruby's `"a".to_i => 0` makes it hard to do anything else.
427-
coerced_value = (Integer(v) rescue nil) || (Float(v) rescue nil)
428-
429-
if coerced_value.nil?
430-
raise ArgumentError.new("Failed to coerce value to Numeric. Received #{v} (#{v.class})")
431-
else
432-
coerced_value
433-
end
434-
end
435-
end
418+
# class Numeric < Coercible
419+
# def initialize(name, default = nil, strict = true)
420+
# super(name, ::Numeric, default, strict)
421+
# end
422+
#
423+
# def coerce(v)
424+
# return v if v.is_a?(::Numeric)
425+
#
426+
# # I hate these "exceptions as control flow" idioms
427+
# # but Ruby's `"a".to_i => 0` makes it hard to do anything else.
428+
# coerced_value = (Integer(v) rescue nil) || (Float(v) rescue nil)
429+
#
430+
# if coerced_value.nil?
431+
# raise ArgumentError.new("Failed to coerce value to Numeric. Received #{v} (#{v.class})")
432+
# else
433+
# coerced_value
434+
# end
435+
# end
436+
# end
436437

437438
class Integer < Coercible
438439
def initialize(name, default = nil, strict = true)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
require "spec_helper"
1919
require "logstash/settings"
2020

21+
# Mirrored in java class org.logstash.settings.Numeric
2122
describe LogStash::Setting::Numeric do
2223
subject { described_class.new("a number", nil, false) }
2324
describe "#set" do
2425
context "when giving a string which doesn't represent a string" do
2526
it "should raise an exception" do
26-
expect { subject.set("not-a-number") }.to raise_error(ArgumentError)
27+
expect { subject.set("not-a-number") }.to raise_error(java.lang.IllegalArgumentException)
2728
end
2829
end
2930
context "when giving a string which represents a " do
3031
context "float" do
3132
it "should coerce that string to the number" do
3233
subject.set("1.1")
3334
expect(subject.value).to eq(1.1)
35+
expect(subject.value).to be_within(0.01).of(1.1)
3436
end
3537
end
3638
context "int" do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.logstash.settings;
20+
21+
public class Numeric extends Coercible<Number> {
22+
23+
public Numeric(String name, Number defaultValue) {
24+
super(name, defaultValue, true, noValidator());
25+
}
26+
27+
// constructor used only in tests
28+
public Numeric(String name, Number defaultValue, boolean strict) {
29+
super(name, defaultValue, strict, noValidator());
30+
}
31+
32+
@Override
33+
public Number coerce(Object obj) {
34+
if (obj instanceof Number) {
35+
return (Number) obj;
36+
}
37+
try {
38+
return Integer.parseInt(obj.toString());
39+
} catch (NumberFormatException e) {
40+
// ugly flow control
41+
}
42+
try {
43+
return Float.parseFloat(obj.toString());
44+
} catch (NumberFormatException e) {
45+
// ugly flow control
46+
}
47+
48+
// no integer neither float parsing succeed, invalid coercion
49+
throw new IllegalArgumentException(coercionFailureMessage(obj));
50+
}
51+
52+
private String coercionFailureMessage(Object obj) {
53+
return String.format("Failed to coerce value to Numeric. Received %s (%s)", obj, obj.getClass());
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.logstash.settings;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.*;
25+
26+
// Mirrored from spec/logstash/settings/numeric_spec.rb
27+
public class NumericTest {
28+
29+
private Numeric sut;
30+
31+
@Before
32+
public void setUp() {
33+
sut = new Numeric("a number", null, false);
34+
}
35+
36+
@Test(expected = IllegalArgumentException.class)
37+
public void givenValueThatIsNotStringWhenSetIsInvokedThrowsException() {
38+
sut.set("not-a-number");
39+
}
40+
41+
@Test
42+
public void givenValueStringThatRepresentFloatWhenSetIsInvokedShouldCoerceThatStringToTheNumber() {
43+
sut.set("1.1");
44+
45+
float value = (Float) sut.value();
46+
assertEquals(1.1f, value, 0.001);
47+
}
48+
49+
@Test
50+
public void givenValueStringThatRepresentIntegerWhenSetIsInvokedShouldCoerceThatStringToTheNumber() {
51+
sut.set("1");
52+
53+
int value = (Integer) sut.value();
54+
assertEquals(1, value);
55+
}
56+
}

0 commit comments

Comments
 (0)