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

https://github.com/foursquare/mongo-java-driver · Java · 154 lines · 81 code · 22 blank · 51 comment · 16 complexity · 151a235a32f82247b074f426051a08ba MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 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 static com.mongodb.assertions.Assertions.isTrueArgument;
  19. import static com.mongodb.assertions.Assertions.notNull;
  20. /**
  21. * A MongoDB namespace, which includes a database name and collection name.
  22. *
  23. * @since 3.0
  24. */
  25. @Immutable
  26. public final class MongoNamespace {
  27. public static final String COMMAND_COLLECTION_NAME = "$cmd";
  28. private final String databaseName;
  29. private final String collectionName;
  30. private final String fullName; // cache to avoid repeated string building
  31. /**
  32. * Construct an instance.
  33. *
  34. * @param fullName the full namespace
  35. */
  36. public MongoNamespace(final String fullName) {
  37. notNull("fullName", fullName);
  38. isTrueArgument("fullName is of form <db>.<collection>", isFullNameValid(fullName));
  39. this.databaseName = getDatatabaseNameFromFullName(fullName);
  40. this.collectionName = getCollectionNameFullName(fullName);
  41. this.fullName = fullName;
  42. }
  43. /**
  44. * Construct an instance.
  45. *
  46. * @param databaseName the non-null database name
  47. * @param collectionName the non-null collection name
  48. */
  49. public MongoNamespace(final String databaseName, final String collectionName) {
  50. this.databaseName = notNull("databaseName", databaseName);
  51. this.collectionName = notNull("collectionName", collectionName);
  52. this.fullName = databaseName + "." + collectionName;
  53. }
  54. /**
  55. * Gets the database name.
  56. *
  57. * @return the database name
  58. */
  59. public String getDatabaseName() {
  60. return databaseName;
  61. }
  62. /**
  63. * Gets the collection name.
  64. *
  65. * @return the collection name
  66. */
  67. public String getCollectionName() {
  68. return collectionName;
  69. }
  70. /**
  71. * Gets the full name, which is the database name and the collection name, separated by a period.
  72. *
  73. * @return the full name
  74. */
  75. public String getFullName() {
  76. return fullName;
  77. }
  78. @Override
  79. public boolean equals(final Object o) {
  80. if (this == o) {
  81. return true;
  82. }
  83. if (o == null || getClass() != o.getClass()) {
  84. return false;
  85. }
  86. MongoNamespace that = (MongoNamespace) o;
  87. if (!collectionName.equals(that.collectionName)) {
  88. return false;
  89. }
  90. if (!databaseName.equals(that.databaseName)) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. /**
  96. * Returns the standard MongoDB representation of a namespace, which is {@code &lt;database&gt;.&lt;collection&gt;}.
  97. *
  98. * @return string representation of the namespace.
  99. */
  100. @Override
  101. public String toString() {
  102. return fullName;
  103. }
  104. @Override
  105. public int hashCode() {
  106. int result = databaseName.hashCode();
  107. result = 31 * result + (collectionName.hashCode());
  108. return result;
  109. }
  110. private static boolean isFullNameValid(final String fullName) {
  111. int firstDotIndex = fullName.indexOf(".");
  112. if (firstDotIndex == -1) {
  113. return false;
  114. }
  115. if (firstDotIndex == 0) {
  116. return false;
  117. }
  118. if (fullName.charAt(fullName.length() - 1) == '.') {
  119. return false;
  120. }
  121. if (fullName.charAt(firstDotIndex + 1) == '.') {
  122. return false;
  123. }
  124. return true;
  125. }
  126. private static String getCollectionNameFullName(final String namespace) {
  127. return namespace.substring(namespace.indexOf('.') + 1);
  128. }
  129. private static String getDatatabaseNameFromFullName(final String namespace) {
  130. return namespace.substring(0, namespace.indexOf('.'));
  131. }
  132. }