Package com.reve.utils.sequence
Class SequenceUtils
java.lang.Object
com.reve.utils.sequence.SequenceUtils
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
FieldsModifier and TypeFieldDescriptionstatic final intinitializing maxValidSequence, actually the valid sequence range will be : from 1 to maxValidSequencestatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcompareMax(int a, int b) static intcompareMin(int a, int b) static intdistance(int source, int destination) Determines if it has the shortest path to go from source to destination or from destination to source.static intdistance2(int source, int destination) Deprecated.static booleanisSequenceInvalid(int sequenceNumber) Check if the given sequence number is invalid.static intlatestSequence(int a, int b) static intnextSequence(int currentSequence) Calculates the possible next sequence of given current sequencestatic intpreviousSequence(int currentSequence) Calculates the possible previous sequence of given current sequence
-
Field Details
-
MAX_VALID_SEQUENCE
public static final int MAX_VALID_SEQUENCEinitializing 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.- Parameters:
source- is previously got sequencedestination- 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 sequencedestination- is recently got sequence- Returns:
- the distance value from
sourcetodestination. This value can be negative as well.
-
latestSequence
public static int latestSequence(int a, int b) - Parameters:
a- is previously got sequenceb- 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 sequenceb- 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 sequenceb- is 2nd sequence- Returns:
- 1 if a is less than b, 0 if both equal, -1 if a is greater than b
-