/mongodb/src/main/java/com/oreilly/springdata/mongodb/core/Customer.java

https://github.com/mykel33/askmecode · Java · 125 lines · 46 code · 18 blank · 61 comment · 0 complexity · 566cdab1c9fd59807fe8db2b27fc133f MD5 · raw file

  1. /*
  2. * Copyright 2012 the original author or authors.
  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.oreilly.springdata.mongodb.core;
  17. import java.util.Collections;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import org.springframework.data.mongodb.core.index.Indexed;
  21. import org.springframework.data.mongodb.core.mapping.Document;
  22. import org.springframework.data.mongodb.core.mapping.Field;
  23. import org.springframework.util.Assert;
  24. /**
  25. * A customer.
  26. *
  27. * @author Oliver Gierke
  28. */
  29. @Document
  30. public class Customer extends AbstractDocument {
  31. private String firstname, lastname;
  32. @Field("email")
  33. @Indexed(unique = true)
  34. private EmailAddress emailAddress;
  35. private Set<Address> addresses = new HashSet<Address>();
  36. /**
  37. * Creates a new {@link Customer} from the given firstname and lastname.
  38. *
  39. * @param firstname must not be {@literal null} or empty.
  40. * @param lastname must not be {@literal null} or empty.
  41. */
  42. public Customer(String firstname, String lastname) {
  43. Assert.hasText(firstname);
  44. Assert.hasText(lastname);
  45. this.firstname = firstname;
  46. this.lastname = lastname;
  47. }
  48. protected Customer() {
  49. }
  50. /**
  51. * Adds the given {@link Address} to the {@link Customer}.
  52. *
  53. * @param address must not be {@literal null}.
  54. */
  55. public void add(Address address) {
  56. Assert.notNull(address);
  57. this.addresses.add(address);
  58. }
  59. /**
  60. * Returns the firstname of the {@link Customer}.
  61. *
  62. * @return
  63. */
  64. public String getFirstname() {
  65. return firstname;
  66. }
  67. /**
  68. * Returns the lastname of the {@link Customer}.
  69. *
  70. * @return
  71. */
  72. public String getLastname() {
  73. return lastname;
  74. }
  75. /**
  76. * Sets the lastname of the {@link Customer}.
  77. *
  78. * @param lastname
  79. */
  80. public void setLastname(String lastname) {
  81. this.lastname = lastname;
  82. }
  83. /**
  84. * Returns the {@link EmailAddress} of the {@link Customer}.
  85. *
  86. * @return
  87. */
  88. public EmailAddress getEmailAddress() {
  89. return emailAddress;
  90. }
  91. /**
  92. * Sets the {@link Customer}'s {@link EmailAddress}.
  93. *
  94. * @param emailAddress must not be {@literal null}.
  95. */
  96. public void setEmailAddress(EmailAddress emailAddress) {
  97. this.emailAddress = emailAddress;
  98. }
  99. /**
  100. * Return the {@link Customer}'s addresses.
  101. *
  102. * @return
  103. */
  104. public Set<Address> getAddresses() {
  105. return Collections.unmodifiableSet(addresses);
  106. }
  107. }