Example usage for com.google.common.io LineProcessor LineProcessor

List of usage examples for com.google.common.io LineProcessor LineProcessor

Introduction

In this page you can find the example usage for com.google.common.io LineProcessor LineProcessor.

Prototype

LineProcessor

Source Link

Usage

From source file:org.geogit.storage.bdbje.JEStagingDatabase.java

/**
 * Gets the specified conflict from the database.
 * //from w  ww.j  ava  2s. c  o  m
 * @param namespace the namespace of the conflict
 * @param path the conflict to retrieve
 * @return the conflict, or {@link Optional#absent()} if it was not found
 */
@Override
public Optional<Conflict> getConflict(@Nullable String namespace, final String path) {
    Optional<File> file = findOrCreateConflictsFile(namespace);
    if (!file.isPresent()) {
        return Optional.absent();
    }
    Conflict conflict = null;
    try {
        File conflictsFile = file.get();
        synchronized (conflictsFile.getCanonicalPath().intern()) {
            conflict = Files.readLines(conflictsFile, Charsets.UTF_8, new LineProcessor<Conflict>() {
                Conflict conflict = null;

                @Override
                public Conflict getResult() {
                    return conflict;
                }

                @Override
                public boolean processLine(String s) throws IOException {
                    Conflict c = Conflict.valueOf(s);
                    if (c.getPath().equals(path)) {
                        conflict = c;
                        return false;
                    } else {
                        return true;
                    }
                }
            });
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    return Optional.fromNullable(conflict);
}

From source file:org.jclouds.abiquo.environment.InfrastructureTestEnvironment.java

public static String readLicense() throws IOException {
    URL url = CloudTestEnvironment.class.getResource("/license/expired");
    return Resources.readLines(url, Charset.defaultCharset(), new LineProcessor<String>() {
        StringBuilder sb = new StringBuilder();

        @Override//from w  w w .  j  av a2  s.  c  om
        public String getResult() {
            return sb.toString();
        }

        @Override
        public boolean processLine(String line) throws IOException {
            if (!line.startsWith("#")) {
                sb.append(line);
            }
            return true;
        }
    });
}

From source file:net.simon04.guavavfs.VirtualFiles.java

/**
 * Reads all of the lines from a file. The lines do not include
 * line-termination characters, but do include other leading and
 * trailing whitespace./*from w w w .  j  a  v  a 2 s  .  c om*/
 * <p>
 * <p>This method returns a mutable {@code List}. For an
 * {@code ImmutableList}, use
 * {@code Files.asCharSource(file, charset).readLines()}.
 *
 * @param file    the file to read from
 * @param charset the charset used to decode the input stream; see {@link
 *                Charsets} for helpful predefined constants
 * @return a mutable {@link List} containing all the lines
 * @throws IOException if an I/O error occurs
 */
public static List<String> readLines(String file, Charset charset) throws IOException {
    // don't use asCharSource(file, charset).readLines() because that returns
    // an immutable list, which would change the behavior of this method
    return readLines(file, charset, new LineProcessor<List<String>>() {
        final List<String> result = Lists.newArrayList();

        @Override
        public boolean processLine(String line) {
            result.add(line);
            return true;
        }

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