/driver-core/src/main/com/mongodb/MongoNamespace.java

https://github.com/jyemin/mongo-java-driver · Java · 200 lines · 113 code · 20 blank · 67 comment · 15 complexity · e3865772070c3a21ca6079d5716b515e MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb;
  17. import com.mongodb.annotations.Immutable;
  18. import org.bson.codecs.pojo.annotations.BsonCreator;
  19. import org.bson.codecs.pojo.annotations.BsonIgnore;
  20. import org.bson.codecs.pojo.annotations.BsonProperty;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. import static com.mongodb.assertions.Assertions.isTrueArgument;
  24. import static com.mongodb.assertions.Assertions.notNull;
  25. import static java.util.Arrays.asList;
  26. /**
  27. * A MongoDB namespace, which includes a database name and collection name.
  28. *
  29. * @since 3.0
  30. */
  31. @Immutable
  32. public final class MongoNamespace {
  33. /**
  34. * The collection name in which to execute a command.
  35. */
  36. public static final String COMMAND_COLLECTION_NAME = "$cmd";
  37. private static final Set<Character> PROHIBITED_CHARACTERS_IN_DATABASE_NAME =
  38. new HashSet<Character>(asList('\0', '/', '\\', ' ', '"', '.'));
  39. private final String databaseName;
  40. private final String collectionName;
  41. @BsonIgnore
  42. private final String fullName; // cache to avoid repeated string building
  43. /**
  44. * Check the validity of the given database name. A valid database name is non-null, non-empty, and does not contain any of the
  45. * following characters: {@code '\0', '/', '\\', ' ', '"', '.'}. The server may impose additional restrictions on database names.
  46. *
  47. * @param databaseName the database name
  48. * @throws IllegalArgumentException if the database name is invalid
  49. * @since 3.4
  50. * @mongodb.driver.manual reference/limits/#naming-restrictions Naming Restrictions
  51. */
  52. public static void checkDatabaseNameValidity(final String databaseName) {
  53. notNull("databaseName", databaseName);
  54. isTrueArgument("databaseName is not empty", !databaseName.isEmpty());
  55. for (int i = 0; i < databaseName.length(); i++) {
  56. if (PROHIBITED_CHARACTERS_IN_DATABASE_NAME.contains(databaseName.charAt(i))) {
  57. throw new IllegalArgumentException("state should be: databaseName does not contain '" + databaseName.charAt(i) + "'");
  58. }
  59. }
  60. }
  61. /**
  62. * Check the validity of the given collection name. A valid collection name is non-null and non-empty. The server may impose
  63. * additional restrictions on collection names.
  64. *
  65. * @param collectionName the collection name
  66. * @throws IllegalArgumentException if the collection name is invalid
  67. * @since 3.4
  68. * @mongodb.driver.manual reference/limits/#naming-restrictions Naming Restrictions
  69. */
  70. public static void checkCollectionNameValidity(final String collectionName) {
  71. notNull("collectionName", collectionName);
  72. isTrueArgument("collectionName is not empty", !collectionName.isEmpty());
  73. }
  74. /**
  75. * Construct an instance for the given full name. The database name is the string preceding the first {@code "."} character.
  76. *
  77. * @param fullName the non-null full namespace
  78. * @see #checkDatabaseNameValidity(String)
  79. * @see #checkCollectionNameValidity(String)
  80. */
  81. public MongoNamespace(final String fullName) {
  82. notNull("fullName", fullName);
  83. this.fullName = fullName;
  84. this.databaseName = getDatatabaseNameFromFullName(fullName);
  85. this.collectionName = getCollectionNameFullName(fullName);
  86. checkDatabaseNameValidity(databaseName);
  87. checkCollectionNameValidity(collectionName);
  88. }
  89. /**
  90. * Construct an instance from the given database name and collection name.
  91. *
  92. * @param databaseName the valid database name
  93. * @param collectionName the valid collection name
  94. * @see #checkDatabaseNameValidity(String)
  95. * @see #checkCollectionNameValidity(String)
  96. */
  97. @BsonCreator
  98. public MongoNamespace(@BsonProperty("db") final String databaseName,
  99. @BsonProperty("coll") final String collectionName) {
  100. checkDatabaseNameValidity(databaseName);
  101. checkCollectionNameValidity(collectionName);
  102. this.databaseName = databaseName;
  103. this.collectionName = collectionName;
  104. this.fullName = databaseName + '.' + collectionName;
  105. }
  106. /**
  107. * Gets the database name.
  108. *
  109. * @return the database name
  110. */
  111. @BsonProperty("db")
  112. public String getDatabaseName() {
  113. return databaseName;
  114. }
  115. /**
  116. * Gets the collection name.
  117. *
  118. * @return the collection name
  119. */
  120. @BsonProperty("coll")
  121. public String getCollectionName() {
  122. return collectionName;
  123. }
  124. /**
  125. * Gets the full name, which is the database name and the collection name, separated by a period.
  126. *
  127. * @return the full name
  128. */
  129. public String getFullName() {
  130. return fullName;
  131. }
  132. @Override
  133. public boolean equals(final Object o) {
  134. if (this == o) {
  135. return true;
  136. }
  137. if (o == null || getClass() != o.getClass()) {
  138. return false;
  139. }
  140. MongoNamespace that = (MongoNamespace) o;
  141. if (!collectionName.equals(that.collectionName)) {
  142. return false;
  143. }
  144. if (!databaseName.equals(that.databaseName)) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * Returns the standard MongoDB representation of a namespace, which is {@code &lt;database&gt;.&lt;collection&gt;}.
  151. *
  152. * @return string representation of the namespace.
  153. */
  154. @Override
  155. public String toString() {
  156. return fullName;
  157. }
  158. @Override
  159. public int hashCode() {
  160. int result = databaseName.hashCode();
  161. result = 31 * result + (collectionName.hashCode());
  162. return result;
  163. }
  164. private static String getCollectionNameFullName(final String namespace) {
  165. int firstDot = namespace.indexOf('.');
  166. if (firstDot == -1) {
  167. return namespace;
  168. }
  169. return namespace.substring(firstDot + 1);
  170. }
  171. private static String getDatatabaseNameFromFullName(final String namespace) {
  172. int firstDot = namespace.indexOf('.');
  173. if (firstDot == -1) {
  174. return "";
  175. }
  176. return namespace.substring(0, firstDot);
  177. }
  178. }