1919
2020import org .bson .Document ;
2121import org .jspecify .annotations .Nullable ;
22+
2223import org .springframework .core .convert .converter .Converter ;
2324import org .springframework .data .mongodb .core .index .IndexDefinition ;
24- import org .springframework .data .mongodb .core .index .IndexInfo ;
2525import org .springframework .util .ObjectUtils ;
2626
2727import com .mongodb .client .model .Collation ;
3636 */
3737abstract class IndexConverters {
3838
39- private static final Converter <IndexDefinition , IndexOptions > DEFINITION_TO_MONGO_INDEX_OPTIONS ;
40- private static final Converter <Document , IndexInfo > DOCUMENT_INDEX_INFO ;
41-
42- static {
43-
44- DEFINITION_TO_MONGO_INDEX_OPTIONS = getIndexDefinitionIndexOptionsConverter ();
45- DOCUMENT_INDEX_INFO = getDocumentIndexInfoConverter ();
46- }
47-
4839 private IndexConverters () {
4940
5041 }
5142
52- static Converter <IndexDefinition , IndexOptions > indexDefinitionToIndexOptionsConverter () {
53- return DEFINITION_TO_MONGO_INDEX_OPTIONS ;
54- }
43+ static IndexOptions toIndexOptions (IndexDefinition indexDefinition ) {
5544
56- static Converter <Document , IndexInfo > documentToIndexInfoConverter () {
57- return DOCUMENT_INDEX_INFO ;
58- }
59-
60- private static Converter <IndexDefinition , IndexOptions > getIndexDefinitionIndexOptionsConverter () {
61-
62- return indexDefinition -> {
45+ Document indexOptions = indexDefinition .getIndexOptions ();
46+ IndexOptions ops = new IndexOptions ();
6347
64- Document indexOptions = indexDefinition .getIndexOptions ();
65- IndexOptions ops = new IndexOptions ();
66-
67- if (indexOptions .containsKey ("name" )) {
68- ops = ops .name (indexOptions .get ("name" ).toString ());
69- }
70- if (indexOptions .containsKey ("unique" )) {
71- ops = ops .unique ((Boolean ) indexOptions .get ("unique" ));
72- }
73- if (indexOptions .containsKey ("sparse" )) {
74- ops = ops .sparse ((Boolean ) indexOptions .get ("sparse" ));
75- }
76- if (indexOptions .containsKey ("background" )) {
77- ops = ops .background ((Boolean ) indexOptions .get ("background" ));
78- }
79- if (indexOptions .containsKey ("expireAfterSeconds" )) {
80- ops = ops .expireAfter ((Long ) indexOptions .get ("expireAfterSeconds" ), TimeUnit .SECONDS );
81- }
82- if (indexOptions .containsKey ("min" )) {
83- ops = ops .min (((Number ) indexOptions .get ("min" )).doubleValue ());
84- }
85- if (indexOptions .containsKey ("max" )) {
86- ops = ops .max (((Number ) indexOptions .get ("max" )).doubleValue ());
87- }
88- if (indexOptions .containsKey ("bits" )) {
89- ops = ops .bits ((Integer ) indexOptions .get ("bits" ));
90- }
91- if (indexOptions .containsKey ("default_language" )) {
92- ops = ops .defaultLanguage (indexOptions .get ("default_language" ).toString ());
93- }
94- if (indexOptions .containsKey ("language_override" )) {
95- ops = ops .languageOverride (indexOptions .get ("language_override" ).toString ());
96- }
97- if (indexOptions .containsKey ("weights" )) {
98- ops = ops .weights ((org .bson .Document ) indexOptions .get ("weights" ));
99- }
48+ if (indexOptions .containsKey ("name" )) {
49+ ops = ops .name (indexOptions .get ("name" ).toString ());
50+ }
51+ if (indexOptions .containsKey ("unique" )) {
52+ ops = ops .unique ((Boolean ) indexOptions .get ("unique" ));
53+ }
54+ if (indexOptions .containsKey ("sparse" )) {
55+ ops = ops .sparse ((Boolean ) indexOptions .get ("sparse" ));
56+ }
57+ if (indexOptions .containsKey ("background" )) {
58+ ops = ops .background ((Boolean ) indexOptions .get ("background" ));
59+ }
60+ if (indexOptions .containsKey ("expireAfterSeconds" )) {
61+ ops = ops .expireAfter ((Long ) indexOptions .get ("expireAfterSeconds" ), TimeUnit .SECONDS );
62+ }
63+ if (indexOptions .containsKey ("min" )) {
64+ ops = ops .min (((Number ) indexOptions .get ("min" )).doubleValue ());
65+ }
66+ if (indexOptions .containsKey ("max" )) {
67+ ops = ops .max (((Number ) indexOptions .get ("max" )).doubleValue ());
68+ }
69+ if (indexOptions .containsKey ("bits" )) {
70+ ops = ops .bits ((Integer ) indexOptions .get ("bits" ));
71+ }
72+ if (indexOptions .containsKey ("default_language" )) {
73+ ops = ops .defaultLanguage (indexOptions .get ("default_language" ).toString ());
74+ }
75+ if (indexOptions .containsKey ("language_override" )) {
76+ ops = ops .languageOverride (indexOptions .get ("language_override" ).toString ());
77+ }
78+ if (indexOptions .containsKey ("weights" )) {
79+ ops = ops .weights ((org .bson .Document ) indexOptions .get ("weights" ));
80+ }
10081
101- for (String key : indexOptions .keySet ()) {
102- if (ObjectUtils .nullSafeEquals ("2dsphere" , indexOptions .get (key ))) {
103- ops = ops .sphereVersion (2 );
104- }
82+ for (String key : indexOptions .keySet ()) {
83+ if (ObjectUtils .nullSafeEquals ("2dsphere" , indexOptions .get (key ))) {
84+ ops = ops .sphereVersion (2 );
10585 }
86+ }
10687
107- if (indexOptions .containsKey ("partialFilterExpression" )) {
108- ops = ops .partialFilterExpression ((org .bson .Document ) indexOptions .get ("partialFilterExpression" ));
109- }
88+ if (indexOptions .containsKey ("partialFilterExpression" )) {
89+ ops = ops .partialFilterExpression ((org .bson .Document ) indexOptions .get ("partialFilterExpression" ));
90+ }
11091
111- if (indexOptions .containsKey ("collation" )) {
112- ops = ops .collation (fromDocument (indexOptions .get ("collation" , Document .class )));
113- }
92+ if (indexOptions .containsKey ("collation" )) {
93+ ops = ops .collation (fromDocument (indexOptions .get ("collation" , Document .class )));
94+ }
11495
115- if (indexOptions .containsKey ("wildcardProjection" )) {
116- ops .wildcardProjection (indexOptions .get ("wildcardProjection" , Document .class ));
117- }
96+ if (indexOptions .containsKey ("wildcardProjection" )) {
97+ ops .wildcardProjection (indexOptions .get ("wildcardProjection" , Document .class ));
98+ }
11899
119- if (indexOptions .containsKey ("hidden" )) {
120- ops = ops .hidden ((Boolean ) indexOptions .get ("hidden" ));
121- }
100+ if (indexOptions .containsKey ("hidden" )) {
101+ ops = ops .hidden ((Boolean ) indexOptions .get ("hidden" ));
102+ }
122103
123- return ops ;
124- };
104+ return ops ;
125105 }
126106
127107 public static @ Nullable Collation fromDocument (@ Nullable Document source ) {
@@ -133,8 +113,4 @@ private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexO
133113 return org .springframework .data .mongodb .core .query .Collation .from (source ).toMongoCollation ();
134114 }
135115
136- private static Converter <Document , IndexInfo > getDocumentIndexInfoConverter () {
137- return IndexInfo ::indexInfoOf ;
138- }
139-
140116}
0 commit comments