Identifier.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package me.ag2s.epublib.domain;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. import java.util.UUID;
  5. import me.ag2s.epublib.util.StringUtil;
  6. /**
  7. * A Book's identifier.
  8. *
  9. * Defaults to a random UUID and scheme "UUID"
  10. *
  11. * @author paul
  12. */
  13. public class Identifier implements Serializable {
  14. private static final long serialVersionUID = 955949951416391810L;
  15. @SuppressWarnings("unused")
  16. public interface Scheme {
  17. String UUID = "UUID";
  18. String ISBN = "ISBN";
  19. String URL = "URL";
  20. String URI = "URI";
  21. }
  22. private boolean bookId = false;
  23. private String scheme;
  24. private String value;
  25. /**
  26. * Creates an Identifier with as value a random UUID and scheme "UUID"
  27. */
  28. public Identifier() {
  29. this(Scheme.UUID, UUID.randomUUID().toString());
  30. }
  31. public Identifier(String scheme, String value) {
  32. this.scheme = scheme;
  33. this.value = value;
  34. }
  35. /**
  36. * The first identifier for which the bookId is true is made the
  37. * bookId identifier.
  38. *
  39. * If no identifier has bookId == true then the first bookId identifier
  40. * is written as the primary.
  41. *
  42. * @param identifiers i
  43. * @return The first identifier for which the bookId is true is made
  44. * the bookId identifier.
  45. */
  46. public static Identifier getBookIdIdentifier(List<Identifier> identifiers) {
  47. if (identifiers == null || identifiers.isEmpty()) {
  48. return null;
  49. }
  50. Identifier result = null;
  51. for (Identifier identifier : identifiers) {
  52. if (identifier.isBookId()) {
  53. result = identifier;
  54. break;
  55. }
  56. }
  57. if (result == null) {
  58. result = identifiers.get(0);
  59. }
  60. return result;
  61. }
  62. public String getScheme() {
  63. return scheme;
  64. }
  65. public void setScheme(String scheme) {
  66. this.scheme = scheme;
  67. }
  68. public String getValue() {
  69. return value;
  70. }
  71. public void setValue(String value) {
  72. this.value = value;
  73. }
  74. public void setBookId(boolean bookId) {
  75. this.bookId = bookId;
  76. }
  77. /**
  78. * This bookId property allows the book creator to add multiple ids and
  79. * tell the epubwriter which one to write out as the bookId.
  80. *
  81. * The Dublin Core metadata spec allows multiple identifiers for a Book.
  82. * The epub spec requires exactly one identifier to be marked as the book id.
  83. *
  84. * @return whether this is the unique book id.
  85. */
  86. public boolean isBookId() {
  87. return bookId;
  88. }
  89. public int hashCode() {
  90. return StringUtil.defaultIfNull(scheme).hashCode() ^ StringUtil
  91. .defaultIfNull(value).hashCode();
  92. }
  93. public boolean equals(Object otherIdentifier) {
  94. if (!(otherIdentifier instanceof Identifier)) {
  95. return false;
  96. }
  97. return StringUtil.equals(scheme, ((Identifier) otherIdentifier).scheme)
  98. && StringUtil.equals(value, ((Identifier) otherIdentifier).value);
  99. }
  100. @SuppressWarnings("NullableProblems")
  101. @Override
  102. public String toString() {
  103. if (StringUtil.isBlank(scheme)) {
  104. return "" + value;
  105. }
  106. return "" + scheme + ":" + value;
  107. }
  108. }