Java Iterator multiply(String string, Iterator placeholders, Iterator replacements)

Here you can find the source of multiply(String string, Iterator placeholders, Iterator replacements)

Description

multiply

License

Apache License

Declaration

public static String[] multiply(String string, Iterator placeholders, Iterator replacements) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static String[] multiply(String string, Iterator placeholders, Iterator replacements) {
        String[] result = new String[] { string };
        while (placeholders.hasNext()) {
            result = multiply(result, (String) placeholders.next(), (String[]) replacements.next());
        }//from  w w  w. j av a2  s . co m
        return result;
    }

    private static String[] multiply(String[] strings, String placeholder, String[] replacements) {
        String[] results = new String[replacements.length * strings.length];
        int n = 0;
        for (int i = 0; i < replacements.length; i++) {
            for (int j = 0; j < strings.length; j++) {
                results[n++] = replaceOnce(strings[j], placeholder, replacements[i]);
            }
        }
        return results;
    }

    public static String replaceOnce(String template, String placeholder, String replacement) {
        if (template == null) {
            return template; // returnign null!
        }
        int loc = template.indexOf(placeholder);
        if (loc < 0) {
            return template;
        } else {
            return new StringBuilder(template.substring(0, loc)).append(replacement)
                    .append(template.substring(loc + placeholder.length())).toString();
        }
    }

    public static String toString(Object[] array) {
        int len = array.length;
        if (len == 0)
            return "";
        StringBuilder buf = new StringBuilder(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(array[i]).append(", ");
        }
        return buf.append(array[len - 1]).toString();
    }
}

Related

  1. makeCollection(final Iterator i)
  2. max(final Iterator iter, final int max)
  3. mergeIterator( final Iterator iterator1, final Iterator iterator2)
  4. moveNext(Iterator iterator)
  5. multiIterator(Iterator... iterable)
  6. multiply(String string, Iterator placeholders, Iterator replacements)
  7. multiSetIterator(Iterator... iterable)
  8. nextPermutation(Iterator> it)
  9. printContainer(Iterator it, String sep)