本文整理匯總了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;
}
});
}