io.github.jcharm.convert.coder.PatternSimpledCoder Maven / Gradle / Ivy
/**
* Copyright (c) 2016,Daniel Wang ([email protected]) All rights reserved。
*/
package io.github.jcharm.convert.coder;
import java.util.regex.Pattern;
import io.github.jcharm.convert.Reader;
import io.github.jcharm.convert.SimpledCoder;
import io.github.jcharm.convert.Writer;
/**
* Pattern 的SimpledCoder实现。
*
* @param Reader输入的子类型
* @param Writer输出的子类型
*/
public class PatternSimpledCoder extends SimpledCoder {
/** The Constant instance. */
public static final PatternSimpledCoder instance = new PatternSimpledCoder();
@Override
public Pattern convertFrom(final R in) {
final String value = in.readString();
if (value == null) {
return null;
}
final int pos = value.indexOf(',');
return Pattern.compile(value.substring(pos + 1), Integer.parseInt(value.substring(0, pos)));
}
@Override
public void convertTo(final W out, final Pattern value) {
if (value == null) {
out.writeNull();
} else {
out.writeString(value.flags() + "," + value.pattern());
}
}
}