Repository: deepjavalibrary/djl
Pull request #3007 (2024-02-27 21:06:45)

[api] Adds utility method to Model for accessing properties
## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
Attention: Patch coverage is `50.00000%` with `4 lines` in your changes are missing coverage. Please review.
> Project coverage is 72.21%. Comparing base [(`19e4302`)](https://app.codecov.io/gh/deepjavalibrary/djl/commit/19e4302241f57531621325aba8f96be24c5d6d92?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) to head [(`5226348`)](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).
> Report is 1 commits behind head on master.

| [Files](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | Patch % | Lines |
|---|---|---|
| [api/src/main/java/ai/djl/Model.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL01vZGVsLmphdmE=) | 50.00% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) |

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #3007      +/-   ##
============================================
- Coverage     72.29%   72.21%   -0.09%     
+ Complexity     7284     7282       -2     
============================================
  Files           722      722              
  Lines         32483    32491       +8     
  Branches       3388     3390       +2     
============================================
- Hits          23484    23462      -22     
- Misses         7384     7414      +30     
  Partials       1615     1615              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/deepjavalibrary/djl/pull/3007?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).


Patch:
diff --git a/api/src/main/java/ai/djl/Model.java b/api/src/main/java/ai/djl/Model.java
index 1903ca392..dab256894 100644
--- a/api/src/main/java/ai/djl/Model.java
+++ b/api/src/main/java/ai/djl/Model.java
@@ -233,6 +233,36 @@ public interface Model extends AutoCloseable {
      */
     void setProperty(String key, String value);
 
+    /**
+     * Returns the property of the model based on property name.
+     *
+     * @param key the name of the property
+     * @param defValue the default value if key not found
+     * @return the value of the property
+     */
+    default int intProperty(String key, int defValue) {
+        String value = getProperty(key);
+        if (value == null || value.isEmpty()) {
+            return defValue;
+        }
+        return Integer.parseInt(value);
+    }
+
+    /**
+     * Returns the property of the model based on property name.
+     *
+     * @param key the name of the property
+     * @param defValue the default value if key not found
+     * @return the value of the property
+     */
+    default long longProperty(String key, long defValue) {
+        String value = getProperty(key);
+        if (value == null || value.isEmpty()) {
+            return defValue;
+        }
+        return Integer.parseInt(value);
+    }
+
     /**
      * Gets the {@link NDManager} from the model.
      *


Test patch:
diff --git a/api/src/test/java/ai/djl/translate/TranslatorTest.java b/api/src/test/java/ai/djl/translate/TranslatorTest.java
index 5ce63faa2..1a636af77 100644
--- a/api/src/test/java/ai/djl/translate/TranslatorTest.java
+++ b/api/src/test/java/ai/djl/translate/TranslatorTest.java
@@ -83,6 +83,10 @@ public class TranslatorTest {
                 Predictor<Image[], Classifications[]> predictor = model.newPredictor()) {
             Classifications[] res = predictor.predict(inputs);
             Assert.assertEquals(res.length, 2);
+            int intValue = model.intProperty("something", -1);
+            Assert.assertEquals(intValue, -1);
+            long longValue = model.longProperty("something", -1L);
+            Assert.assertEquals(longValue, -1L);
         }
     }
 }