Search code examples
javastatic-analysisliteralspmd

PMD : Avoid using Literals in Conditional Statements


I have following code. I am getting "Avoid using Literals in Conditional Statements." warning in PMD on line number 5.

List<Object> listObj = getData();
 if (listObj.isEmpty()) {
      throw new NoEntity("No entity found for given Device.");
 }
 if (listObj.size() > 1) {
      throw new MultiEntity(
          "Multiple entity record found for given Device.");
 }

I prefer not to put global static final int variable having value as 1 and use it in if condition. Any other solution for this ?


Solution

  • If you are using Apache Commons Lang, it's available in NumberUtils https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/math/NumberUtils.html#INTEGER_ONE