Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*-
*
* Copyright 2018, 2020 The Jackson Laboratory Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Matthew Gerring
*/
package org.geneweaver.io.reader;
import java.io.Closeable;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.beanutils.BeanMap;
import org.geneweaver.domain.Entity;
// TODO: Auto-generated Javadoc
/**
* Class for readers of lines to types.
*
* This class is a bit hard to understand because it is a Stream. The slight complexity
* here is worth it to enable a Stream to be used with any file. Parallel streams might
* be faster for processing large gene files.
*
* @author Matthew Gerring
* @param The type of thing this reader will read.
*/
public abstract class LineIteratorReader extends AbstractStreamReader implements Spliterator {
/** The Constant build. */
private static final long build = generateBuildNumber(); // Once per vm execution
/**
* The scanner for all the file(s) which we will parse.
*/
protected Iterator iterator;
/** The count. */
protected volatile int count;
/**
* The type when winding to stop for.
* This is used to get whole gene increments and make multi-threading work in the tests.
*/
private String windStopType;
/**
* It is possible to reject a line after it has been read.
* The rejected line is then returned from the next nextLine() call.
*/
private String rejectedLine;
/**
* delimiter used to split strings. By default any whitespace.
*/
private String delimiter = System.getProperty("org.geneweaver.io.delimiter", "\\s+");
/**
* Comment character
*/
private String comment = "#";
/**
* Header lines, if any
*/
protected List header;
/**
* Instantiates a new abstract reader.
*
* @param species the species
* @param file the file
* @throws IOException Signals that an I/O exception has occurred.
*/
protected void setup(ReaderRequest request) throws ReaderException {
if (request.getDelimiter()!=null) {
setDelimiter(request.getDelimiter());
}
try {
super.init(request);
if (request.isNoInputStream()) {
iterator = null;
} else {
// Iterate the file with a stream
this.iterator = StreamUtil.createStream(request);
}
this.count = 0;
} catch (IOException ne) {
throw new ReaderException(ne);
}
}
/**
* Testing only.
*/
protected LineIteratorReader() {
// TODO Auto-generated constructor stub
}
/**
* This stream must not be used in parallel stream programming.
* Use forkJoinStream() instead.
*
* @return the stream
*/
public Stream stream() {
header = null;
if (isEmpty() && isDataSource()) {
try {
this.iterator = StreamUtil.createStream(request.getFile(), request.isCloseInputStream());
} catch (IOException e) {
throw new IllegalArgumentException("The scanner iterator cannot be recreated from "+request.getFile(), e);
}
}
// Will throw an exception if called when empty
return StreamSupport.stream(this, false);
}
/**
* Generate build number.
*
* @return the long
*/
private static long generateBuildNumber() {
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.ENGLISH));
return Long.parseLong(format);
}
/**
* Parse the line to type T.
*
* @param line the line
* @return the t
* @throws ReaderException the reader exception
*/
protected abstract T create(String line) throws ReaderException;
private String assignmentChar = " ";
/**
* The variants are encoded with delimiter space(' ') or '#'.
*
* @return the assignment char
*/
protected String getAssignmentChar() {
return assignmentChar;
}
protected String setAssignmentChar(String ac) {
String curChar = assignmentChar;
assignmentChar = ac;
return curChar;
}
/**
* Transfer.
*
* @param propName the prop name
* @param from the from
* @param attrName the attr name
* @param to the to
* @return previous value in to or null if name is not in from or was not previously set in to.
*/
protected Object transfer(String propName, Map from, String attrName, Map