Advertisement
Guest User

Untitled

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