Much prettier sorting

This commit is contained in:
Alexander Munch-Hansen 2019-10-17 11:28:05 +02:00
parent 646d4a297d
commit ea228d9285
2 changed files with 36 additions and 93 deletions

13
lol.rb
View File

@ -14,7 +14,7 @@ to_string_template = "0 1 x 2 meter"
sorting_one = Type.new("String", "Name")
sorting_two = Type.new("int", "Length")
sorting_three = Type.new("int", "Width")
sorting_filters = [sorting_one, sorting_two, sorting_three]
def construct_class(name_of_class, parameters, to_string_template, sorting_filters)
@ -25,16 +25,13 @@ def construct_class(name_of_class, parameters, to_string_template, sorting_filte
fields += "private #{x[:type]} #{x[:name]}; \n"
end
tmptmp = parameters.map { |x| "#{x[:type]} #{x[:name]}"}
constructor_parameters = tmptmp.join ","
constructor_parameters = parameters.map { |x| "#{x[:type]} #{x[:name]}"}.join ","
lel = parameters.map do |x|
getters = parameters.map do |x|
"public #{x[:type]} get#{x[:name]}(){
return #{x[:name]};
}"
end
getters = lel.join "\n"
end.join "\n"
lol = parameters.map do |x|
"this.#{x[:name]} = #{x[:name]};"
@ -195,7 +192,7 @@ end
file = File.open("#{name}.java", "w")
params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "Width"}]
file.puts(construct_class name, params, to_string_template)
file.puts(construct_class name, params, to_string_template, sorting_filters)
file.close

View File

@ -1,93 +1,39 @@
# TODO: Make variadic, such that we only need one method
def compare_to_two_vars(first, second)
def compare_to(sortings)
first, second, third = *sortings
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable}) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
def return_stat sorting
if sorting.type == "String"
{:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"}
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first_variable} != other.#{first.variable} {
#{return_string}
}
"
end
if second.type == "String"
return_string = "return this.#{second.variable}.compareTo(other.#{second.variable});"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
{:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.variable}"}
end
end
"
#{if_statement}
#{return_string}
"
end
def compare_to_three_vars(first, second, third)
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable})) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first.variable} != other.#{first.variable}) {
#{return_string}
}
"
end
if second.type == "String"
else_if_statement = "
else if (!this.#{second.variable}.equals(other.#{second.variable})) {
return this.#{second.variable}.compareTo(other.#{second.variable};
}
"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
end
else_if_statement = "
else if (this.#{second.variable} != other.#{second.variable}) {
#{return_string}
}
"
end
if third.type == "String"
return_string = "return this.#{third.variable}.compareTo(other.#{third.variable});"
else
if third.comparator == ">"
return_string = "return this.#{third.variable} - other.#{third.variable};"
else
return_string = "return other.#{third.variable} - this.#{third.variable};"
end
end
"
#{if_statement}
#{else_if_statement}
#{return_string}
"
def comp sorting
if sorting.type == "String"
{:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})",
:ret => (return_stat sorting)[:ret]}
else
{:if => "this.#{sorting.variable} != other.#{sorting.variable}",
:ret => (return_stat sorting)[:ret]}
end
end
if defined? third
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
} else if (#{(comp second)[:if]}) {
return #{(comp second)[:ret]};
}
return #{(comp third)[:ret]};"
else
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
}
return #{(comp second)[:ret]};
"
end
end