Java Code Examples for com.google.common.io.LineProcessor

The following are top voted examples for showing how to use com.google.common.io.LineProcessor. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: n4js   File: PositiveAnalyser.java   View source code 7 votes vote down vote up
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());
	}
}
 
Example 2
Project: jug-material   File: FileProcessingTest.java   View source code 7 votes vote down vote up
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;
}
 
Example 3
Project: purplejs   File: CoreLibHelper.java   View source code 7 votes vote down vote up
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;
        }
    } );
}
 
Example 4
Project: blockplus   File: PolyominosTest.java   View source code 7 votes vote down vote up
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;
            }
        });
    }
 
Example 5
Project: hawkbit   File: BulkUploadHandler.java   View source code 6 votes vote down vote up
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);
}
 
Example 6
Project: metasfresh   File: FileImportReader.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 7
Project: mclab-core   File: Rewrites.java   View source code 6 votes vote down vote up
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;
    }
  });
}
 
Example 8
Project: tassal   File: TextFileUtils.java   View source code 6 votes vote down vote up
/**
 * 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;
				}
			});
}
 
Example 9
Project: bue-common-open   File: StringUtils.java   View source code 6 votes vote down vote up
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();
}
 
Example 10
Project: excalibur   File: ParserReader.java   View source code 6 votes vote down vote up
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;
        }
    });
}
 
Example 11
Project: jg2p   File: InputReader.java   View source code 6 votes vote down vote up
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;
    }
  });
}
 
Example 12
Project: java-extended   File: ResourcesTest.java   View source code 6 votes vote down vote up
@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"));
}
 
Example 13
Project: java-extended   File: ResourcesTest.java   View source code 6 votes vote down vote up
@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;
        }
    });
}
 
Example 14
Project: codemining-utils   File: TextFileUtils.java   View source code 6 votes vote down vote up
/**
 * 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;
				}
			});
}
 
Example 15
Project: takari-lifecycle   File: DependencyClasspathEntry.java   View source code 6 votes vote down vote up
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);
}
 
Example 16
Project: log-synth   File: VinSampler.java   View source code 6 votes vote down vote up
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;
        }
    });
}
 
Example 17
Project: log-synth   File: VinSampler.java   View source code 6 votes vote down vote up
private static SetMultimap<String, String> multiMapResource(String name) throws IOException {
    final Splitter onTab = Splitter.on("\t");

    return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<SetMultimap<String, String>>() {
        final SetMultimap<String, String> r = HashMultimap.create();

        @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 SetMultimap<String, String> getResult() {
            return r;
        }
    });
}
 
Example 18
Project: levelup-java-examples   File: CountLinesTextFile.java   View source code 6 votes vote down vote up
@Test
public void count_lines_text_file_guava() throws IOException {

	long linesCounted = com.google.common.io.Files.readLines(
			Paths.get(fileLocation).toFile(), Charsets.UTF_8,
			new LineProcessor<Long>() {

				long numberOfLinesInTextFile = 0;

				@Override
				public boolean processLine(String line) throws IOException {

					numberOfLinesInTextFile++;
					return true;
				}

				@Override
				public Long getResult() {
					return numberOfLinesInTextFile;
				}
			});

	assertEquals(10, linesCounted);
}
 
Example 19
Project: BSPAT   File: QueryRefGenome.java   View source code 6 votes vote down vote up
private StringBuilder loadRefGenome(String refGenomePath, String chr) throws IOException {
	return CharStreams.readLines(new FileReader(String.format("%s/%s.fa", refGenomePath, chr)),
			new LineProcessor<StringBuilder>() {
				StringBuilder refChrBuilder = new StringBuilder(100000000);

				@Override
				public boolean processLine(String s) throws IOException {
					if (!s.startsWith(">")) {
						refChrBuilder.append(s);
					}
					return true;
				}

				@Override
				public StringBuilder getResult() {
					return refChrBuilder;
				}
			});
}
 
Example 20
Project: basking   File: WriteCacheFileTask.java   View source code 6 votes vote down vote up
/**
 * Returns a new line processor for the cache file. The processor generates map of song IDs to
 * file names.
 */
static LineProcessor<Map<Integer, String>> newCacheFileLineProcessor() {
    return new LineProcessor<Map<Integer, String>>() {

        @SuppressWarnings("CanBeFinal")
        Map<Integer, String> map = Maps.newHashMap();

        @Override
        public boolean processLine(String line) throws IOException {
            String[] split = line.split("\\|");
            map.put(Integer.valueOf(split[0]), split[1]);
            return true;
        }

        @Override
        public Map<Integer, String> getResult() {
            return map;
        }
    };
}
 
Example 21
Project: mclab   File: Refactorings.java   View source code 6 votes vote down vote up
public static <T extends Reader> List<Refactoring>
    fromInputSupplier(InputSupplier<T> supplier) throws IOException{
  return CharStreams.readLines(supplier,  new LineProcessor<List<Refactoring>>() {
    private List<Refactoring> refactorings = Lists.newArrayList();
    private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings();

    @Override
    public List<Refactoring> getResult() {
      return Collections.unmodifiableList(refactorings);
    }

    @Override
    public boolean processLine(String line) {
      Iterator<String> parts = SPLITTER.split(line).iterator();
      String from = parts.next();
      String to = parts.next();
      refactorings.add(Refactoring.of(from, to, Refactoring.Visit.Expressions));
      return true;
    }
  });
}
 
Example 22
Project: oma-riista-web   File: CodeStyleTest.java   View source code 5 votes vote down vote up
@Test
public void verifySourceFilesDoNotContainTabCharacters() throws IOException {
    final Iterator<File> iter = FileUtils.iterateFiles(new File("src"), new CodeStyleFileFilter(), TrueFileFilter.INSTANCE);

    final List<String> filesContainingTabs = new ArrayList<>();

    while (iter.hasNext()) {
        final File file = iter.next();
        Files.readLines(file, StandardCharsets.US_ASCII, new LineProcessor<Void>() {
            @Override
            public boolean processLine(@Nonnull final String line) throws IOException {
                boolean result = true;
                if (line.contains("\t")) {
                    filesContainingTabs.add(file.getCanonicalPath());
                    result = false;
                }
                return result;
            }

            @Override
            public Void getResult() {
                return null;
            }
        });
    }

    assertEmpty(filesContainingTabs, "The following files contain tab characters: ");
}
 
Example 23
Project: Alchemy   File: NonUpdate.java   View source code 5 votes vote down vote up
public static ImmutableList<String> getWhiteList() {
	File file = new File("nu-whitelist.txt");
	try {
		return Files.readLines(file, Charsets.UTF_8, new LineProcessor<ImmutableList<String>>() {
			
			List<String> result = Lists.newLinkedList();

			@Override
			public boolean processLine(String line) {
				if (!line.startsWith("#") && !line.isEmpty())
					result.add(line);
				return true;
			}

			@Override
			public ImmutableList<String> getResult() {
				return ImmutableList.copyOf(result);
			}
			
		});
	} catch (IOException e) {
		if (!file.isFile())
			try {
				if (file.createNewFile()) {
					String whitelist = Joiner.on('\n').join(DEFAULT_WHITE_LIST);
					logger.info("Create file: nu-whitelist.txt\n" + whitelist);
					Files.write(whitelist, file, Charsets.UTF_8);
					return DEFAULT_WHITE_LIST;
				}
			} catch (IOException ex) { ex.printStackTrace(); }
		return ImmutableList.of();
	}
}
 
Example 24
Project: react-java-goos   File: GooGoo.java   View source code 5 votes vote down vote up
/**
 * 读取模版文件并做变量替换
 */
private static List<String> renderTemplate(String templateFileName, final Map<String, String> params) throws MalformedURLException, IOException {
    final Pattern p = Pattern.compile("\\{(.*?)\\}");
    // 定义每行的处理逻辑
    LineProcessor<List<String>> processor = new LineProcessor<List<String>>() {

        private List<String> result = Lists.newArrayList();

        @Override
        public boolean processLine(String line) throws IOException {
            String tmp = line;
            Matcher m = p.matcher(line);
            while (m.find()) {
                String key = m.group(1);
                if (params.containsKey(key)) {
                    tmp = tmp.replaceAll("\\{" + key + "\\}", params.get(key));
                }
            }

            result.add(tmp);
            return true;
        }

        @Override
        public List<String> getResult() {
            return result;
        }
    };

    return Resources.readLines(Resources.getResource(templateFileName), Charsets.UTF_8, processor);
}
 
Example 25
Project: ch.vorburger.minecraft.osgi   File: FileNewsRepository.java   View source code 5 votes vote down vote up
private List<News> load() throws IOException {
    if (!file.exists()) {
        return new ArrayList<>();
    }
    return Files.readLines(file, CHARSET, new LineProcessor<List<News>>() {

        List<News> loadedNews = new ArrayList<>();

        @Override
        public boolean processLine(String line) throws IOException {
            try {
                line = line.trim();
                if (!line.isEmpty()) {
                    int indexOfSpace1 = line.indexOf(' ');
                    String createdAsString = line.substring(0, indexOfSpace1);
                    int indexOfSpace2 = line.indexOf(' ', indexOfSpace1 + 1);
                    String uuidAsString = line.substring(indexOfSpace1 + 1, indexOfSpace2);
                    String textAsString = line.substring(indexOfSpace2 + 1);

                    Instant createdOn = dateTimeFormatter.parse(createdAsString, Instant::from);
                    UUID uuid = UUID.fromString(uuidAsString);
                    User byUser = getUser(uuid);
                    Text message = textSerializer.deserialize(textAsString);

                    loadedNews.add(ImmutableNews.builder().createdOn(createdOn).author(byUser).message(message).build());
                }
                return true;
            } catch (StringIndexOutOfBoundsException | DateTimeParseException | TextParseException | UserNotFoundException e) {
                throw new IOException("Failed to read line of file " + file + ": " + line, e);
            }
        }

        @Override
        public List<News> getResult() {
            return loadedNews;
        }
    });
}
 
Example 26
Project: FastBuild   File: ReobfExceptor.java   View source code 5 votes vote down vote up
private Map<String, String> readCSVs() throws IOException {
    final Map<String, String> csvData = Maps.newHashMap();
    File[] csvs = new File[]
            {
                    fieldCSV == null ? null : fieldCSV,
                    methodCSV == null ? null : methodCSV
            };

    for (File f : csvs) {
        if (f == null) continue;

        Files.readLines(f, Charset.defaultCharset(), new LineProcessor<Object>() {
            @Override
            public boolean processLine(String line) throws IOException {
                String[] s = line.split(",");
                csvData.put(s[0], s[1]);
                return true;
            }

            @Override
            public Object getResult() {
                return null;
            }
        });
    }

    return csvData;
}
 
Example 27
Project: FastBuild   File: ReobfExceptor.java   View source code 5 votes vote down vote up
private Map<String, String> createClassMap(Map<String, String> markerMap, final List<String> interfaces) throws IOException {
    Map<String, String> excMap = Files.readLines(excConfig, Charset.defaultCharset(), new LineProcessor<Map<String, String>>() {
        Map<String, String> tmp = Maps.newHashMap();

        @Override
        public boolean processLine(String line) throws IOException {
            if (line.contains(".") ||
                    !line.contains("=") ||
                    line.startsWith("#")) return true;

            String[] s = line.split("=");
            if (!interfaces.contains(s[0])) tmp.put(s[0], s[1] + "_");

            return true;
        }

        @Override
        public Map<String, String> getResult() {
            return tmp;
        }
    });

    Map<String, String> map = Maps.newHashMap();
    for (Entry<String, String> e : excMap.entrySet()) {
        String renamed = markerMap.get(e.getValue());
        if (renamed != null) {
            map.put(e.getKey(), renamed);
        }
    }
    return map;
}
 
Example 28
Project: benayn   File: Sources.java   View source code 5 votes vote down vote up
/**
 * @see com.google.common.io.LineProcessor
 * @see com.google.common.io.Resources#getResource(String)
 * @see com.google.common.io.Resources#readLines(URL, Charset, LineProcessor)
 */
public static <T> T getResourceWith(String resourceName, LineProcessor<T> lineProcessor) {
    checkNotNull(lineProcessor);
    URL url = Resources.getResource(checkNotEmpty(resourceName));
    try {
        return Resources.readLines(url, Charsets.UTF_8, lineProcessor);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 29
Project: Review-It   File: Indexer.java   View source code 5 votes vote down vote up
private CharArraySet getStopWordSet() {
    File stopWordsFile = new File(Configuration.getInstance().getStopWord());
    try {
        return Files.readLines(stopWordsFile,
                Charsets.UTF_8,

                new LineProcessor<CharArraySet>() {
                    CharArraySet result = new CharArraySet(Configuration
                                                .getInstance()
                                                .getStopWordSize(), true);

                    @Override
                    public CharArraySet getResult() {
                        return result;
                    }

                    @Override
                    public boolean processLine(String line) {
                        if (Strings.isNotBlank(line)) {
                            result.add(line.trim());
                        }
                        return true;
                    }
                });
    } catch (IOException e) {
        throw new RuntimeException("Unable to find  Stopwords file: ", e);
    }
}
 
Example 30
Project: SrgLib   File: MappingsFormat.java   View source code 5 votes vote down vote up
default Mappings parse(Readable readable) throws IOException {
    LineReader lineReader = new LineReader(readable);
    LineProcessor<Mappings> lineProcessor = createLineProcessor();
    String line;
    while ((line = lineReader.readLine()) != null) {
        if (!lineProcessor.processLine(line)) {
            break;
        }
    }
    return lineProcessor.getResult();
}
 
Example 31
Project: bue-common-open   File: MultitableLoader.java   View source code 5 votes vote down vote up
private void loadToMultitable(final CharSource source,
    final ImmutableMultitable.Builder<R, C, V> ret) throws IOException {
  source.readLines(new LineProcessor<Void>() {
    @Override
    public boolean processLine(final String line) throws IOException {
      final List<String> fields = fieldSplitter().splitToList(line);
      if (fields.size() == 3) {
        final R rowKey = interpret(fields.get(0), rowInterpreter(), "row key", line);
        final C columnKey = interpret(fields.get(1), columnInterpreter(), "column key", line);

        if (valueListSplitter().isPresent()) {
          for (final String value : valueListSplitter().get().split(fields.get(2))) {
            ret.put(rowKey, columnKey, interpret(value, valueInterpreter(), "value", line));
          }
        } else {
          ret.put(rowKey, columnKey, interpret(fields.get(2), valueInterpreter(), "value", line));
        }
      } else {
        throw new IOException("Cannot parse lines as multitable entries:\n" + line);
      }
      // we never stop procesisng lines early
      return true;
    }

    @Override
    public Void getResult() {
      return null;
    }
  });
}
 
Example 32
Project: dohko   File: IOUtils2.java   View source code 5 votes vote down vote up
/**
 * Reads all of the lines from an {@link InputStream} object. The lines include the line-termination characters, and also include other leading
 * and trailing whitespace.
 * <p>
 * Does not close the {@code InputStream}.
 * 
 * @param is The stream to read from.
 * @throws IOException if an I/O error occurs.
 */
public static String readLines(InputStream is) throws IOException
{
    InputStreamReader isr = new InputStreamReader(is);
    try
    {
        return CharStreams.readLines(isr, new LineProcessor<String>()
        {
            final StringBuilder lines = new StringBuilder();

            @Override
            public boolean processLine(String line) throws IOException
            {
                lines.append(line).append(Strings2.NEW_LINE);
                return true;
            }

            @Override
            public String getResult()
            {
                return lines.toString();
            }
        });
    }
    finally
    {
        closeQuietly(isr);
    }
}
 
Example 33
Project: excalibur   File: AppTest.java   View source code 5 votes vote down vote up
public static void main(String[] args) throws IOException {

        AvscSchemaBuilder asb = new AvscSchemaBuilder(Metric.class);
        final DataFileWriter<Metric2> dataFileWriter = new DataFileWriter<Metric2>(new SpecificDatumWriter<Metric2>(asb.createSchema()));
        dataFileWriter.create(asb.createSchema(), new File("v1.avro"));
        Path p = Paths.get("v1.log");
        Files.readLines(p.toFile(), Charset.defaultCharset(), new LineProcessor<String>() {
            @Override
            public boolean processLine(String line) throws IOException {
                String[] vals = line.split(" ");
                Metric2 mc =
                        Metric2.builder().dataVersion(Integer.valueOf(vals[0])).salt(Integer.valueOf(vals[1])).applicationId(Long.valueOf(vals[2]))
                                .timeScope(Integer.valueOf(vals[3])).metricTypeId(Long.valueOf(vals[4])).metricId(Long.valueOf(vals[5])).time(Integer.valueOf(vals[6]))
                                .agentRunId(Long.valueOf(vals[7])).uuid(vals[8]).num1(Double.valueOf(vals[9])).num2(Double.valueOf(vals[10])).num3(Double.valueOf(vals[11]))
                                .num4(Double.valueOf(vals[12])).num5(Double.valueOf(vals[13])).num6(Double.valueOf(vals[14])).timestamp(Long.valueOf(vals[15])).build();
                dataFileWriter.append(mc);
                return true;
            }

            @Override
            public String getResult() {
                return null;
            }
        });
        dataFileWriter.flush();
        dataFileWriter.close();
    }
 
Example 34
Project: excalibur   File: BaseReader.java   View source code 5 votes vote down vote up
public void handle() throws IOException {
    Files.readLines(Paths.get(sourceDataFilePath).toFile(), Charset.defaultCharset(), new LineProcessor<String>() {
        @Override public boolean processLine(String line) throws IOException {
            service.addRecord("A", "a", System.currentTimeMillis(), Long.valueOf(line));
            return true;
        }

        @Override public String getResult() {
            return null;
        }
    });
    service.shuffle(service.acquireAllData(), resultDataFilePath);
}
 
Example 35
Project: excalibur   File: WindowHistogramReader.java   View source code 5 votes vote down vote up
public void handle() throws IOException {
    Files.readLines(Paths.get(sourceDataFilePath).toFile(), Charset.defaultCharset(), new LineProcessor<String>() {
        @Override public boolean processLine(String line) throws IOException {
            String[] splits = line.split(" ");
            service.addRecord("A", "a", Long.valueOf(splits[0]), Long.valueOf(splits[1]));
            return true;
        }

        @Override public String getResult() {
            return null;
        }
    });
    service.shuffle(service.acquireAllData(), resultDataFilePath);
}
 
Example 36
Project: jg2p   File: SeqInputReader.java   View source code 5 votes vote down vote up
public List<AlignGroup> readInput(CharSource source) throws IOException {
  return source.readLines(new LineProcessor<List<AlignGroup>>() {

    final List<AlignGroup> groups = Lists.newArrayList();
    String lastGroup = "";
    List<Alignment> aligns = Lists.newArrayList();

    @Override
    public boolean processLine(String line) throws IOException {
      List<String> fields = caretSplit.splitToList(line);
      if (!lastGroup.equals(fields.get(0))) {
        lastGroup = fields.get(0);
        if (!aligns.isEmpty()) {
          groups.add(new AlignGroup(aligns));
        }
        aligns.clear();
      }
      Double score = Double.parseDouble(fields.get(1));
      Word input = Word.fromSpaceSeparated(fields.get(2));
      Iterable<String> graphs = pipeSplit.split(fields.get(3));
      Iterable<String> phones = pipeSplit.split(fields.get(4));

      aligns.add(new Alignment(input, Zipper.up(graphs, phones), score));
      return true;
    }

    @Override
    public List<AlignGroup> getResult() {
      if (!aligns.isEmpty()) {
        groups.add(new AlignGroup(aligns));
      }
      return groups;
    }
  });
}
 
Example 37
Project: TopK   File: TopKTest.java   View source code 5 votes vote down vote up
@Test
public void testText() throws IOException {

    final int k = 200;

    final ReferenceTopK<String> ref = new ReferenceTopK<>(k);
    final DeterministicTopK<String> det = new DeterministicTopK<>(k);
    final Pattern pattern = Pattern.compile("[\\p{Punct}\\p{Space}]");

    Resources.readLines(Resources.getResource("alice.txt"), Charsets.UTF_8, new LineProcessor<Void>() {

        @Override
        public boolean processLine(String line) throws IOException {
            Scanner scanner = new Scanner(line).useDelimiter(pattern);
            while (scanner.hasNext()) {
                String item = scanner.next().toLowerCase();
                ref.add(item);
                det.add(item);
            }
            return true;
        }

        @Override
        public Void getResult() {
            return null;
        }
    });
    Iterable<String> a = det.get();
    Iterable<String> b = ref.get();

    System.out.format("Ref: %s%n", ref);
    System.out.format("Det: %s%n", det);

    List<String> detSet = ImmutableList.copyOf(Iterables.limit(det.get(), 20));
    List<String> refSet = ImmutableList.copyOf(Iterables.limit(ref.get(), 20));
    assertThat(detSet, equalTo(refSet));
}
 
Example 38
Project: java-extended   File: Resources.java   View source code 5 votes vote down vote up
/**
 * @param contextClass  the class the resource is relative to
 * @param resourceName  the resource name
 * @param lineProcessor the line processor to use
 * @param <T>           the type of object to load into
 * @return the loaded object of type <tt>T</tt>
 * @see com.google.common.io.LineProcessor
 * @see com.google.common.io.Resources#getResource(String)
 * @see com.google.common.io.Resources#readLines(java.net.URL, java.nio.charset.Charset, LineProcessor)
 */
public static <T> T getResourceWith(Class<?> contextClass, String resourceName, LineProcessor<T> lineProcessor) {
    checkArgument(contextClass != null, "Expected non-null contextClass");
    checkNotEmpty(resourceName);
    checkArgument(lineProcessor != null, "Expected non-null lineProcessor");

    URL url = getResource(contextClass, resourceName);
    try {
        return readLines(url, UTF_8, lineProcessor);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 39
Project: nicta-ner   File: IO.java   View source code 5 votes vote down vote up
private static <T> T readResource(final Class<?> origin, final String resource,
                                  final LineProcessor<T> processor) throws IOException {
    try { return readLines(getResource(origin, resource), UTF_8, processor); }
    catch (final IOException ioe) {
        throw new IOException("Error reading resource: '" + resource + "'", ioe);
    }
}
 
Example 40
Project: swmutsel   File: ArgumentsProcessor.java   View source code 5 votes vote down vote up
private static String[] mergeFileArguments(final String[] rawArguments) {

        List<String> arguments = Lists.newArrayList();
        
        for (String arg : rawArguments) {
            // If we've been given an input file
            if (arg.startsWith("@")) {
                String filename = arg.substring(1);
                try {
                    List<String> nonCommentLines = Files.readLines(new File(filename), Charset.defaultCharset(), new LineProcessor<List<String>>() {
                        List<String> lines = Lists.newArrayList();

                        @Override
                        public boolean processLine(String line) throws IOException {
                            // This is the end of the file
                            if (line.startsWith("//")) return false;
                            // Ignore comments and empty lines, ignore everything after a comment
                            if (!line.startsWith("#") && line.length() > 0) lines.add(line.split("#")[0]);
                            return true;
                        }

                        @Override
                        public List<String> getResult() {
                            return lines;
                        }
                    });

                    arguments.addAll(nonCommentLines);

                } catch (IOException e) {
                    CoreUtils.msg("ERROR: Could not read file '%s'\n", arg.substring(1));
                }

            } else {
                arguments.add(arg);
            }
        }

        return arguments.toArray(new String[arguments.size()]);
    }