Skip to content

Commit

Permalink
Merge branch 'main' into add-result-of-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen authored Jun 6, 2024
2 parents a6e4b86 + 2ed984c commit 7bbad68
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-examples</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Strata-Examples</name>
<description>Example code to demonstrate use of Strata</description>
Expand Down
2 changes: 1 addition & 1 deletion modules/basics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-basics</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,18 @@ private void validate() {
List<ValueStep> resolve(List<ValueStep> existingSteps, RollConvention rollConv) {
ImmutableList.Builder<ValueStep> steps = ImmutableList.builder();
steps.addAll(existingSteps);
LocalDate prev = firstStepDate;
LocalDate date = firstStepDate;
while (!date.isAfter(lastStepDate)) {
LocalDate prev = rollConv.adjust(firstStepDate);
LocalDate date = rollConv.adjust(firstStepDate);
LocalDate adjustedLastStepDate = rollConv.adjust(lastStepDate);
while (!date.isAfter(adjustedLastStepDate)) {
steps.add(ValueStep.of(date, adjustment));
prev = date;
date = rollConv.next(date, frequency);
}
if (!prev.equals(lastStepDate)) {
if (!prev.equals(adjustedLastStepDate)) {
throw new IllegalArgumentException(Messages.format(
"ValueStepSequence lastStepDate did not match frequency '{}' using roll convention '{}', {} != {}",
frequency, rollConv, lastStepDate, prev));
frequency, rollConv, adjustedLastStepDate, prev));
}
return steps.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public void test_resolve() {
assertThat(steps.get(3)).isEqualTo(ValueStep.of(date(2016, 10, 20), ADJ));
}

@Test
public void test_resolve_with_roll_convention() {
ValueStepSequence test = ValueStepSequence.of(date(2022, 9, 21), date(2026, 9, 21), Frequency.P12M, ADJ);
List<ValueStep> steps = test.resolve(ImmutableList.of(), RollConventions.IMM);
assertThat(steps.size()).isEqualTo(5);
assertThat(steps.get(0)).isEqualTo(ValueStep.of(date(2022, 9, 21), ADJ));
assertThat(steps.get(1)).isEqualTo(ValueStep.of(date(2023, 9, 20), ADJ));
assertThat(steps.get(2)).isEqualTo(ValueStep.of(date(2024, 9, 18), ADJ));
assertThat(steps.get(3)).isEqualTo(ValueStep.of(date(2025, 9, 17), ADJ));
assertThat(steps.get(4)).isEqualTo(ValueStep.of(date(2026, 9, 16), ADJ));
}

@Test
public void test_resolve_invalid() {
ValueStepSequence test = ValueStepSequence.of(date(2016, 4, 20), date(2016, 10, 20), Frequency.P12M, ADJ);
Expand Down
2 changes: 1 addition & 1 deletion modules/calc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-calc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/collect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-collect</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ public static <R> Predicate<R> not(Predicate<R> predicate) {
*
* @param <T> the type of element in the stream
* @return the operator
* @throws IllegalArgumentException if more than one element is present
*/
public static <T> BinaryOperator<T> ensureOnlyOne() {
return (a, b) -> {
Expand All @@ -690,6 +691,31 @@ public static <T> BinaryOperator<T> ensureOnlyOne() {
};
}

/**
* Reducer used in a stream to ensure there is no more than one matching element.
* <p>
* This method returns an operator that can be used with {@link Stream#reduce(BinaryOperator)}
* that returns either zero or one elements from the stream. Unlike {@link Stream#findFirst()}
* or {@link Stream#findAny()}, this approach ensures an exception is thrown if there
* is more than one element in the stream.
* <p>
* This would be used as follows (with a static import):
* <pre>
* stream.filter(...).reduce(ensureOnlyOne()).get();
* </pre>
*
* @param <T> the type of element in the stream
* @param message the message template for the {@link IllegalArgumentException} with "{}" placeholders
* @param args the arguments for the message
* @return the operator
* @throws IllegalArgumentException if more than one element is present
*/
public static <T> BinaryOperator<T> ensureOnlyOne(String message, Object... args) {
return (a, b) -> {
throw new IllegalArgumentException(Messages.format(message, args));
};
}

//-------------------------------------------------------------------------
/**
* Function used in a stream to cast instances to a particular type without filtering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ public void test_ensureOnlyOne() {
assertThatIllegalArgumentException().isThrownBy(() -> Stream.of("a", "b").reduce(Guavate.ensureOnlyOne()));
}

@Test
public void test_ensureOnlyOne_withCustomMessage() {
String message = "Expected one letter but found multiple for date {}";
LocalDate arg = LocalDate.of(2024, 4, 24);
assertThat(Stream.empty().reduce(Guavate.ensureOnlyOne(message, arg))).isEqualTo(Optional.empty());
assertThat(Stream.of("a").reduce(Guavate.ensureOnlyOne(message, arg))).isEqualTo(Optional.of("a"));
assertThatIllegalArgumentException().isThrownBy(() -> Stream.of("a", "b")
.reduce(Guavate.ensureOnlyOne(message, arg)))
.withMessage(Messages.format(message, arg));
}

//-------------------------------------------------------------------------
@Test
public void test_casting() {
Expand Down
2 changes: 1 addition & 1 deletion modules/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-data</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-loader</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/market/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-market</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/math/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-math</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/measure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-measure</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Strata-Parent</name>
<description>OpenGamma Strata Parent</description>
Expand Down
2 changes: 1 addition & 1 deletion modules/pricer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-pricer</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/product/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-product</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-parent</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>strata-report</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.opengamma.strata</groupId>
<artifactId>strata-root</artifactId>
<version>2.12.39-SNAPSHOT</version>
<version>2.12.40-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Strata-Root</name>
<description>OpenGamma Strata root</description>
Expand Down

0 comments on commit 7bbad68

Please sign in to comment.