Skip to content

Commit 918da39

Browse files
Refactor DocumentSource to enforce parameter count for polygon coordinates; update documentation for clarity on expected format and return values.
1 parent da27f07 commit 918da39

1 file changed

Lines changed: 17 additions & 26 deletions

File tree

sdk/contentunderstanding/azure-ai-contentunderstanding/customization/src/main/java/ContentUnderstandingCustomizations.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,18 +1489,18 @@ private void addSourcesMethod(LibraryCustomization customization, Logger logger)
14891489
+ "import java.util.List;\n"
14901490
+ "import java.util.Objects;\n\n"
14911491
+ "/**\n"
1492-
+ " * Represents a parsed document grounding source in the format {@code D(page,x1,y1,...,xN,yN)} or {@code D(page)}.\n"
1492+
+ " * Represents a parsed document grounding source in the format {@code D(page,x1,y1,x2,y2,x3,y3,x4,y4)}.\n"
14931493
+ " *\n"
1494-
+ " * <p>The page number is 1-based. The polygon defines a region with three or more points\n"
1495-
+ " * in the document's coordinate space. When only a page number is provided (no coordinates),\n"
1496-
+ " * {@link #getPolygon()} and {@link #getBoundingBox()} return {@code null}.</p>\n"
1494+
+ " * <p>The page number is 1-based. The polygon is a quadrilateral defined by four points\n"
1495+
+ " * with coordinates in the document's coordinate space.</p>\n"
14971496
+ " *\n"
14981497
+ " * @see ContentSource\n"
14991498
+ " */\n"
15001499
+ "@Immutable\n"
15011500
+ "public final class DocumentSource extends ContentSource {\n"
15021501
+ " private static final ClientLogger LOGGER = new ClientLogger(DocumentSource.class);\n"
1503-
+ " private static final String PREFIX = \"D(\";\n\n"
1502+
+ " private static final String PREFIX = \"D(\";\n"
1503+
+ " private static final int EXPECTED_PARAM_COUNT = 9;\n\n"
15041504
+ " private final int pageNumber;\n"
15051505
+ " private final List<PointF> polygon;\n"
15061506
+ " private final RectangleF boundingBox;\n\n"
@@ -1512,6 +1512,11 @@ private void addSourcesMethod(LibraryCustomization customization, Logger logger)
15121512
+ " }\n"
15131513
+ " String inner = source.substring(PREFIX.length(), source.length() - 1);\n"
15141514
+ " String[] parts = inner.split(\",\");\n"
1515+
+ " if (parts.length != EXPECTED_PARAM_COUNT) {\n"
1516+
+ " throw LOGGER.logExceptionAsError(\n"
1517+
+ " new IllegalArgumentException(\"Document source expected \" + EXPECTED_PARAM_COUNT\n"
1518+
+ " + \" parameters (page + 8 coordinates), got \" + parts.length + \": '\" + source + \"'.\"));\n"
1519+
+ " }\n"
15151520
+ " try {\n"
15161521
+ " this.pageNumber = Integer.parseInt(parts[0].trim());\n"
15171522
+ " } catch (NumberFormatException e) {\n"
@@ -1522,23 +1527,10 @@ private void addSourcesMethod(LibraryCustomization customization, Logger logger)
15221527
+ " throw LOGGER.logExceptionAsError(\n"
15231528
+ " new IllegalArgumentException(\"Page number must be >= 1, got \" + this.pageNumber + \".\"));\n"
15241529
+ " }\n"
1525-
+ " if (parts.length == 1) {\n"
1526-
+ " // Page-only: D(page)\n"
1527-
+ " this.polygon = null;\n"
1528-
+ " this.boundingBox = null;\n"
1529-
+ " return;\n"
1530-
+ " }\n"
1531-
+ " int coordCount = parts.length - 1;\n"
1532-
+ " if (coordCount < 6 || coordCount % 2 != 0) {\n"
1533-
+ " throw LOGGER.logExceptionAsError(\n"
1534-
+ " new IllegalArgumentException(\"Document source expected page-only (1 param) or page + at least 3 coordinate pairs (7+ params), got \"\n"
1535-
+ " + parts.length + \": '\" + source + \"'.\"));\n"
1536-
+ " }\n"
1537-
+ " int pointCount = coordCount / 2;\n"
1538-
+ " List<PointF> points = new ArrayList<>(pointCount);\n"
1530+
+ " List<PointF> points = new ArrayList<>(4);\n"
15391531
+ " float minX = Float.MAX_VALUE, minY = Float.MAX_VALUE;\n"
15401532
+ " float maxX = -Float.MAX_VALUE, maxY = -Float.MAX_VALUE;\n"
1541-
+ " for (int i = 0; i < pointCount; i++) {\n"
1533+
+ " for (int i = 0; i < 4; i++) {\n"
15421534
+ " int xIndex = 1 + (i * 2);\n"
15431535
+ " int yIndex = 2 + (i * 2);\n"
15441536
+ " float x, y;\n"
@@ -1572,27 +1564,26 @@ private void addSourcesMethod(LibraryCustomization customization, Logger logger)
15721564
+ " return pageNumber;\n"
15731565
+ " }\n\n"
15741566
+ " /**\n"
1575-
+ " * Gets the polygon coordinates defining the region, or {@code null} when only a page number is available.\n"
1567+
+ " * Gets the polygon coordinates as four points defining a quadrilateral region.\n"
15761568
+ " *\n"
1577-
+ " * @return An unmodifiable list of {@link PointF} values, or {@code null} for page-only sources.\n"
1569+
+ " * @return An unmodifiable list of four {@link PointF} values.\n"
15781570
+ " */\n"
15791571
+ " public List<PointF> getPolygon() {\n"
15801572
+ " return polygon;\n"
15811573
+ " }\n\n"
15821574
+ " /**\n"
1583-
+ " * Gets the axis-aligned bounding rectangle computed from the polygon coordinates,\n"
1584-
+ " * or {@code null} when only a page number is available.\n"
1575+
+ " * Gets the axis-aligned bounding rectangle computed from the polygon coordinates.\n"
15851576
+ " * Useful for drawing highlight rectangles over extracted fields.\n"
15861577
+ " *\n"
1587-
+ " * @return The bounding box, or {@code null} for page-only sources.\n"
1578+
+ " * @return The bounding box.\n"
15881579
+ " */\n"
15891580
+ " public RectangleF getBoundingBox() {\n"
15901581
+ " return boundingBox;\n"
15911582
+ " }\n\n"
15921583
+ " /**\n"
15931584
+ " * Parses a single document source segment.\n"
15941585
+ " *\n"
1595-
+ " * @param source The source string in the format {@code D(page)} or {@code D(page,x1,y1,...,xN,yN)}.\n"
1586+
+ " * @param source The source string in the format {@code D(page,x1,y1,...,x4,y4)}.\n"
15961587
+ " * @return A new {@link DocumentSource}.\n"
15971588
+ " * @throws NullPointerException if {@code source} is null.\n"
15981589
+ " * @throws IllegalArgumentException if the source string is not in the expected format.\n"

0 commit comments

Comments
 (0)