All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jchanghong.core.text.replacer.ReplacerChain Maven / Gradle / Ivy

The newest version!
package com.jchanghong.core.text.replacer;

import com.jchanghong.core.lang.Chain;
import com.jchanghong.core.text.StrBuilder;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 字符串替换链,用于组合多个字符串替换逻辑
 * 
 * @author looly
 * @since 4.1.5
 */
public class ReplacerChain extends StrReplacer implements Chain {
	private static final long serialVersionUID = 1L;

	private final List replacers = new LinkedList<>();

	/**
	 * 构造
	 * 
	 * @param strReplacers 字符串替换器
	 */
	public ReplacerChain(StrReplacer... strReplacers) {
		for (StrReplacer strReplacer : strReplacers) {
			addChain(strReplacer);
		}
	}

	@SuppressWarnings("NullableProblems")
	@Override
	public Iterator iterator() {
		return replacers.iterator();
	}

	@Override
	public ReplacerChain addChain(StrReplacer element) {
		replacers.add(element);
		return this;
	}

	@Override
	protected int replace(CharSequence str, int pos, StrBuilder out) {
		int consumed = 0;
		for (StrReplacer strReplacer : replacers) {
			consumed = strReplacer.replace(str, pos, out);
			if (0 != consumed) {
				return consumed;
			}
		}
		return consumed;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy