-
Notifications
You must be signed in to change notification settings - Fork 27
Introduce EntryDateResolutionStrategy #83
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
Closed
nieldw
wants to merge
1
commit into
qoomon:master
from
nieldw:feature/entry_date_resolution_strategy
Closed
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions
14
...ubmessage/field/subfield/EarlierMonthImpliesFollowingYearEntryDateResolutionStrategy.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,14 @@ | ||
| package com.qoomon.banking.swift.submessage.field.subfield; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.MonthDay; | ||
|
|
||
| public class EarlierMonthImpliesFollowingYearEntryDateResolutionStrategy implements EntryDateResolutionStrategy { | ||
| @Override | ||
| public LocalDate resolve(MonthDay entryMonthDay, LocalDate valueDate) { | ||
| int entryYear = entryMonthDay.getMonthValue() >= valueDate.getMonthValue() | ||
| ? valueDate.getYear() | ||
| : valueDate.getYear() + 1; | ||
| return entryMonthDay.atYear(entryYear); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
.../java/com/qoomon/banking/swift/submessage/field/subfield/EntryDateResolutionStrategy.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 com.qoomon.banking.swift.submessage.field.subfield; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.MonthDay; | ||
|
|
||
| /** | ||
| * Strategy to determine the correct {@link com.qoomon.banking.swift.submessage.field.StatementLine} entry date. | ||
| * | ||
| * Statement line :61: sub field 2 contains an optional entry date: | ||
| * | ||
| * Notation: [4!n] | ||
| * Format: 'MMDD' | ||
| * | ||
| * Implementers of this strategy must determine the correct year of the entry date based on the the given value date. | ||
| */ | ||
| public interface EntryDateResolutionStrategy { | ||
| LocalDate resolve(MonthDay entryMonthDay, LocalDate valueDate); | ||
| } |
47 changes: 47 additions & 0 deletions
47
...mon/banking/swift/submessage/field/subfield/ShortestDeltaEntryDateResolutionStrategy.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,47 @@ | ||
| package com.qoomon.banking.swift.submessage.field.subfield; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.MonthDay; | ||
|
|
||
| import static java.lang.Math.abs; | ||
| import static java.lang.Math.min; | ||
|
|
||
| /** | ||
| * Resolve entry date based on the shortest delta between the entry date and the value date. | ||
| * <p> | ||
| * This strategy views the {@link MonthDay} timeline as a circle, like the hours on a clock. If the shortest delta | ||
| * around the circle does not span 31 December, the entry date's year is the same as that of the value date. Otherwise | ||
| * it is either the previous year if the entry date is before the value date on the circular timeline, or the next year | ||
| * if the entry date is after the value date. | ||
| * <p> | ||
| * Limitations: This strategy can not account for an entry date more than 12 months in the past (or future). | ||
| */ | ||
| public class ShortestDeltaEntryDateResolutionStrategy implements EntryDateResolutionStrategy { | ||
| @Override | ||
| public LocalDate resolve(MonthDay entryMonthDay, LocalDate valueDate) { | ||
| int e = LocalDate.from(entryMonthDay.adjustInto(valueDate)).getDayOfYear(); | ||
| int v = valueDate.getDayOfYear(); | ||
| int lengthOfYear = valueDate.lengthOfYear(); | ||
|
|
||
| int valueToEntryDelta = v - e; | ||
| boolean valueAfterEntry = valueToEntryDelta > 0; | ||
| int shortestDelta = min(abs(valueToEntryDelta), lengthOfYear - abs(valueToEntryDelta)); | ||
|
|
||
| boolean spans31December = valueAfterEntry ? | ||
| e + shortestDelta != v : | ||
| v + shortestDelta != e; | ||
|
|
||
| int entryYear; | ||
| if (!spans31December) { | ||
| entryYear = valueDate.getYear(); | ||
| } else { | ||
| if (valueAfterEntry) { | ||
| entryYear = valueDate.getYear() + 1; | ||
| } else { | ||
| entryYear = valueDate.getYear() - 1; | ||
| } | ||
| } | ||
|
|
||
| return entryMonthDay.atYear(entryYear); | ||
| } | ||
| } |
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
...banking/swift/submessage/field/subfield/ShortestDeltaEntryDateResolutionStrategyTest.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 com.qoomon.banking.swift.submessage.field.subfield; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.MonthDay; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class ShortestDeltaEntryDateResolutionStrategyTest { | ||
| private final EntryDateResolutionStrategy strategy = new ShortestDeltaEntryDateResolutionStrategy(); | ||
|
|
||
| @Test | ||
| public void of_WHEN_smallest_delta_between_entry_date_and_value_date_does_not_span_December_THEN_entry_year_is_same_as_value_year() { | ||
| // When | ||
| LocalDate entryDate = strategy.resolve(MonthDay.parse("--05-12"), LocalDate.parse("2016-03-31")); | ||
|
|
||
| // Then | ||
| assertThat(entryDate).isEqualTo(LocalDate.parse("2016-05-12")); | ||
| } | ||
|
|
||
| @Test | ||
| public void of_WHEN_smallest_delta_does_not_span_December_and_entry_date_before_value_date_THEN_entry_year_is_same_as_value_year() { | ||
| // When | ||
| LocalDate entryDate = strategy.resolve(MonthDay.parse("--03-12"), LocalDate.parse("2016-05-31")); | ||
|
|
||
| // Then | ||
| assertThat(entryDate).isEqualTo(LocalDate.parse("2016-03-12")); | ||
| } | ||
|
|
||
| @Test | ||
| public void of_WHEN_smallest_delta_spans_December_and_entry_date_before_value_date_THEN_entry_year_is_next_year() { | ||
| // When | ||
| LocalDate entryDate = strategy.resolve(MonthDay.parse("--01-12"), LocalDate.parse("2015-10-31")); | ||
|
|
||
| // Then | ||
| assertThat(entryDate).isEqualTo(LocalDate.parse("2016-01-12")); | ||
| } | ||
|
|
||
| @Test | ||
| public void of_WHEN_smallest_delta_spans_December_and_entry_date_after_value_date_THEN_entry_year_is_previous_year() { | ||
| // When | ||
| LocalDate entryDate = strategy.resolve(MonthDay.parse("--11-12"), LocalDate.parse("2016-03-31")); | ||
|
|
||
| // Then | ||
| assertThat(entryDate).isEqualTo(LocalDate.parse("2015-11-12")); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is easier if we just pass the EntryDate year as parameter here.