java.lang.Object
net.i2p.util.VersionComparator
All Implemented Interfaces:
Serializable, Comparator<String>

public class VersionComparatorextends Object implements Comparator<String>, Serializable
Compares versions. Characters other than [0-9.-_] are ignored. I2P only uses '.' but Sun Java uses '_' and plugins may use any of '.-_' Moved from TrustedUpdate.java.
See Also:
  • Constructor Details

    • VersionComparator

      public VersionComparator()
      A stateless comparator - compare(String, String) just delegates to comp() - so a bare instance behaves exactly like any other.
  • Method Details

    • compare

      public int compare(String l, String r)
      Specified by:
      compare in interface Comparator<String>
    • comp

      public static int comp(String l, String r)
      To avoid churning comparators.
      Parameters:
      l - the left-hand version string, the one being ordered
      r - the right-hand version string, the one being ordered against
      Returns:
      negative if l sorts older than r, positive if newer, 0 if equivalent
    • isSeparator

      private static boolean isSeparator(char c)
      True for a character that separates version parts.
      Parameters:
      c - the character to test
      Returns:
      true if it separates parts
    • isDigit

      private static boolean isDigit(char c)
      True for a decimal digit.
      Parameters:
      c - the character to test
      Returns:
      true if it is a digit
    • getDigit

      private static int getDigit(char c)
      The value of a decimal digit.
      Parameters:
      c - the character to convert
      Returns:
      its value
    • nextSeparator

      private static int nextSeparator(String s, int start)
      Index of the next separator character, or the end of the string.
      Parameters:
      s - string to process
      start - starting index in the string to process
      Returns:
      the index of the next separator character, or end of string
    • parseLong

      private static long parseLong(String s, int start, int end)
      Parses a long, ignoring any non-digit characters.
      Parameters:
      s - string to parse from
      start - index in the string to start
      end - index in the string to stop at
      Returns:
      the parsed value, or -1 if nothing was parsed or there was a problem.