-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Convert Ruby Integer and PositiveInteger settings classes to Java #17460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a59c20
Moved Integer Ruby class to Java
andsel 7edc640
Moved PositiveInteger Ruby class to Java SettingPositiveInteger
andsel 8e0aec1
Code clean, removed debug logs
andsel 8892031
Code clean, removed commented code
andsel 1beed7b
Minor, reinserted dropped line
andsel 13aa4b7
Test, covered all coerce execution paths
andsel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
logstash-core/src/main/java/org/logstash/settings/SettingInteger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.logstash.settings; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class SettingInteger extends Coercible<Integer> { | ||
|
||
public SettingInteger(String name, Integer defaultValue) { | ||
super(name, defaultValue, true, noValidator()); | ||
} | ||
|
||
// constructor used only in tests, but needs to be public to be used in Ruby spec | ||
public SettingInteger(String name, Integer defaultValue, boolean strict) { | ||
super(name, defaultValue, strict, noValidator()); | ||
} | ||
|
||
// Exposed to be redefined in subclasses | ||
protected SettingInteger(String name, Integer defaultValue, boolean strict, Predicate<Integer> validator) { | ||
super(name, defaultValue, strict, validator); | ||
} | ||
|
||
@Override | ||
public Integer coerce(Object obj) { | ||
if (!(obj instanceof String)) { | ||
// it's an Integer and cast | ||
if (obj instanceof Integer) { | ||
return (Integer) obj; | ||
} | ||
// JRuby bridge convert ints to Long | ||
if (obj instanceof Long) { | ||
return ((Long) obj).intValue(); | ||
} | ||
} else { | ||
// try to parse string to int | ||
try { | ||
return Integer.parseInt(obj.toString()); | ||
} catch (NumberFormatException e) { | ||
// ugly flow control | ||
} | ||
} | ||
|
||
// invalid coercion | ||
throw new IllegalArgumentException(coercionFailureMessage(obj)); | ||
} | ||
|
||
private String coercionFailureMessage(Object obj) { | ||
return String.format("Failed to coerce value to SettingInteger. Received %s (%s)", obj, obj.getClass()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
logstash-core/src/main/java/org/logstash/settings/SettingPositiveInteger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.logstash.settings; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class SettingPositiveInteger extends SettingInteger { | ||
|
||
public SettingPositiveInteger(String name, Integer defaultValue) { | ||
super(name, defaultValue, true, new Predicate<Integer>() { | ||
@Override | ||
public boolean test(Integer v) { | ||
if (v <= 0) { | ||
throw new IllegalArgumentException("Number must be bigger than 0. Received: " + v); | ||
} | ||
return true; | ||
} | ||
}); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
logstash-core/src/test/java/org/logstash/settings/SettingIntegerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.logstash.settings; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class SettingIntegerTest { | ||
|
||
private SettingInteger sut; | ||
|
||
@Before | ||
public void setUp() { | ||
sut = new SettingInteger("a number", null, false); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void givenNumberWhichIsNotIntegerWhenSetIsInvokedThrowsException() { | ||
sut.set(1.1); | ||
} | ||
|
||
@Test | ||
public void givenNumberWhichIsIntegerWhenSetIsInvokedThenShouldSetTheNumber() { | ||
sut.set(100); | ||
|
||
Assert.assertEquals(Integer.valueOf(100), sut.value()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.