Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-980

DurationFormatUtils uses == for comparing objects

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Patch Needed
    • Component/s: lang.time.*
    • Labels:
      None

      Description

      As reported on the ML, Findbugs complains that == is being used to compare objects in the class DurationFormatUtils.

      These objects are the strings which define the various durations: "y", "M", "d" etc. These are final static objects (singletons) so the use of == should be OK but it is not good practice.

      One way to avoid the warnings would be to use an Enum for the singleton objects. For example:

      enum Duration { YEAR, MONTH, ... }
          static final ParseObject y = ParseObject.YEAR;
          static final ParseObject M = ParseObject.MONTH;
      ...
      

      Note: the package protected fields y, M etc are currently needed for the unit tests.

      The above change would then allow the format() method to use a switch statement which would likely be faster than the if chain it has to use now.

      Eliminating the warnings for == which are currently safe would make it obvious if == was used elsewhere in an unsafe way.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              sebb Sebb
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated: