Skip to content

Commit 8c191ad

Browse files
committed
Simplify qualifier comparison
Previously, the complexity of the qualifier comparison led to a single method that was quite hard to understand. This change simplifies that method by breaking out smaller understandable and well-named chunks with the same functionality. [#50968083]
1 parent c0267a4 commit 8c191ad

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

.yardopts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
--protected
2+
--no-private
23
-
34
LICENSE
45
NOTICE

lib/java_buildpack/version_resolver.rb

+23-13
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def self.matches(tokenized_candidate_version, tokenized_version)
5252
end
5353
end
5454

55-
# Internal class. DO NOT USE.
55+
# @private
5656
class TokenizedVersion < Array
5757
include Comparable
5858

59-
# Internal constant. DO NOT USE.
59+
# @private
6060
WILDCARD = '+'
6161

6262
def initialize(version, allow_wildcards = true)
@@ -70,25 +70,17 @@ def initialize(version, allow_wildcards = true)
7070
validate allow_wildcards
7171
end
7272

73-
# Internal method. DO NOT USE.
73+
# @private
7474
def <=>(another)
7575
comparison = self[0] <=> another[0]
7676
comparison = self[1] <=> another[1] if comparison == 0
7777
comparison = self[2] <=> another[2] if comparison == 0
78-
79-
self_qualifier = self[3].nil? ? '' : self[3]
80-
another_qualifier = another[3].nil? ? '' : another[3]
81-
i = 0
82-
until comparison != 0 || i == [self_qualifier.length, another_qualifier.length].min
83-
comparison = char_compare(self_qualifier[i], another_qualifier[i])
84-
i += 1
85-
end
86-
comparison = self_qualifier.length <=> another_qualifier.length if comparison == 0
78+
comparison = qualifier_compare(self[3].nil? ? '' : self[3], another[3].nil? ? '' : another[3]) if comparison == 0
8779

8880
comparison
8981
end
9082

91-
# Internal method. DO NOT USE.
83+
# @private
9284
def to_s
9385
@version
9486
end
@@ -132,6 +124,24 @@ def micro_and_qualifier(s)
132124
return micro, qualifier
133125
end
134126

127+
def minimum_qualifier_length(a, b)
128+
[a.length, b.length].min
129+
end
130+
131+
def qualifier_compare(a, b)
132+
comparison = 0
133+
134+
i = 0
135+
until comparison != 0 || i == minimum_qualifier_length(a, b)
136+
comparison = char_compare(a[i], b[i])
137+
i += 1
138+
end
139+
140+
comparison = a.length <=> b.length if comparison == 0
141+
142+
comparison
143+
end
144+
135145
def validate(allow_wildcards)
136146
post_wildcard = false
137147
self.each do |value|

0 commit comments

Comments
 (0)