@Test public void testRead_02() throws InvalidPathException { String name = "Larus fuscus fuscus"; ScientificName sn = new ScientificName(); sn.setFullScientificName(name); SpecimenIdentification si = new SpecimenIdentification(); si.setScientificName(sn); Specimen specimen = new Specimen(); specimen.addIndentification(si); Path path = new Path("identifications.0.scientificName.fullScientificName"); PathValueReader pr = new PathValueReader(path); Object value = pr.read(specimen); assertEquals("01", name, value); }
public Object read(Object obj) throws InvalidPathException { Objects.requireNonNull(obj, "Object must not be null"); if (path.countElements() == 0) { return obj; } for (int i = 0; i < path.countElements(); i++) { obj = readField(path.getElement(i), obj); if (obj == null || i == path.countElements() - 1) { return obj; } if (obj instanceof Iterable) { ++i; try { int idx = Integer.parseInt(path.getElement(i)); Iterator<?> iterator = ((Iterable<?>) obj).iterator(); for (int j = 0; j <= idx; j++) { if (!iterator.hasNext()) { return null; } obj = iterator.next(); } if (obj == null || i == path.countElements() - 1) { return obj; } } catch (NumberFormatException e) { String fmt = "Missing array index after %s in path %s"; String msg = String.format(fmt, path.getElement(i), path); throw new InvalidPathException(msg); } } } return null; }
PathValueReader { public Object read(Object obj) throws InvalidPathException { Objects.requireNonNull(obj, "Object must not be null"); if (path.countElements() == 0) { return obj; } for (int i = 0; i < path.countElements(); i++) { obj = readField(path.getElement(i), obj); if (obj == null || i == path.countElements() - 1) { return obj; } if (obj instanceof Iterable) { ++i; try { int idx = Integer.parseInt(path.getElement(i)); Iterator<?> iterator = ((Iterable<?>) obj).iterator(); for (int j = 0; j <= idx; j++) { if (!iterator.hasNext()) { return null; } obj = iterator.next(); } if (obj == null || i == path.countElements() - 1) { return obj; } } catch (NumberFormatException e) { String fmt = "Missing array index after %s in path %s"; String msg = String.format(fmt, path.getElement(i), path); throw new InvalidPathException(msg); } } } return null; } }
PathValueReader { public Object read(Object obj) throws InvalidPathException { Objects.requireNonNull(obj, "Object must not be null"); if (path.countElements() == 0) { return obj; } for (int i = 0; i < path.countElements(); i++) { obj = readField(path.getElement(i), obj); if (obj == null || i == path.countElements() - 1) { return obj; } if (obj instanceof Iterable) { ++i; try { int idx = Integer.parseInt(path.getElement(i)); Iterator<?> iterator = ((Iterable<?>) obj).iterator(); for (int j = 0; j <= idx; j++) { if (!iterator.hasNext()) { return null; } obj = iterator.next(); } if (obj == null || i == path.countElements() - 1) { return obj; } } catch (NumberFormatException e) { String fmt = "Missing array index after %s in path %s"; String msg = String.format(fmt, path.getElement(i), path); throw new InvalidPathException(msg); } } } return null; } PathValueReader(String path);  PathValueReader(Path path); }
PathValueReader { public Object read(Object obj) throws InvalidPathException { Objects.requireNonNull(obj, "Object must not be null"); if (path.countElements() == 0) { return obj; } for (int i = 0; i < path.countElements(); i++) { obj = readField(path.getElement(i), obj); if (obj == null || i == path.countElements() - 1) { return obj; } if (obj instanceof Iterable) { ++i; try { int idx = Integer.parseInt(path.getElement(i)); Iterator<?> iterator = ((Iterable<?>) obj).iterator(); for (int j = 0; j <= idx; j++) { if (!iterator.hasNext()) { return null; } obj = iterator.next(); } if (obj == null || i == path.countElements() - 1) { return obj; } } catch (NumberFormatException e) { String fmt = "Missing array index after %s in path %s"; String msg = String.format(fmt, path.getElement(i), path); throw new InvalidPathException(msg); } } } return null; } PathValueReader(String path);  PathValueReader(Path path); Object read(Object obj); }
PathValueReader { public Object read(Object obj) throws InvalidPathException { Objects.requireNonNull(obj, "Object must not be null"); if (path.countElements() == 0) { return obj; } for (int i = 0; i < path.countElements(); i++) { obj = readField(path.getElement(i), obj); if (obj == null || i == path.countElements() - 1) { return obj; } if (obj instanceof Iterable) { ++i; try { int idx = Integer.parseInt(path.getElement(i)); Iterator<?> iterator = ((Iterable<?>) obj).iterator(); for (int j = 0; j <= idx; j++) { if (!iterator.hasNext()) { return null; } obj = iterator.next(); } if (obj == null || i == path.countElements() - 1) { return obj; } } catch (NumberFormatException e) { String fmt = "Missing array index after %s in path %s"; String msg = String.format(fmt, path.getElement(i), path); throw new InvalidPathException(msg); } } } return null; } PathValueReader(String path);  PathValueReader(Path path); Object read(Object obj);  }