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

LGPL

Declaration

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

Method Source Code

//package com.java2s;
/*/*from  w  w w .  j  a  v  a 2  s.c  o m*/
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.util.Iterator;

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());
        }
        return result;
    }

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

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

Related

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