本文整理汇总了Java中com.google.common.io.LineProcessor类的典型用法代码示例。如果您正苦于以下问题:Java LineProcessor类的具体用法?Java LineProcessor怎么用?Java LineProcessor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LineProcessor类属于com.google.common.io包,在下文中一共展示了LineProcessor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: withLineNumbers
import com.google.common.io.LineProcessor; //导入依赖的package包/类
private String withLineNumbers(String code) {
try {
return CharStreams.readLines(new StringReader(code), new LineProcessor<String>() {
private final StringBuilder lines = new StringBuilder();
private int lineNo = 1;
@Override
public boolean processLine(String line) throws IOException {
lines.append(Strings.padStart(String.valueOf(lineNo++), 3, ' ')).append(": ").append(line)
.append("\n");
return true;
}
@Override
public String getResult() {
return lines.toString();
}
});
} catch (IOException e) {
throw new IllegalStateException(e.getMessage());
}
}
示例2: findFirst10Errors_guava
import com.google.common.io.LineProcessor; //导入依赖的package包/类
private List<String> findFirst10Errors_guava(File file) throws IOException {
List<String> lines = Files.readLines(file, Charsets.UTF_8, new LineProcessor<List<String>>() {
List<String> accumulator = new ArrayList<String>();
@Override
public boolean processLine(String line) throws IOException {
if (line.startsWith(ERROR_PREFIX)) {
accumulator.add(line);
}
return accumulator.size() < 10;
}
@Override
public List<String> getResult() {
return accumulator;
}
});
return lines;
}
示例3: processLines
import com.google.common.io.LineProcessor; //导入依赖的package包/类
public void processLines( final Object stream, final Consumer<String> callback )
throws Exception
{
final CharSource source = toCharSource( stream );
source.readLines( new LineProcessor<Object>()
{
@Override
public boolean processLine( final String line )
throws IOException
{
callback.accept( line );
return true;
}
@Override
public Object getResult()
{
return null;
}
} );
}
示例4: load
import com.google.common.io.LineProcessor; //导入依赖的package包/类
private static List<String> load(final String path) throws IOException {
return Files.readLines(new File(path), Charsets.UTF_8, new LineProcessor<List<String>>() {
private final List<String> result = Lists.newArrayList();
private final StringBuilder stringBuilder = new StringBuilder();
@Override
public boolean processLine(final String line) throws IOException {
if (line.isEmpty()) {
if (this.stringBuilder.length() > 0)
this.stringBuilder.deleteCharAt(this.stringBuilder.length() - 1);
this.result.add(this.stringBuilder.toString());
this.stringBuilder.setLength(0);
}
else this.stringBuilder.append(line).append("\n");
return true;
}
@Override
public List<String> getResult() {
return this.result;
}
});
}
示例5: getTotalNumberOfLines
import com.google.common.io.LineProcessor; //导入依赖的package包/类
private BigDecimal getTotalNumberOfLines() {
try {
return new BigDecimal(Files.readLines(tempFile, Charset.defaultCharset(), new LineProcessor<Integer>() {
private int count;
@Override
public Integer getResult() {
return count;
}
@Override
public boolean processLine(final String line) {
count++;
return true;
}
}));
} catch (final IOException e) {
LOG.error("Error while reading temp file for upload.", e);
}
return new BigDecimal(0);
}
示例6: readRegularLines
import com.google.common.io.LineProcessor; //导入依赖的package包/类
/**
* Read file that has not any multi-line text
*
* @param file
* @param charset
* @return
* @throws IOException
*/
public List<String> readRegularLines(@NonNull final File file, @NonNull final Charset charset) throws IOException
{
final List<String> loadedDataLines = new ArrayList<>();
Files.readLines(file, charset, new LineProcessor<Void>()
{
@Override
public boolean processLine(final String line) throws IOException
{
loadedDataLines.add(line);
return true;
}
@Override
public Void getResult()
{
return null;
}
});
return loadedDataLines;
}
示例7: fromCharSource
import com.google.common.io.LineProcessor; //导入依赖的package包/类
public static List<Rewrite> fromCharSource(CharSource source) throws IOException {
return source.readLines(new LineProcessor<List<Rewrite>>() {
private List<Rewrite> refactorings = new ArrayList<>();
private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings();
@Override
public List<Rewrite> getResult() {
return Collections.unmodifiableList(refactorings);
}
@Override
public boolean processLine(String line) {
List<String> parts = SPLITTER.splitToList(line);
refactorings.add(Rewrite.of(parts.get(0), parts.get(1), Rewrite.Visit.Expressions));
return true;
}
});
}
示例8: numberOfLinesInFile
import com.google.common.io.LineProcessor; //导入依赖的package包/类
/**
* Return the number of lines in this file.
*
* @param f
* @return
* @throws IOException
*/
public static int numberOfLinesInFile(final File f) throws IOException {
return Files.readLines(f, Charset.defaultCharset(),
new LineProcessor<Integer>() {
int count = 0;
@Override
public Integer getResult() {
return count;
}
@Override
public boolean processLine(final String line) {
count++;
return true;
}
});
}
示例9: stringSetFrom
import com.google.common.io.LineProcessor; //导入依赖的package包/类
public static Set<String> stringSetFrom(final CharSource supplier) throws IOException {
final LineProcessor<Set<String>> callback = new LineProcessor<Set<String>>() {
private final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
@Override
public boolean processLine(final String s) {
builder.add(s.trim());
return true;
}
@Override
public Set<String> getResult() {
return builder.build();
}
};
supplier.readLines(callback);
return callback.getResult();
}
示例10: parse
import com.google.common.io.LineProcessor; //导入依赖的package包/类
public void parse() throws Exception {
Files.readLines(Paths.get(path).toFile(), Charset.defaultCharset(), new LineProcessor<String>() {
@Override public boolean processLine(String line) throws IOException {
HistogramResult result = JSON.parseObject(line, HistogramResult.class);
String histogramString = result.getHistogram();
System.out.println(histogramString);
ByteBuffer buffer = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(histogramString));
try {
Histogram histogram = Histogram.decodeFromCompressedByteBuffer(buffer, 0);
histogram.setStartTimeStamp(result.getStartTime());
histogram.setEndTimeStamp(result.getEndTime());
//TODO
} catch (DataFormatException e) {
e.printStackTrace();
}
return true;
}
@Override public String getResult() {
return null;
}
});
}
示例11: read
import com.google.common.io.LineProcessor; //导入依赖的package包/类
public List<InputRecord> read(CharSource source) throws IOException {
return source.readLines(new LineProcessor<List<InputRecord>>() {
private final List<InputRecord> recs = Lists.newArrayList();
@Override
public boolean processLine(String line) throws IOException {
if (isBlank(line)) {
return true;
}
if (isComment(line)) {
return true;
}
InputRecord maybe = reader.parse(line);
if (maybe != null) {
recs.add(maybe);
}
return true;
}
@Override
public List<InputRecord> getResult() {
return recs;
}
});
}
示例12: shouldGetResourceWithLineProcessorFromContext
import com.google.common.io.LineProcessor; //导入依赖的package包/类
@Test
public void shouldGetResourceWithLineProcessorFromContext() throws Exception {
// given
String classResource = "../base/Either.class";
// when
String classString = getResourceWith(Resources.class, classResource, new LineProcessor<String>() {
StringBuilder builder = new StringBuilder();
@Override
public boolean processLine(String line) throws IOException {
builder.append(line).append("\n");
return true;
}
@Override
public String getResult() {
return builder.toString();
}
});
// then
assertThat(classString, isNotEmptyString());
assertThat(classString, containsString("com/bluecatcode/common/base/Either"));
}
示例13: shouldWrapExceptionInGetResourceWithLineProcessorFromContext
import com.google.common.io.LineProcessor; //导入依赖的package包/类
@Test
public void shouldWrapExceptionInGetResourceWithLineProcessorFromContext() throws Exception {
// given
String classResource = "../base/Either.class";
// expect
exception.expect(IllegalStateException.class);
// when
getResourceWith(Resources.class, classResource, new LineProcessor<String>() {
@Override
public boolean processLine(String line) throws IOException {
throw new IOException("test");
}
@Override
public String getResult() {
return null;
}
});
}
示例14: parseExportPackage
import com.google.common.io.LineProcessor; //导入依赖的package包/类
protected static Collection<String> parseExportPackage(InputStream is) throws IOException {
LineProcessor<List<String>> processor = new LineProcessor<List<String>>() {
final List<String> result = new ArrayList<String>();
@Override
public boolean processLine(String line) throws IOException {
result.add(line.replace('.', '/'));
return true; // keep reading
}
@Override
public List<String> getResult() {
return result;
}
};
return CharStreams.readLines(new InputStreamReader(is, Charsets.UTF_8), processor);
}
示例15: mapResource
import com.google.common.io.LineProcessor; //导入依赖的package包/类
private static Map<String, String> mapResource(String name) throws IOException {
final Splitter onTab = Splitter.on("\t");
return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<Map<String, String>>() {
final Map<String, String> r = Maps.newHashMap();
@Override
public boolean processLine(String line) throws IOException {
Iterator<String> pieces = onTab.split(line).iterator();
String key = pieces.next();
r.put(key, pieces.next());
return true;
}
@Override
public Map<String, String> getResult() {
return r;
}
});
}