/driver-core/src/main/com/mongodb/connection/ClusterId.java

https://github.com/jyemin/mongo-java-driver · Java · 112 lines · 57 code · 15 blank · 40 comment · 11 complexity · 02b56321dcff47e43be77aa354d61575 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.connection;
  17. import com.mongodb.internal.VisibleForTesting;
  18. import org.bson.types.ObjectId;
  19. import static com.mongodb.assertions.Assertions.notNull;
  20. import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
  21. /**
  22. * A client-generated identifier that uniquely identifies a connection to a MongoDB cluster, which could be sharded, replica set,
  23. * or standalone.
  24. *
  25. * @since 3.0
  26. */
  27. public final class ClusterId {
  28. private final String value;
  29. private final String description;
  30. /**
  31. * Construct an instance.
  32. *
  33. */
  34. public ClusterId() {
  35. this(null);
  36. }
  37. /**
  38. * Construct an instance.
  39. *
  40. * @param description the user defined description of the MongoClient
  41. */
  42. public ClusterId(final String description) {
  43. this.value = new ObjectId().toHexString();
  44. this.description = description;
  45. }
  46. @VisibleForTesting(otherwise = PRIVATE)
  47. ClusterId(final String value, final String description) {
  48. this.value = notNull("value", value);
  49. this.description = description;
  50. }
  51. /**
  52. * Gets the value of the identifier.
  53. *
  54. * @return the value
  55. */
  56. public String getValue() {
  57. return value;
  58. }
  59. /**
  60. * Gets the user defined description of the MongoClient.
  61. *
  62. * @return the user defined description of the MongoClient
  63. */
  64. public String getDescription() {
  65. return description;
  66. }
  67. @Override
  68. public boolean equals(final Object o) {
  69. if (this == o) {
  70. return true;
  71. }
  72. if (o == null || getClass() != o.getClass()) {
  73. return false;
  74. }
  75. ClusterId clusterId = (ClusterId) o;
  76. if (!value.equals(clusterId.value)) {
  77. return false;
  78. }
  79. if (description != null ? !description.equals(clusterId.description) : clusterId.description != null) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. @Override
  85. public int hashCode() {
  86. int result = value.hashCode();
  87. result = 31 * result + (description != null ? description.hashCode() : 0);
  88. return result;
  89. }
  90. @Override
  91. public String toString() {
  92. return "ClusterId{"
  93. + "value='" + value + '\''
  94. + ", description='" + description + '\''
  95. + '}';
  96. }
  97. }