2020import java .util .List ;
2121import java .util .Map ;
2222
23+ import static java .util .Arrays .asList ;
24+
2325
2426/**
2527 * An abstract class that represents preferred replica set members to which a query or command can be sent.
@@ -38,7 +40,9 @@ public abstract class ReadPreference {
3840
3941 /**
4042 * @return <code>DBObject</code> representation of this preference
43+ * @deprecated for internal use only
4144 */
45+ @ Deprecated
4246 public abstract DBObject toDBObject ();
4347
4448 /**
@@ -111,7 +115,7 @@ public TaggedReadPreference(Map<String, String> tags) {
111115 }
112116 _tags = new BasicDBObject (tags );
113117 List <DBObject > maps = splitMapIntoMultipleMaps (_tags );
114- _pref = new TaggableReadPreference .SecondaryReadPreference (maps .get (0 ), getRemainingMaps (maps ));
118+ _pref = new TaggableReadPreference .SecondaryReadPreference (toTagsList ( maps .get (0 ), getRemainingMaps (maps ) ));
115119
116120 }
117121
@@ -121,7 +125,7 @@ public TaggedReadPreference(DBObject tags) {
121125 }
122126 _tags = tags ;
123127 List <DBObject > maps = splitMapIntoMultipleMaps (_tags );
124- _pref = new TaggableReadPreference .SecondaryReadPreference (maps .get (0 ), getRemainingMaps (maps ));
128+ _pref = new TaggableReadPreference .SecondaryReadPreference (toTagsList ( maps .get (0 ), getRemainingMaps (maps ) ));
125129 }
126130
127131 public DBObject getTags () {
@@ -187,47 +191,156 @@ public static ReadPreference primaryPreferred() {
187191 }
188192
189193 /**
194+ * @return ReadPreference which reads secondary.
195+ */
196+ public static ReadPreference secondary () {
197+ return _SECONDARY ;
198+ }
199+
200+ /**
201+ * @return ReadPreference which reads secondary if available, otherwise from primary.
202+ */
203+ public static ReadPreference secondaryPreferred () {
204+ return _SECONDARY_PREFERRED ;
205+ }
206+
207+ /**
208+ * @return ReadPreference which reads nearest node.
209+ */
210+ public static ReadPreference nearest () {
211+ return _NEAREST ;
212+ }
213+
214+ /**
215+ * Return a primary preferred read preference with the given tag set.
216+ *
190217 * @return ReadPreference which reads primary if available, otherwise a secondary respective of tags.
218+ * @since 2.13
191219 */
192- public static TaggableReadPreference primaryPreferred (DBObject firstTagSet , DBObject ... remainingTagSets ) {
193- return new TaggableReadPreference . PrimaryPreferredReadPreference ( firstTagSet , remainingTagSets );
220+ public static TaggableReadPreference primaryPreferred (TagSet tagSet ) {
221+ return primaryPreferred ( asList ( tagSet ) );
194222 }
195223
196224 /**
197- * @return ReadPreference which reads secondary.
225+ * Return a secondary read preference with the given tag set.
226+ *
227+ * * @return ReadPreference which reads secondary respective of tags.
228+ * @since 2.13
198229 */
199- public static ReadPreference secondary () {
200- return _SECONDARY ;
230+ public static TaggableReadPreference secondary (TagSet tagSet ) {
231+ return secondary (asList (tagSet ));
232+ }
233+
234+ /**
235+ * Return a secondary preferred read preference with the given tag set.
236+ *
237+ * @return ReadPreference which reads secondary if available respective of tags, otherwise from primary irrespective of tags.
238+ * @since 2.13
239+ */
240+ public static TaggableReadPreference secondaryPreferred (TagSet tagSet ) {
241+ return secondaryPreferred (asList (tagSet ));
242+ }
243+
244+ /**
245+ * Return a nearest read preference with the given tag set.
246+ *
247+ * @return ReadPreference which reads nearest respective of tags
248+ * @since 2.13
249+ */
250+ public static TaggableReadPreference nearest (TagSet tagSet ) {
251+ return nearest (asList (tagSet ));
252+ }
253+
254+ /**
255+ * Return a primary preferred read preference with the given list of tag sets.
256+ *
257+ * @return ReadPreference which reads primary if available, otherwise a secondary respective of tags.
258+ * @since 2.13
259+ */
260+ public static TaggableReadPreference primaryPreferred (List <TagSet > tagSetList ) {
261+ return new TaggableReadPreference .PrimaryPreferredReadPreference (tagSetList );
201262 }
202263
203264 /**
265+ * Return a secondary read preference with the given list of tag sets.
266+ *
204267 * @return ReadPreference which reads secondary respective of tags.
268+ * @since 2.13
205269 */
206- public static TaggableReadPreference secondary (DBObject firstTagSet , DBObject ... remainingTagSets ) {
207- return new TaggableReadPreference .SecondaryReadPreference (firstTagSet , remainingTagSets );
270+ public static TaggableReadPreference secondary (List < TagSet > tagSetList ) {
271+ return new TaggableReadPreference .SecondaryReadPreference (tagSetList );
208272 }
209273
210274 /**
211- * @return ReadPreference which reads secondary if available, otherwise from primary.
275+ * Return a secondary preferred read preference with the given list of tag sets.
276+ *
277+ * @return ReadPreference which reads secondary if available respective of tags, otherwise from primary irrespective of tags.
278+ * @since 2.13
212279 */
213- public static ReadPreference secondaryPreferred () {
214- return _SECONDARY_PREFERRED ;
280+ public static TaggableReadPreference secondaryPreferred (List <TagSet > tagSetList ) {
281+ return new TaggableReadPreference .SecondaryPreferredReadPreference (tagSetList );
282+ }
283+
284+ /**
285+ * Return a nearest read preference with the given list of tag sets.
286+ *
287+ * @return ReadPreference which reads nearest respective of tags
288+ * @since 2.13
289+ */
290+ public static TaggableReadPreference nearest (List <TagSet > tagSetList ) {
291+ return new TaggableReadPreference .NearestReadPreference (tagSetList );
292+ }
293+
294+ /**
295+ * @return ReadPreference which reads primary if available, otherwise a secondary respective of tags.
296+ * @deprecated use factory methods that take {@code TagSet} instead
297+ * @see com.mongodb.ReadPreference#primaryPreferred(TagSet)
298+ * @see com.mongodb.ReadPreference#primaryPreferred(java.util.List)
299+ */
300+ @ Deprecated
301+ public static TaggableReadPreference primaryPreferred (DBObject firstTagSet , DBObject ... remainingTagSets ) {
302+ return new TaggableReadPreference .PrimaryPreferredReadPreference (toTagsList (firstTagSet , remainingTagSets ));
303+ }
304+
305+ /**
306+ * @return ReadPreference which reads secondary respective of tags.
307+ * @deprecated use factory methods that take {@code TagSet} instead
308+ * @see com.mongodb.ReadPreference#secondary(TagSet)
309+ * @see com.mongodb.ReadPreference#secondary(java.util.List)
310+ */
311+ @ Deprecated
312+ public static TaggableReadPreference secondary (DBObject firstTagSet , DBObject ... remainingTagSets ) {
313+ return new TaggableReadPreference .SecondaryReadPreference (toTagsList (firstTagSet , remainingTagSets ));
215314 }
216315
217316 /**
218317 * @return ReadPreference which reads secondary if available respective of tags, otherwise from primary irrespective of tags.
318+ * @deprecated use factory methods that take {@code TagSet} instead
319+ * @see com.mongodb.ReadPreference#secondaryPreferred(TagSet)
320+ * @see com.mongodb.ReadPreference#secondaryPreferred(java.util.List)
219321 */
322+ @ Deprecated
220323 public static TaggableReadPreference secondaryPreferred (DBObject firstTagSet , DBObject ... remainingTagSets ) {
221- return new TaggableReadPreference .SecondaryPreferredReadPreference (firstTagSet , remainingTagSets );
324+ return new TaggableReadPreference .SecondaryPreferredReadPreference (toTagsList ( firstTagSet , remainingTagSets ) );
222325 }
223326
224327 /**
225- * @return ReadPreference which reads nearest node.
328+ * @return ReadPreference which reads nearest node respective of tags.
329+ * @deprecated use factory methods that take {@code TagSet} instead
330+ * @see com.mongodb.ReadPreference#nearest(TagSet)
331+ * @see com.mongodb.ReadPreference#nearest(java.util.List)
226332 */
227- public static ReadPreference nearest () {
228- return _NEAREST ;
333+ @ Deprecated
334+ public static TaggableReadPreference nearest (DBObject firstTagSet , DBObject ... remainingTagSets ) {
335+ return new TaggableReadPreference .NearestReadPreference (toTagsList (firstTagSet , remainingTagSets ));
229336 }
230337
338+ /**
339+ * Creates a read preference from the given read preference name.
340+ *
341+ * @param name the name of the read preference
342+ * @return the read preference
343+ */
231344 public static ReadPreference valueOf (String name ) {
232345 if (name == null ) {
233346 throw new IllegalArgumentException ();
@@ -254,34 +367,48 @@ public static ReadPreference valueOf(String name) {
254367 throw new IllegalArgumentException ("No match for read preference of " + name );
255368 }
256369
257- public static TaggableReadPreference valueOf (String name , DBObject firstTagSet , final DBObject ... remainingTagSets ) {
370+ /**
371+ * Creates a taggable read preference from the given read preference name and list of tag sets.
372+ *
373+ * @param name the name of the read preference
374+ * @param tagSetList the list of tag sets
375+ * @return the taggable read preference
376+ */
377+ public static TaggableReadPreference valueOf (String name , List <TagSet > tagSetList ) {
258378 if (name == null ) {
259379 throw new IllegalArgumentException ();
260380 }
261381
262382 name = name .toLowerCase ();
263383
264384 if (name .equals (_SECONDARY .getName ().toLowerCase ())) {
265- return new TaggableReadPreference . SecondaryReadPreference ( firstTagSet , remainingTagSets );
385+ return secondary ( tagSetList );
266386 }
267387 if (name .equals (_SECONDARY_PREFERRED .getName ().toLowerCase ())) {
268- return new TaggableReadPreference . SecondaryPreferredReadPreference ( firstTagSet , remainingTagSets );
388+ return secondaryPreferred ( tagSetList );
269389 }
270390 if (name .equals (_PRIMARY_PREFERRED .getName ().toLowerCase ())) {
271- return new TaggableReadPreference . PrimaryPreferredReadPreference ( firstTagSet , remainingTagSets );
391+ return primaryPreferred ( tagSetList );
272392 }
273393 if (name .equals (_NEAREST .getName ().toLowerCase ())) {
274- return new TaggableReadPreference . NearestReadPreference ( firstTagSet , remainingTagSets );
394+ return nearest ( tagSetList );
275395 }
276396
277397 throw new IllegalArgumentException ("No match for read preference of " + name );
278398 }
279399
280400 /**
281- * @return ReadPreference which reads nearest node respective of tags.
401+ * Creates a taggable read preference from the given read preference name and list of tag sets.
402+ *
403+ * @param name the name of the read preference
404+ * @param firstTagSet the first set of tags
405+ * @param remainingTagSets the remaining set of tags
406+ * @return the taggable read preference
407+ * @deprecated use method that takes a {@code List<TagSet>}
282408 */
283- public static TaggableReadPreference nearest (DBObject firstTagSet , DBObject ... remainingTagSets ) {
284- return new TaggableReadPreference .NearestReadPreference (firstTagSet , remainingTagSets );
409+ @ Deprecated
410+ public static TaggableReadPreference valueOf (String name , DBObject firstTagSet , final DBObject ... remainingTagSets ) {
411+ return valueOf (name , toTagsList (firstTagSet , remainingTagSets ));
285412 }
286413
287414 /**
@@ -331,6 +458,24 @@ public static ReadPreference withTags( final DBObject tags ) {
331458 private static final ReadPreference _PRIMARY_PREFERRED ;
332459 private static final ReadPreference _NEAREST ;
333460
461+ private static List <TagSet > toTagsList (DBObject firstTagSet , DBObject ... remainingTagSets ) {
462+ List <TagSet > tagsList = new ArrayList <TagSet >(remainingTagSets .length + 1 );
463+ tagsList .add (toTags (firstTagSet ));
464+ for (DBObject cur : remainingTagSets ) {
465+ tagsList .add (toTags (cur ));
466+ }
467+
468+ return tagsList ;
469+ }
470+
471+ private static TagSet toTags (final DBObject tagsDocument ) {
472+ List <Tag > tagList = new ArrayList <Tag >();
473+ for (String key : tagsDocument .keySet ()) {
474+ tagList .add (new Tag (key , tagsDocument .get (key ).toString ()));
475+ }
476+ return new TagSet (tagList );
477+ }
478+
334479 static {
335480 _PRIMARY = new PrimaryReadPreference ();
336481 _SECONDARY = new TaggableReadPreference .SecondaryReadPreference ();
0 commit comments