More beautify

This commit is contained in:
Alexander Munch-Hansen 2019-10-17 11:48:54 +02:00
parent c81e3d7399
commit f5911f4cb6
3 changed files with 33 additions and 38 deletions

View File

@ -13,11 +13,8 @@ end
def find_best_with_extra_parameter(container_obj, based_on, compare, type, extra_based_on, extra_compare, extra_param, based_on_type) def find_best_with_extra_parameter(container_obj, based_on, compare, type, extra_based_on, extra_compare, extra_param, based_on_type)
if extra_compare == " > "
result_init = "Integer.MIN_VALUE" result_init = extra_compare == " > " ? "Integer.MIN_VALUE" : "Integer.MAX_VALUE"
else
result_init = "Integer.MAX_VALUE"
end
if based_on_type == "int" if based_on_type == "int"
helper_variable = "int result#{based_on} = #{result_init};" helper_variable = "int result#{based_on} = #{result_init};"

View File

@ -1,6 +1,7 @@
def func_find_all(container_obj, attribute, filter_by) def func_find_all(container_obj, attribute, filter_by)
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).collect(Collectors.toList());" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).collect(Collectors.toList());"
@ -8,18 +9,18 @@ def func_find_all(container_obj, attribute, filter_by)
end end
def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func) def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func)
# TODO: Fix comparator to work for strings, such that I use .equals instead of ==
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{aggregate_func}();" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{aggregate_func}();"
end end
def find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable) def find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable)
# TODO: Fix comparator to work for strings, such that I use .equals instead of ==
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{variable}()).#{aggregate_func}();" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{variable}()).#{aggregate_func}();"
@ -27,7 +28,8 @@ end
def find_one(container_obj, attribute, filter_by) def find_one(container_obj, attribute, filter_by)
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).findAny().orElse(null);" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).findAny().orElse(null);"
@ -35,7 +37,8 @@ end
def find_best_value(container_obj, attribute, filter_by, min_or_max, based_on) def find_best_value(container_obj, attribute, filter_by, min_or_max, based_on)
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{based_on}()).#{min_or_max};" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{based_on}()).#{min_or_max};"
@ -43,7 +46,8 @@ end
def find_best_object(container_obj, attribute, filter_by, min_or_max, based_on) def find_best_object(container_obj, attribute, filter_by, min_or_max, based_on)
filters = filter_by.map do |x| filters = filter_by.map do |x|
"s.get#{attribute}() #{x.comparator} #{x.value}" comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}"
"s.get#{attribute}()#{comparator}"
end end
# TODO: Consider also having an "or" here # TODO: Consider also having an "or" here
"return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{min_or_max}(Comparator.comparing(#{based_on.type}.get#{based_on.variable}()));" "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{min_or_max}(Comparator.comparing(#{based_on.type}.get#{based_on.variable}()));"

44
lol.rb
View File

@ -14,6 +14,10 @@ to_string_template = "0 1 x 2 meter"
sorting_one = Type.new("String", "Name") sorting_one = Type.new("String", "Name")
sorting_two = Type.new("int", "Length") sorting_two = Type.new("int", "Length")
sorting_three = Type.new("int", "Width") sorting_three = Type.new("int", "Width")
sorting_two.comparator = "<"
sorting_three.comparator = "<"
sorting_filters = [sorting_one, sorting_two, sorting_three] sorting_filters = [sorting_one, sorting_two, sorting_three]
def construct_class(name_of_class, parameters, to_string_template, sorting_filters) def construct_class(name_of_class, parameters, to_string_template, sorting_filters)
@ -74,9 +78,6 @@ def construct_driver(class_name, parameters, methods, container_class, container
end.join "," end.join ","
end end
methods_to_print = ""
maggie = methods.map do |x| maggie = methods.map do |x|
if x[:print] if x[:print]
"System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));" "System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));"
@ -85,8 +86,8 @@ def construct_driver(class_name, parameters, methods, container_class, container
end end
end end
driver_string = " "
public class TestDriver { public class TestDriver {
public TestDriver(){} public TestDriver(){}
@ -150,7 +151,7 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name)
"this.#{x[:name]} = #{x[:name]};" "this.#{x[:name]} = #{x[:name]};"
end end
container_string = " "
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -164,27 +165,17 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name)
#{maggie.join "\n"} #{maggie.join "\n"}
public void print#{name}() { public void print#{name}() {
System.out.println(\"#{name}\"); System.out.println(\"#{name}\");
Collections.sort(#{arraylist_name}); Collections.sort(#{arraylist_name});
for (var i : #{arraylist_name}) { for (var i : #{arraylist_name}) {
System.out.println(i); System.out.println(i);
}
} }
} }"
}
"
container_string
end 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, sorting_filters)
file.close
container_methods = [ container_methods = [
{ {
@ -234,12 +225,15 @@ container_methods = [
:type => "String", :type => "String",
:name => "Name" :name => "Name"
}], }],
:method_type => find_whatever_of_variable(container_obj, "Name", [Filter.new("Name", "==")], "sum", "Length") :method_type => find_whatever_of_variable(container_obj, "Name", [Filter.new("Name", ".equals")], "sum", "Length")
} }
] ]
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, sorting_filters)
file.close
another_another_file = File.open("#{container_class}.java", "w") another_another_file = File.open("#{container_class}.java", "w")
another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => container_obj}], container_methods, container_obj)) another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => container_obj}], container_methods, container_obj))