4040@ Immutable
4141public abstract class TaggableReadPreference extends ReadPreference {
4242 private final List <TagSet > tagSetList = new ArrayList <TagSet >();
43- private final long maxStalenessMS ;
43+ private final Long maxStalenessMS ;
4444
4545 TaggableReadPreference () {
46- maxStalenessMS = 0 ;
46+ this . maxStalenessMS = null ;
4747 }
4848
49- TaggableReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
49+ TaggableReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
5050 notNull ("tagSetList" , tagSetList );
51- isTrueArgument ("maxStaleness >= 0" , maxStalenessMS >= 0 );
52- this .maxStalenessMS = maxStalenessMS ;
51+ isTrueArgument ("maxStaleness is null or >= 0" , maxStaleness == null || maxStaleness >= 0 );
52+ this .maxStalenessMS = maxStaleness == null ? null : MILLISECONDS . convert ( maxStaleness , timeUnit ) ;
5353
5454 for (final TagSet tagSet : tagSetList ) {
5555 this .tagSetList .add (tagSet );
@@ -69,7 +69,7 @@ public BsonDocument toDocument() {
6969 readPrefObject .put ("tags" , tagsListToBsonArray ());
7070 }
7171
72- if (maxStalenessMS != 0 ) {
72+ if (maxStalenessMS != null ) {
7373 readPrefObject .put ("maxStalenessMS" , new BsonInt64 (maxStalenessMS ));
7474 }
7575 return readPrefObject ;
@@ -89,13 +89,16 @@ public List<TagSet> getTagSetList() {
8989 * Gets the maximum acceptable staleness of a secondary in order to be considered for read operations.
9090 *
9191 * @param timeUnit the time unit in which to return the value
92- * @return the maximum acceptable staleness in the given time unit. The default is 0, meaning there is no staleness check.
92+ * @return the maximum acceptable staleness in the given time unit, or null if the value is not set
9393 *
9494 * @since 3.4
9595 * @mongodb.server.release 3.4
9696 */
97- public long getMaxStaleness (final TimeUnit timeUnit ) {
97+ public Long getMaxStaleness (final TimeUnit timeUnit ) {
9898 notNull ("timeUnit" , timeUnit );
99+ if (maxStalenessMS == null ) {
100+ return null ;
101+ }
99102 return timeUnit .convert (maxStalenessMS , TimeUnit .MILLISECONDS );
100103 }
101104
@@ -104,7 +107,7 @@ public String toString() {
104107 return "ReadPreference{"
105108 + "name=" + getName ()
106109 + (tagSetList .isEmpty () ? "" : ", tagSetList=" + tagSetList )
107- + (maxStalenessMS == 0 ? "" : ", maxStalenessMS=" + maxStalenessMS )
110+ + (maxStalenessMS == null ? "" : ", maxStalenessMS=" + maxStalenessMS )
108111 + '}' ;
109112 }
110113
@@ -119,7 +122,7 @@ public boolean equals(final Object o) {
119122
120123 TaggableReadPreference that = (TaggableReadPreference ) o ;
121124
122- if (maxStalenessMS != that .maxStalenessMS ) {
125+ if (maxStalenessMS != null ? ! maxStalenessMS . equals ( that .maxStalenessMS ) : that . maxStalenessMS != null ) {
123126 return false ;
124127 }
125128 if (!tagSetList .equals (that .tagSetList )) {
@@ -133,7 +136,7 @@ public boolean equals(final Object o) {
133136 public int hashCode () {
134137 int result = tagSetList .hashCode ();
135138 result = 31 * result + getName ().hashCode ();
136- result = 31 * result + (int ) ( maxStalenessMS ^ ( maxStalenessMS >>> 32 ) );
139+ result = 31 * result + (maxStalenessMS != null ? maxStalenessMS . hashCode () : 0 );
137140 return result ;
138141 }
139142
@@ -154,7 +157,7 @@ protected static ClusterDescription copyClusterDescription(final ClusterDescript
154157
155158 protected List <ServerDescription > selectFreshServers (final ClusterDescription clusterDescription ,
156159 final List <ServerDescription > servers ) {
157- if (getMaxStaleness (MILLISECONDS ) == 0 ) {
160+ if (getMaxStaleness (MILLISECONDS ) == null ) {
158161 return servers ;
159162 }
160163
@@ -248,8 +251,8 @@ static class SecondaryReadPreference extends TaggableReadPreference {
248251 SecondaryReadPreference () {
249252 }
250253
251- SecondaryReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
252- super (tagSetList , maxStalenessMS );
254+ SecondaryReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
255+ super (tagSetList , maxStaleness , timeUnit );
253256 }
254257
255258 @ Override
@@ -283,8 +286,8 @@ static class SecondaryPreferredReadPreference extends SecondaryReadPreference {
283286 SecondaryPreferredReadPreference () {
284287 }
285288
286- SecondaryPreferredReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
287- super (tagSetList , maxStalenessMS );
289+ SecondaryPreferredReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
290+ super (tagSetList , maxStaleness , timeUnit );
288291 }
289292
290293 @ Override
@@ -310,8 +313,8 @@ static class NearestReadPreference extends TaggableReadPreference {
310313 NearestReadPreference () {
311314 }
312315
313- NearestReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
314- super (tagSetList , maxStalenessMS );
316+ NearestReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
317+ super (tagSetList , maxStaleness , timeUnit );
315318 }
316319
317320
@@ -347,8 +350,8 @@ static class PrimaryPreferredReadPreference extends SecondaryReadPreference {
347350 PrimaryPreferredReadPreference () {
348351 }
349352
350- PrimaryPreferredReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
351- super (tagSetList , maxStalenessMS );
353+ PrimaryPreferredReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
354+ super (tagSetList , maxStaleness , timeUnit );
352355 }
353356
354357 @ Override
0 commit comments