@@ -86,15 +86,15 @@ public Optional<Collation> getCollation() {
86
86
* @return {@link Optional#empty()} if not set.
87
87
*/
88
88
public Optional <Instant > getResumeTimestamp () {
89
- return Optional .ofNullable (resumeTimestamp ).map (this :: asInstant );
89
+ return Optional .ofNullable (resumeTimestamp ).map (timestamp -> asTimestampOfType ( timestamp , Instant . class ) );
90
90
}
91
91
92
92
/**
93
93
* @return {@link Optional#empty()} if not set.
94
94
* @since 2.2
95
95
*/
96
96
public Optional <BsonTimestamp > getResumeBsonTimestamp () {
97
- return Optional .ofNullable (resumeTimestamp ).map (this :: asBsonTimestamp );
97
+ return Optional .ofNullable (resumeTimestamp ).map (timestamp -> asTimestampOfType ( timestamp , BsonTimestamp . class ) );
98
98
}
99
99
100
100
/**
@@ -114,30 +114,26 @@ public static ChangeStreamOptionsBuilder builder() {
114
114
return new ChangeStreamOptionsBuilder ();
115
115
}
116
116
117
- private Instant asInstant (Object timestamp ) {
118
- return asTimestampOfType ( timestamp , Instant . class );
117
+ private static < T > T asTimestampOfType (Object timestamp , Class < T > targetType ) {
118
+ return targetType . cast ( doGetTimestamp ( timestamp , targetType ) );
119
119
}
120
120
121
- private BsonTimestamp asBsonTimestamp (Object timestamp ) {
122
- return asTimestampOfType (timestamp , BsonTimestamp .class );
123
- }
124
-
125
- private <T > T asTimestampOfType (Object timestamp , Class <T > targetType ) {
121
+ private static <T > Object doGetTimestamp (Object timestamp , Class <T > targetType ) {
126
122
127
123
if (ClassUtils .isAssignableValue (targetType , timestamp )) {
128
- return ( T ) timestamp ;
124
+ return timestamp ;
129
125
}
130
126
131
127
if (timestamp instanceof Instant ) {
132
- return ( T ) new BsonTimestamp ((int ) ((Instant ) timestamp ).getEpochSecond (), 0 );
128
+ return new BsonTimestamp ((int ) ((Instant ) timestamp ).getEpochSecond (), 0 );
133
129
}
134
130
135
131
if (timestamp instanceof BsonTimestamp ) {
136
- return ( T ) Instant .ofEpochSecond (((BsonTimestamp ) timestamp ).getTime ());
132
+ return Instant .ofEpochSecond (((BsonTimestamp ) timestamp ).getTime ());
137
133
}
138
134
139
135
throw new IllegalArgumentException (
140
- "o_O that should actually not happen. The timestampt should be an Instant or a BsonTimestamp but was "
136
+ "o_O that should actually not happen. The timestamp should be an Instant or a BsonTimestamp but was "
141
137
+ ObjectUtils .nullSafeClassName (timestamp ));
142
138
}
143
139
0 commit comments