Guest User

Untitled

a guest
Dec 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. // CHALLENGE (work in pairs):
  2. // Improve code snippets listed below –
  3. //
  4.  
  5.  
  6. // 1 - represent Xml class in the system
  7. class XmlMarshaller {
  8. JaxbMarshaller jaxbMarshaller = new JaxbMarshaller(Invoice.class);
  9.  
  10. byte[] marshallToXml(Invoice invoice) {
  11. try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
  12. return jaxbMarshaller.marshallObject(invoice, outStream).toByteArray();
  13. }
  14. }
  15. }
  16.  
  17.  
  18. class Xml<T> {
  19.  
  20.  
  21. byte[] bytes(T entity) {
  22. JaxbMarshaller jaxb = new JaxbMarshaller(entity.getClass());
  23. try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
  24. return jaxbMarshaller.marshallObject(invoice, outStream).toByteArray();
  25. }
  26. }
  27. }
  28.  
  29.  
  30.  
  31. // 2 - refactor to iliminate all null checks and branching
  32. class AmazonS3File {
  33.  
  34. private final String path;
  35. private Optional<Metadata> metadata = Optioal.EMPTY;
  36.  
  37. void attach(Metadata metadata) {
  38. this.metadata = Optional.of(metadata);
  39. }
  40.  
  41. Optional<Metadata> metadata() {…}
  42. }
  43.  
  44. class AmazonS3Bucket {
  45. void upload(AmazonS3File file) {
  46. … upload ...
  47. file.metadata.ifPresent(uploadMetadata(file.getMetadata())); //this::uploadMetadata
  48. }
  49. }
  50.  
  51.  
  52.  
  53. // 3 - refactor to conform to command-query separation (remember principle – "do or die")
  54. class Registration {
  55.  
  56. private final RegistrationForm form;
  57.  
  58. Optional<ValidationErrors> complete() {
  59. if (form.isValid()) {
  60. … complete registration ...
  61. return Optional.empty();
  62. } else {
  63. return form.validationErrors();
  64. }
  65. }
  66. }
  67.  
  68.  
  69.  
  70. // 4 - does the method violate the principle of least astonishment? is there something suspicious with the design? why?
  71. // Ban ban(User user) {
  72. // Ban ban = new Ban(user.ipAddress(), Period.ofDays(3));
  73. // user.banned(); // sets "banned" boolean in User to "true"
  74. // return ban;
  75. // }
  76.  
  77. User u = new User;
  78. u.ban(Period.ofDays(3)); // returns void
  79.  
  80. // 5 – any ideas how to improve this code?
  81. void sort() {
  82. Collection<User> users = ...
  83. users.sort((one, another) -> Long.compare(one.getId(), another.getId()));
  84. }
  85.  
  86. Comparator.comparing(User::getId)!!!!!!!!!!!!!!!!!!!
  87.  
  88. // 6 – every raw() invocation leads to remote call (via Vault). Please fix
  89. class SecurePassword {
  90.  
  91. private final Vault vault;
  92.  
  93. private SecurePassword(Vault vault) {
  94. this.vault = vault;
  95. }
  96.  
  97. public String raw() {
  98. return vault.verySecurePassword();
  99. }
  100.  
  101. }
  102.  
  103.  
  104. class SecurePassword {
  105.  
  106. private final Supplier<String> rawPAssword
  107.  
  108. private final Vault vault;
  109.  
  110. private SecurePassword(Vault vault) {
  111. //guava
  112. rawPassword = Suppliers.memoize(vault::verySecurePassword)
  113. this.vault = vault;
  114. }
  115.  
  116. public String raw() {
  117. return rawPAssword.get();
  118. }
  119.  
  120. }
  121.  
  122.  
  123. // 7 - implement "fullName" method, so that it returned "firstName lastName" if nickname is missing
  124. // or "firstName <nickname> lastName" if nickname is present.
  125. // for example – "Robert Martin" or "Robert <Uncle Bob> Martin"
  126. // don't optimize prematurely (e.g. prefer simple String concatenation over StringBuilder).
  127. class User {
  128. private final Optional<String> nickName;
  129. private final String firstName;
  130. private final String lastName;
  131.  
  132. String fullName() {
  133. StringFormatter("%s %s%s", firstName, nickName.orElse(" "), lastName)
  134. }
  135. }
Add Comment
Please, Sign In to add comment