2020
2121import static com .mongodb .assertions .Assertions .isTrueArgument ;
2222import static com .mongodb .assertions .Assertions .notNull ;
23+ import static java .util .Arrays .asList ;
2324
2425/**
2526 * A MongoDB namespace, which includes a database name and collection name.
@@ -35,30 +36,66 @@ public final class MongoNamespace {
3536 private final String fullName ; // cache to avoid repeated string building
3637
3738 /**
38- * Construct an instance.
39+ * Check the validity of the given database name. A valid database name is non-null, non-empty, and does not contain any of the
40+ * following strings: {@code " ", "."}. The server may impose additional restrictions on database names.
3941 *
40- * @param fullName the full namespace
42+ * @param databaseName the database name
43+ * @throws IllegalArgumentException if the database name is invalid
44+ * @since 3.4
45+ * @mongodb.driver.manual reference/limits/#naming-restrictions Naming Restrictions
4146 */
42- public MongoNamespace (final String fullName ) {
43- notNull ("fullName" , fullName );
44- isTrueArgument ("fullName is of form <db>.<collection>" , isFullNameValid (fullName ));
47+ public static void checkDatabaseNameValidity (final String databaseName ) {
48+ notNull ("databaseName" , databaseName );
49+ isTrueArgument ("databaseName is not empty" , !databaseName .isEmpty ());
50+ for (String cur : asList (" " , "." )) {
51+ isTrueArgument ("databaseName does not contain '" + cur + "'" , !databaseName .contains (cur ));
52+ }
53+ }
4554
55+ /**
56+ * Check the validity of the given collection name. A valid collection name is non-null and non-empty. The server may impose
57+ * additional restrictions on collection names.
58+ *
59+ * @param collectionName the collection name
60+ * @throws IllegalArgumentException if the collection name is invalid
61+ * @since 3.4
62+ * @mongodb.driver.manual reference/limits/#naming-restrictions Naming Restrictions
63+ */
64+ public static void checkCollectionNameValidity (final String collectionName ) {
65+ notNull ("collectionName" , collectionName );
66+ isTrueArgument ("collectionName is not empty" , !collectionName .isEmpty ());
67+ }
4668
69+ /**
70+ * Construct an instance for the given full name. The database name is the string preceding the first {@code "."} character.
71+ *
72+ * @param fullName the non-null full namespace
73+ * @see #checkDatabaseNameValidity(String)
74+ * @see #checkCollectionNameValidity(String)
75+ */
76+ public MongoNamespace (final String fullName ) {
77+ notNull ("fullName" , fullName );
78+ this .fullName = fullName ;
4779 this .databaseName = getDatatabaseNameFromFullName (fullName );
4880 this .collectionName = getCollectionNameFullName (fullName );
49- this .fullName = fullName ;
81+ checkDatabaseNameValidity (databaseName );
82+ checkCollectionNameValidity (collectionName );
5083 }
5184
5285 /**
53- * Construct an instance.
86+ * Construct an instance from the given database name and collection name .
5487 *
55- * @param databaseName the non-null database name
56- * @param collectionName the non-null collection name
88+ * @param databaseName the valid database name
89+ * @param collectionName the valid collection name
90+ * @see #checkDatabaseNameValidity(String)
91+ * @see #checkCollectionNameValidity(String)
5792 */
5893 public MongoNamespace (final String databaseName , final String collectionName ) {
59- this .databaseName = notNull ("databaseName" , databaseName );
60- this .collectionName = notNull ("collectionName" , collectionName );
61- this .fullName = databaseName + "." + collectionName ;
94+ checkDatabaseNameValidity (databaseName );
95+ checkCollectionNameValidity (collectionName );
96+ this .databaseName = databaseName ;
97+ this .collectionName = collectionName ;
98+ this .fullName = databaseName + '.' + collectionName ;
6299 }
63100
64101 /**
@@ -126,29 +163,25 @@ public int hashCode() {
126163 return result ;
127164 }
128165
129- private static boolean isFullNameValid (final String fullName ) {
130- int firstDotIndex = fullName .indexOf ("." );
131-
132- if (firstDotIndex == -1 ) {
133- return false ;
134- }
135- if (firstDotIndex == 0 ) {
136- return false ;
137- }
138- if (fullName .charAt (fullName .length () - 1 ) == '.' ) {
139- return false ;
166+ private static String getCollectionNameFullName (final String namespace ) {
167+ if (namespace == null ) {
168+ return null ;
140169 }
141- if (fullName .charAt (firstDotIndex + 1 ) == '.' ) {
142- return false ;
170+ int firstDot = namespace .indexOf ('.' );
171+ if (firstDot == -1 ) {
172+ return namespace ;
143173 }
144- return true ;
145- }
146-
147- private static String getCollectionNameFullName (final String namespace ) {
148- return namespace .substring (namespace .indexOf ('.' ) + 1 );
174+ return namespace .substring (firstDot + 1 );
149175 }
150176
151177 private static String getDatatabaseNameFromFullName (final String namespace ) {
152- return namespace .substring (0 , namespace .indexOf ('.' ));
178+ if (namespace == null ) {
179+ return null ;
180+ }
181+ int firstDot = namespace .indexOf ('.' );
182+ if (firstDot == -1 ) {
183+ return "" ;
184+ }
185+ return namespace .substring (0 , firstDot );
153186 }
154187}
0 commit comments