/hudson-utils/src/main/java/org/hudsonci/utils/id/GUID.java

http://github.com/hudson/hudson · Java · 86 lines · 43 code · 14 blank · 29 comment · 6 complexity · 3034e670ce58b5ee1319e684415b8085 MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.utils.id;
  25. import com.thoughtworks.xstream.annotations.XStreamAlias;
  26. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  27. import java.util.UUID;
  28. import static com.google.common.base.Preconditions.checkNotNull;
  29. /**
  30. * Globally Unique IDentifier; backed by {@link UUID}.
  31. *
  32. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  33. * @since 2.1.0
  34. */
  35. @XStreamAlias("guid")
  36. public class GUID
  37. implements ID
  38. {
  39. @XStreamAsAttribute
  40. private final UUID value;
  41. public GUID(final UUID value) {
  42. this.value = checkNotNull(value);
  43. }
  44. public GUID(final String value) {
  45. this(UUID.fromString(value));
  46. }
  47. public GUID() {
  48. this(UUID.randomUUID());
  49. }
  50. public UUID getValue() {
  51. return value;
  52. }
  53. @Override
  54. public boolean equals(final Object obj) {
  55. if (this == obj) {
  56. return true;
  57. }
  58. if (!(obj instanceof GUID)) {
  59. return false;
  60. }
  61. GUID that = (GUID) obj;
  62. return !(value != null ? !value.equals(that.value) : that.value != null);
  63. }
  64. @Override
  65. public int hashCode() {
  66. return value != null ? value.hashCode() : 0;
  67. }
  68. @Override
  69. public String toString() {
  70. return value.toString();
  71. }
  72. }