Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. +---------+-------+
  2. | baf18c4 | Item1 |
  3. | c5e4623 | Item2 |
  4. | bf1932f | Item3 |
  5. +---------+-------+
  6.  
  7. +---------+-------+
  8. | baf18c4 | Item1 |
  9. | c5e4623 | Item2 |
  10. | baf18c4 | Item3 |
  11. | baf18c4 | Item4 |
  12. +---------+-------+
  13.  
  14. +---------+-------+
  15. | baf18c4 | Item1 |
  16. | c5e4623 | Item2 |
  17. | baf18c4 | Item3 |
  18. | bf1932f | Item4 |
  19. | c5e4623 | Item5 |
  20. +---------+-------+
  21.  
  22. package tmp;
  23.  
  24. import static org.junit.Assert.assertEquals;
  25.  
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.Collections;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Map.Entry;
  33. import java.util.stream.Collectors;
  34.  
  35. import org.junit.Test;
  36.  
  37. public class Snippet {
  38. private static String verifyItems(List<Item> items) {
  39. Map<String, List<Item>> map = new HashMap<>();
  40. for (Item item : items) {
  41. String key = item.getCode();
  42. List<Item> list;
  43. if (map.containsKey(key)) {
  44. list = map.get(key);
  45. }
  46. else {
  47. list = new ArrayList<>();
  48. map.put(key, list);
  49. }
  50. list.add(item);
  51. }
  52. boolean isFirst = true;
  53. StringBuilder sb = new StringBuilder();
  54. for (Entry<String, List<Item>> e : map.entrySet()) {
  55. if (e.getValue().size() > 1) {
  56. if (isFirst) {
  57. sb.append("Some of the items are sharing the same code: ");
  58. }
  59. else {
  60. sb.append(", ");
  61. }
  62. isFirst = false;
  63. sb.append("'" + e.getKey() + "' used by ");
  64. sb.append(e.getValue().stream().map(i -> i.getDescription())
  65. .collect(Collectors.joining(", ", "{", "}")));
  66. }
  67. }
  68. return sb.toString();
  69. }
  70.  
  71. @Test
  72. public void testEmpty() throws Exception {
  73. assertEquals("", verifyItems(Collections.emptyList()));
  74. }
  75.  
  76. @Test
  77. public void testOk() throws Exception {
  78. List<Item> items = Arrays.asList(
  79. new Item("baf18c4", "Item1"),
  80. new Item("c5e4623", "Item2"),
  81. new Item("bf1932f", "Item3"));
  82. assertEquals("", verifyItems(items));
  83. }
  84.  
  85. @Test
  86. public void testDuplicate() throws Exception {
  87. List<Item> items = Arrays.asList(
  88. new Item("baf18c4", "Item1"),
  89. new Item("c5e4623", "Item2"),
  90. new Item("baf18c4", "Item3"),
  91. new Item("baf18c4", "Item4"));
  92. assertEquals("Some of the items are sharing the same code: 'baf18c4' used by {Item1, Item3, Item4}", verifyItems(items));
  93. }
  94.  
  95. @Test
  96. public void testDuplicate2() throws Exception {
  97. List<Item> items = Arrays.asList(
  98. new Item("baf18c4", "Item1"),
  99. new Item("c5e4623", "Item2"),
  100. new Item("baf18c4", "Item3"),
  101. new Item("bf1932f", "Item4"),
  102. new Item("c5e4623", "Item5"));
  103. assertEquals("Some of the items are sharing the same code: 'baf18c4' used by {Item1, Item3}, 'c5e4623' used by {Item2, Item5}", verifyItems(items));
  104. }
  105.  
  106. public static class Item {
  107. private String code;
  108. private String description;
  109.  
  110. public Item(String code, String description) {
  111. super();
  112. this.code = code;
  113. this.description = description;
  114. }
  115.  
  116. public String getCode() {
  117. return code;
  118. }
  119.  
  120. public String getDescription() {
  121. return description;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement