Ejemplo n.º 1
0
 public static Result<String> readFile2String(String path) {
   try {
     return Result.success(new String(Files.readAllBytes(Paths.get(path))));
   } catch (IOException e) {
     return Result.failure(String.format("IO error while reading file %s", path), e);
   } catch (Exception e) {
     return Result.failure(String.format("Unexpected error while reading file %s", path), e);
   }
 }
Ejemplo n.º 2
0
 private static Result<List<Element>> readDocument(String rootElementName, String stringDoc) {
   final SAXBuilder builder = new SAXBuilder();
   try {
     final Document document = builder.build(new StringReader(stringDoc));
     final Element rootElement = document.getRootElement();
     return Result.success(List.fromCollection(rootElement.getChildren(rootElementName)));
   } catch (IOException | JDOMException io) {
     return Result.failure(
         String.format(
             "Invalid root element name '%s' or XML data %s", rootElementName, stringDoc),
         io);
   } catch (Exception e) {
     return Result.failure(
         String.format("Unexpected error while reading XML data %s", stringDoc), e);
   }
 }
Ejemplo n.º 3
0
public class ListTest {

  @Test
  public void testFlattenResult() {
    List<Integer> list = List.list(1, 2, 3, 4, 5, 0, 6, 7, 8, 9);
    List<Double> list2 = List.flattenResult(list.map(inverse));
    assertEquals(
        "[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111, NIL]",
        list2.toString());
  }

  private Function<Integer, Result<Double>> inverse =
      x -> x == 0 ? Result.failure("divide by 0") : Result.success((double) 1 / x);
}