fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. List<StringListPair> mappings=Arrays.asList(
  12. new StringListPair("{url}",Arrays.asList("point1","point2")),
  13. new StringListPair("{live}",Arrays.asList("10","20","30")));
  14. String base="I have sample {url} with time to {live}";
  15. System.out.println("Recursive:");
  16. for(String combination:userecursive(mappings,base))
  17. System.out.println(combination);
  18. System.out.println("Direct:");
  19. for(String combination:usedirect(mappings,base))
  20. System.out.println(combination);
  21. }
  22.  
  23. static List<String> userecursive(List<StringListPair> mappings,String base){
  24. List<String> result=new ArrayList<>();
  25. recursive(result, mappings, base, 0);
  26. return result;
  27. }
  28. static void recursive(List<String> result,List<StringListPair> mappings,String sofar,int partindex) {
  29. if(partindex>=mappings.size()) {
  30. result.add(sofar);
  31. return;
  32. }
  33. StringListPair p=mappings.get(partindex);
  34. for(String item:p.l)
  35. recursive(result,mappings,sofar.replace(p.s,item),partindex+1);
  36. }
  37.  
  38. static List<String> usedirect(List<StringListPair> mappings,String base){
  39. int total=1;
  40. for(StringListPair p:mappings)
  41. total*=p.l.size();
  42. List<String> result=new ArrayList<>();
  43. for(int i=0;i<total;i++)
  44. result.add(direct(mappings,base,i));
  45. return result;
  46. }
  47. static String direct(List<StringListPair> mappings,String base,int index) {
  48. for(StringListPair p:mappings) {
  49. base=base.replace(p.s,p.l.get(index % p.l.size()));
  50. index/=p.l.size();
  51. }
  52. return base;
  53. }
  54.  
  55. static class StringListPair{
  56. final String s;
  57. final List<String> l;
  58. StringListPair(String s,List<String> l){
  59. this.s=s;
  60. this.l=l;
  61. }
  62. }
  63. }
Success #stdin #stdout 0.06s 32560KB
stdin
Standard input is empty
stdout
Recursive:
I have sample point1 with time to 10
I have sample point1 with time to 20
I have sample point1 with time to 30
I have sample point2 with time to 10
I have sample point2 with time to 20
I have sample point2 with time to 30
Direct:
I have sample point1 with time to 10
I have sample point2 with time to 10
I have sample point1 with time to 20
I have sample point2 with time to 20
I have sample point1 with time to 30
I have sample point2 with time to 30