Class SequenceUtils

java.lang.Object
com.reve.utils.sequence.SequenceUtils

public class SequenceUtils extends Object
Sodrul Amin Shaon ** Md. Nafiul Alam Fuji ** Reve Systems ** 2022 Lets say we have sequence starting from 1 and highest valid sequence is 10 , then the sequence will be 1, 2, ... 7, 8, 9, 10, 1 ... and so on. We have to implement simple functions so that we can easily get distance, latestSequence and others from simple input of sequence numbers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    initializing maxValidSequence, actually the valid sequence range will be : from 1 to maxValidSequence
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compareMax(int a, int b)
     
    static int
    compareMin(int a, int b)
     
    static int
    distance(int source, int destination)
    Determines if it has the shortest path to go from source to destination or from destination to source.
    static int
    distance2(int source, int destination)
    Deprecated.
    static boolean
    isSequenceInvalid(int sequenceNumber)
    Check if the given sequence number is invalid.
    static int
    latestSequence(int a, int b)
     
    static int
    nextSequence(int currentSequence)
    Calculates the possible next sequence of given current sequence
    static int
    previousSequence(int currentSequence)
    Calculates the possible previous sequence of given current sequence

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_VALID_SEQUENCE

      public static final int MAX_VALID_SEQUENCE
      initializing maxValidSequence, actually the valid sequence range will be : from 1 to maxValidSequence
      See Also:
    • MIN_VALID_SEQUENCE

      public static final int MIN_VALID_SEQUENCE
      See Also:
  • Constructor Details

    • SequenceUtils

      public SequenceUtils()
  • Method Details

    • isSequenceInvalid

      public static boolean isSequenceInvalid(int sequenceNumber)
      Check if the given sequence number is invalid.
      Parameters:
      sequenceNumber - sequence number to be checked for validation.
      Returns:
      true, if sequence number is invalid, otherwise, return false.
    • distance2

      @Deprecated public static int distance2(int source, int destination)
      Deprecated.
      Parameters:
      source - is previously got sequence
      destination - is recently got sequence
      Returns:
      the distance between source and destination considering maxValidSequence. If destination is latest / source is old (considering sending side's creation time) then return positive distance and if destination is old / source is latest (considering sending side's creation time) then return negative distance.
    • distance

      public static int distance(int source, int destination)
      Determines if it has the shortest path to go from source to destination or from destination to source. If less steps required to go from source to destination then distance is positive. otherwise, negative. Let's say Max sequence is 100, source is 51 and destination is 0. distance will be 49. source is 50 and destination is 0. distance will be 50. source is 49 and destination is 0. distance will be -49.
      Parameters:
      source - is previously got sequence
      destination - is recently got sequence
      Returns:
      the distance value from source to destination. This value can be negative as well.
    • latestSequence

      public static int latestSequence(int a, int b)
      Parameters:
      a - is previously got sequence
      b - is recently got sequence
      Returns:
      the actual (considering sending side's creation time) the latest sequence comparing maxValidSequence as lately created sequence can reach early to the receiver through network
    • previousSequence

      public static int previousSequence(int currentSequence)
      Calculates the possible previous sequence of given current sequence
      Parameters:
      currentSequence - The value of current sequence, for which previous sequence need to be calculated.
      Returns:
      possible previous sequence of given #currentSequence
    • nextSequence

      public static int nextSequence(int currentSequence)
      Calculates the possible next sequence of given current sequence
      Parameters:
      currentSequence - recently got sequence
      Returns:
      valid next sequence of currentSequence
    • compareMin

      public static int compareMin(int a, int b)
      Parameters:
      a - is 1st sequence
      b - is 2nd sequence
      Returns:
      -1 if a is less than b, 0 if both equal, 1 if a is greater than b
    • compareMax

      public static int compareMax(int a, int b)
      Parameters:
      a - is 1st sequence
      b - is 2nd sequence
      Returns:
      1 if a is less than b, 0 if both equal, -1 if a is greater than b