We're migrating some business logic from Clojure to Java. Currently I'm working on 2 methods, but I have problems with 2nd one.
First one is - I believe - transforming map to vector, but only including values that we provide keys for (and also removing null values).
(defn- spec-vec [tags-with-classification & tag-types]
(into [] (remove nil? (map tags-with-classification tag-types))))
and this is how I translated it to Java code:
private List<String> specVec(Map<String, String> tagsWithClassification, List<String> tagTypes) {
List<String> list = new ArrayList<>();
for (String tagType : tagTypes) {
String value = tagsWithClassification.get(tagType);
if (value != null) {
list.add(value);
}
}
return list;
}
I'm posting this one, because it is used in the other method, but I might misunderstood what it does.
The other has map with vector of strings as key, and string as value, but there is also some logic related to filtering the key, but that part I already got right - I think.
This is the java code I got so far, but I'm stuck on how to transform this map to HashMap in a nice way (as ArrayList/LinkedList are not very good recommendation to use as a key in HashMap). Or maybe I should use entierly different approcach/different structure? Thanks in advance :)
Note: there are only 5 (4 different strings and null) possible values to be returned.
2nd Java method:
private String bookingModelFor(Map<String, String> tagsWithClassification, List<String> tagTypes) {
List<String> tagNames = new ArrayList<>(Arrays.asList("event", "product", "client-trade-direction", "r-factor-direction"));
List<String> filteredTagValues = specVec(tagsWithClassification, tagNames);
filteredTagValues.addAll(tagTypes);
/*
missing part
*/
return null;
}
2nd Clojure function:
(defn booking-model-for [tags-with-classification & tag-types]
(get {[:trade :goal :increase :vanilla] "Astrid"
[:trade :goal :decrease :vanilla] "Maja"
[:trade :bloc :increase :vanilla] "Isabelle"
[:trade :bloc :decrease :vanilla] "Luna"
[:trade :perles-plus :increase :call] "Astrid"
[:trade :perles-plus :increase :put] "Luna"
[:trade :perles-plus :increase :kickout] "Astrid"
[:trade :perles-plus :decrease :call] "Maja"
[:trade :perles-plus :decrease :put] "Isabelle"
[:trade :perles-plus :decrease :kickout] "Maja"
[:trade :reverse-perles-plus :increase :call] "Astrid"
[:trade :reverse-perles-plus :increase :put] "Luna"
[:trade :reverse-perles-plus :increase :kickout] "Astrid"
[:trade :reverse-perles-plus :decrease :call] "Maja"
[:trade :reverse-perles-plus :decrease :put] "Isabelle"
[:trade :reverse-perles-plus :decrease :kickout] "Maja"
[:trade :kig :increase :put] "Astrid"
[:trade :kig :increase :kickout] "Maja"
[:trade :kig :decrease :put] "Maja"
[:trade :kig :decrease :kickout] "Astrid"
[:expire :goal :decrease :vanilla] "Maja"
[:expire :bloc :decrease :vanilla] "Luna"
[:expire :kig :decrease :put] "Maja"
[:expire :kig :decrease :kickout] "Astrid"
[:expire :perles-plus :increase :put] "Maja"
[:expire :perles-plus :increase :call] "Isabelle"
[:expire :perles-plus :increase :kickout] "Isabelle"
[:expire :reverse-perles-plus :increase :put] "Maja"
[:expire :reverse-perles-plus :increase :call] "Isabelle"
[:expire :reverse-perles-plus :increase :kickout] "Isabelle"
[:barrier-breach :kig :kickout] "Astrid"
[:barrier-breach :perles-plus :kickout] "Astrid"
[:share-adjustment :goal :r-factor>=1 :vanilla] "Maja"
[:share-adjustment :goal :r-factor<1 :vanilla] "Astrid"
[:share-adjustment :bloc :r-factor>=1 :vanilla] "Luna"
[:share-adjustment :bloc :r-factor<1 :vanilla] "Isabelle"
[:share-adjustment :kig :r-factor>=1 :put] "Maja"
[:share-adjustment :kig :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :kig :r-factor<1 :put] "Astrid"
[:share-adjustment :kig :r-factor<1 :kickout] "Maja"
[:share-adjustment :perles-plus :r-factor>=1 :call] "Astrid"
[:share-adjustment :perles-plus :r-factor>=1 :put] "Luna"
[:share-adjustment :perles-plus :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :perles-plus :r-factor<1 :call] "Maja"
[:share-adjustment :perles-plus :r-factor<1 :put] "Isabelle"
[:share-adjustment :perles-plus :r-factor<1 :kickout] "Maja"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :call] "Astrid"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :put] "Luna"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :reverse-perles-plus :r-factor<1 :call] "Maja"
[:share-adjustment :reverse-perles-plus :r-factor<1 :put] "Isabelle"
[:share-adjustment :reverse-perles-plus :r-factor<1 :kickout] "Maja"}
(into (spec-vec tags-with-classification :event :product :client-trade-direction :r-factor-direction) tag-types)))
Here are some test cases that help me in migration:
Tests for 1st function:
(deftest spec-vec-test
(is (= [] (#'ksm.build.transform.neds/spec-vec nil)))
(is (= [:trade :goal 1] (#'ksm.build.transform.neds/spec-vec {:a :trade :b :goal :c :decrease :d :vanilla :e 1} :a :b :e))))
Tests for 2nd function:
(deftest booking-model-for-test
(is (nil? (#'ns/booking-model-for {})))
(is (nil? (#'ns/booking-model-for {:a 1 :b 2 :c 3} :d :e)))
(is (= "Maja"
(#'ns/booking-model-for {:event :trade :product :goal :client-trade-direction :decrease :r-factor-direction :vanilla})))
(is (= "Isabelle"
(#'ns/booking-model-for {:event :expire :product :reverse-perles-plus :client-trade-direction :increase :r-factor-direction :kickout})))
(is (= "Luna"
(#'ns/booking-model-for {:event :share-adjustment :product :reverse-perles-plus :client-trade-direction :r-factor>=1 :r-factor-direction :put}))))