diff --git a/lol.rb b/lol.rb index 34cbd51..7fafc78 100644 --- a/lol.rb +++ b/lol.rb @@ -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 diff --git a/sorting_algos.rb b/sorting_algos.rb index db5ba42..e9611e2 100644 --- a/sorting_algos.rb +++ b/sorting_algos.rb @@ -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};" - 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};" + {:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.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 \ No newline at end of file