Advertisement
Guest User

Untitled

a guest
May 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package com.google.ara.app.manager.util;
  2.  
  3. import java.util.List;
  4.  
  5. public final class ModuleNameJoiner {
  6. private ModuleNameJoiner() { }
  7.  
  8. private static final String DEFAULT_SEPARATOR = ", ";
  9.  
  10. public static String join(List<String> moduleNames, String conjoiner) {
  11. return join(moduleNames, conjoiner, DEFAULT_SEPARATOR);
  12. }
  13.  
  14. public static String join(List<String> moduleNames, String conjoiner, String separator) {
  15. if (moduleNames == null) {
  16. return "";
  17. }
  18.  
  19. switch (moduleNames.size()) {
  20. case 0:
  21. return "";
  22.  
  23. case 1:
  24. return moduleNames.get(0);
  25.  
  26. case 2:
  27. return simpleJoin(moduleNames, separator);
  28.  
  29. default:
  30. return conjoin(
  31. conjoiner,
  32. simpleJoin(moduleNames.subList(0, moduleNames.size() - 1), separator),
  33. moduleNames.get(moduleNames.size() - 1)
  34. );
  35. }
  36. }
  37.  
  38. public static String simpleJoin(List<String> moduleNames, String separator) {
  39. if (moduleNames == null) {
  40. return "";
  41. }
  42.  
  43. boolean first = true;
  44. StringBuffer sb = new StringBuffer(moduleNames.get(0));
  45. for (String s : moduleNames) {
  46. if (first) {
  47. first = false;
  48. continue;
  49. }
  50. sb.append(separator).append(s);
  51. }
  52.  
  53. return sb.toString();
  54. }
  55.  
  56. private static String conjoin(String conjoiner, String prefix, String postfix) {
  57. return prefix + conjoiner + postfix;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement