/**
*
*/
package java.util;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.CharBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 彦舜
*
*/
//创建一个普通类,实现所需要的接口
public final class Scanners implements Iterator<String>, Closeable {
private CharBuffer buf;
private static final int BUFFER_SIZE = 1024;
private int position;
private Matcher matcher;
private Pattern delimPattern;
private Pattern hasNextPattern;
private int hsaNextPosition;
private String hasNextResult;
private Readable source;
private boolean sourceClosed = false;
private boolean needInput = false;
private boolean skipped = false;
private int savedScannerPosition = -1;
private Object typeCache = null;
private boolean matchValid = false;
private boolean closed = false;
private int radix = 10;
private int defaultRadix = 10;
private Locale locale = null;
private LRUCache<String,Pattern> patternCache = new LRUCache<String,Pattern>(7){
protected Pattern create(String s) {
return Pattern.compile(s);
}
protected boolean hasName(Pattern p, String s) {
return p.pattern().equals(s);
}
};
private IOException lastException;
private static Pattern WHITESPACE_PATTERN = Pattern.compile("//p{javaWhitespace}+");
private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
private static Pattern NON_ASCII_DIGIT = Pattern.compile("[//p{javaDigit}&&[^0-9]]");
private String groupSeparator = "//,";
private String decimalSeparator = "//.";
private String nanaString = "NaN";
private String infinityString = "Infinity";
private String positivePrefix = "";
private String negativePrefix = "//-";
private String positiveSuffix = "";
private String negativeSuffxi = "";
private static volatile Pattern boolPattern;
private static final String BOOLEAN_PATTERN = "true|false";
private static Pattern boolPattern() {
Pattern bp = boolPattern;
if(bp == null) {
boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,Pattern.CASE_INSENSITIVE);
return bp;
}
}
private Pattern integerPattern;
private String digits = "012345789abcdefghijklmopqrstuvwxyz";
private String non0Digit = "[//p{javaDigit}&&[^0]]";
private int SIMPLE_GROUP_INDEX = 5;
private String buildIntergerPatternString() {
}
//Scanners类构造器
private Scanners(Readable source, Pattern pattern) {
assert source != null:"source should not be null";
assert pattern != null : "pattern should not be null";
this.source = source;
delimPattern = pattern;
buf = CharBuffer.allocate(BUFFER_SIZE);
buf.limit(0);
matcher = delimPattern.matcher(buf);
matcher.useTransparentBounds(true);
matcher.useAnchoringBounds(false);
useLocale(Locale.getDefault(Locale.Category.FORMAT));
}
private void useLocale(Locale default1) {
// TODO Auto-generated method stub
}
public Scanners(Readable source) {
this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
}
public Scanners(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
public Scanners(InputStream source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)), WHITESPACE_PATTERN);
}
private static Readable makeReadable(InputStream requireNonNull, Object charset) {
// return new InputStreamReader(source, charset);
return null;
}
private static Object toCharset(String csn) {
// Object.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
}catch(IllegalCharsetNameException|UnsupportedCharsetException e) {
throw new IllegalArgumentException(e);
}
}
public Scanners(File source) throws FileNotFoundException {
this((Readable)(new FileInputStream(source).getChannel()));
}
public Scanners(File source, String charsetName) throws FileNotFoundException {
this(Objects.requireNonNull(source), toDecoder(charsetName));
}
public Scanners(File source, CharsetDecoder dec) throws FileNotFoundException {
this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
}
public Scanners(Path source) throws IOException {
this(Files.newInputStream(source));
}
public Scanners(Path source, String charsetName) throws IOException {
// this(Objects.requireNonNull(source), toCharset(charsetName));
}
public Scanners(Path source, Charset charset) throws IOException {
this(makeReadable(Files.newInputStream(source), charset));
}
public Scanners(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
public Scanners(ReadableByteChannel source) {
// this(makeReadable(Objects.requireNonNull(source, "source")), WHITESPACE_PATTERN);
}
public Scanners(ReadableByteChannel source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)));
}
private static Readable makeReadable(ReadableByteChannel readableByteChannel, CharsetDecoder dec) {
return null;
}
private static CharsetDecoder toDecoder(String charsetName) {
return null;
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public String next() {
// TODO Auto-generated method stub
return null;
}
}
彦舜原创,CSDN首发:基于正则表达式的Scanner类的构造器
最新推荐文章于 2022-03-03 18:03:09 发布