Pass a function with lamba expression to make a generic function - java

I am using google's AutoValue to generate some configs, but before generating the configs, I would like to make sure that the entries are sanitized and default values are added to the list.
The AutoValue part looks like:
#AutoValue.Builder
public abstract static class Builder {
public abstract Builder primaryName(List<String> value);
public abstract Optional<List<String>> primaryName();
public abstract Builder primaryTitle(List<String> value);
abstract Optional<List<String>> primaryTitle();
abstract Config autoBuild();
public Config build() {
normalizePriorities();
EntitySourcePrioritizationConfig config = autoBuild();
return config;
}
I have the following code that is repeated in normalizePriorities():
private void normalizePriorities()
{
normalizeName();
normalizeTitle();
}
private void normalizeName() {
if (!primaryName().isPresent()) {
primaryName(defaultPrimaryNames());
} else {
List<String> providedConfigEntries = primaryName().get();
List<String> cleanConfig = sanitizeConfig(providedConfigEntries);
primaryName(cleanConfig);
}
}
private void normalizeTitle() {
if (!primaryTitle().isPresent()) {
primaryTitle(defaultPrimaryTitles());
} else {
List<String> providedConfigEntries = primaryTitle().get();
List<String> cleanConfig = sanitizeConfig(providedConfigEntries);
primaryTitle(cleanConfig);
}
}
I was wondering how I could use lambda expressions in order to reduce the deduplication of code.
The default names and titles are just a list of Strings as follows that could be passed as parameters:
public static ImmutableList<String> defaultPrimaryTitles() {
return ImmutableList.of(
"BBA",
"TNN");
}
public static ImmutableList<String> defaultPrimaryNames() {
return ImmutableList.of(
"SNS Inc.",
"ABC Corp.");
}
I have tried to generify the function like so:
normalize(primaryAlias(), defaultPrimaryTitles());
private void normalize(Optional<List<String>> configList, List<String> defaultConfig){
...
}
Unfortunately, I am not too sure how to generify and pass public abstract Builder primaryTitle(List<String> value) into the method.

You could pass a Consumer that accepts a list of strings and calls the builder methods, e.g.:
Create the consumer and pass it to the normalize method:
Consumer<List<String>> builderConsumer = (x) -> Builder.primaryName(x);
normalize(primaryAlias(), defaultPrimaryTitles(), builderConsumer);
And the normalize method:
private void normalize(ImmutableList<String> configList, List<String> defaultConfig, Consumer<List<String>> builderConsumer) {
List<String> cleanConfig = new ArrayList<>();
// ...
builderConsumer.accept(cleanConfig);
}

Related

Pass parameter to lambda expression - Java

My program requires that I accept a user input and, based on this input, a method is to be carried out. My basic thoughts are described well by the following question/answer:
How to call a method stored in a HashMap? (Java)
To do this, I have created an array of lambda expressions:
public final Runnable[] userCommandMethods = {
() -> userCommand1(),
() -> userCommand2(),
};
And an array of keys:
public final String[] userCommandKeys = {
commandKey1,
commandKey2,
};
Which are joined to create a HashMap using the following method:
public Map<String, Runnable> mapArrays (String[] array1, Runnable[] array2) {
Map<String, Runnable> mappedArrays = new HashMap<String, Runnable>();
for (int i = 0; i < array1.length; i ++) {
mappedArrays.put(array1[i], array2[i]);
}
return mappedArrays;
}
When I attempt to run a method by using myHashMap.get(userInput).run(); it works perfectly, provided none of the methods in userCommandMethods require input parameters.
My question:
How would I pass an input parameter (specifically a Hash Map) into the methods contained within userCommandMethods?
When the userCommand1() method takes an input parameter, but the lambda expression does not, I get the following error:
The method userCommand1(Map<String, String>) in the type ProgramCommands is not applicable for the arguments ()
However, when I do pass a parameter to the method, it states that it cannot be resolved to a variable.
Edit: to elaborate:
When the userCommand1() method takes no arguments:
public void userCommand1 () {
// Do some stuff
}
It works perfectly fine. However, I am unsure how to use the lambda expressions if the method does take an input parameter:
public void userCommand1 (Map<String, String> myMap) {
// Do some stuff
}
You just need to choose another functional interface (not Runnable).
For example, if your methods all take a String parameter, you should use Consumer<String>. If they take a String and an int, then you should use BiConsumer<String, Integer>. If your methods need more than 2 parameters, you need to create your own functional interface. For an example, see my answer here.
// use a list instead of an array, because arrays don't work well with generic types
public final List<Consumer<String>> userCommandMethods = List.of(
x -> userCommand1(x),
x -> userCommand2() // it's fine if the method takes fewer parameters
);
Instead of run, you would call accept, which is what Consumer and BiConsumer's single abstraction method is called.
Note that you can also use the method reference syntax. If userCommand1 is static, x -> userCommand1(x) can be rewritten as SomeClass::userCommand1 where SomeClass is the enclosing class of userCommand1. If userCommand1 is non static, it can be rewritten as this::userCommand1.
You don't need to build the map from two arrays. You can use ofEntries and entry to write the entries inline.
private final Map<String, Consumer<String>> someMap = Map.ofEntries(
Map.entry("foo", SomeClass::userCommand1),
Map.entry("bar", SomeClass::userCommand2),
Map.entry("baz", SomeClass::userCommand3),
// and so on
)
You are using Runnable interface that takes no argument on input:
#FunctionalInterface
public interface Runnable {
public abstract void run();
}
Instead, you can define your custom interface and consume it.
As a simple example:
#FunctionalInterface
public interface RunnableWithArg {
void apply(String t) throws RuntimeException;
}
And implementation may look like:
public class RunnableTest {
//also fine:
//public final RunnableWithArg[] userCommandMethods = { t -> this.userCommand1(t), t -> this.userCommand2(t) };
public final RunnableWithArg[] userCommandMethods = { this::userCommand1, this::userCommand2 };
public String commandKey1 = "commandKey1";
public String commandKey2 = "commandKey2";
public final String[] userCommandKeys = { commandKey1, commandKey2, };
public Map<String, RunnableWithArg> mapArrays(String[] array1, RunnableWithArg[] array2) {
Map<String, RunnableWithArg> mappedArrays = new HashMap<>();
for (int i = 0; i < array1.length; i++) {
mappedArrays.put(array1[i], array2[i]);
}
return mappedArrays;
}
public void userCommand1(String data) {
System.out.println("userCommand1 called with " + data);
}
public void userCommand2(String data) {
System.out.println("userCommand2 called with " + data);
}
public void test()
{
var fncMap = mapArrays(userCommandKeys, userCommandMethods);
for(String key: fncMap.keySet())
{
var fnc = fncMap.get(key);
fnc.apply(key);
}
}
}
And of course you can also define some generic types of "#FunctionalInterface" like this, so you can use it for both taking input and returning some output of generic types:
#FunctionalInterface
public interface AbcFunction<T, R> {
R apply(T t) throws AbcException;
static <T> Function<T, T> identity() {
return t -> t;
}
}
Is this something you are thinking of?
interface Command<T> {
public void run(T arg);
}
class SayHelloCommand implements Command<String>{
public void run(String name){
System.out.println("hello " + name);
}
}
class CountCommand implements Command<Integer>{
public void run(Integer limit){
for(int i=0; i<=limit; i++)
System.out.println(i);
}
}
public class Main{
public static void main(String[] args) {
Command[] commands = new Command[3];
commands[0] = new SayHelloCommand();
commands[1] = new CountCommand();
commands[0].run("Joe");
commands[1].run(5);
}
}

Get list of ArrayList

I want to create a ArrayList of data and return it via Rest point. I tried this:
#Service
public class CardBrandsListService {
public ArrayList<String> getCardBrandsList() {
ArrayList<String> list = new ArrayList<String>();
list.add("visa");
list.add("master");
list.add("Intl Maestro");
list.add("amex");
return list;
}
}
Rest endpoint:
#GetMapping("/card_brand/list")
public ResponseEntity<?> getCurruncy() {
return ResponseEntity.ok(cardBrandsListService.getCardBrandsList().entrySet().stream()
.map(g -> new CardBrandsListDTO(g.getValue())).collect(Collectors.toList()));
}
DTO:
public class CardBrandsListDTO {
private String card_brand;
public String getCard_brand() {
return card_brand;
}
public void setCard_brand(String card_brand) {
this.card_brand = card_brand;
}
}
But I get error: The method entrySet() is undefined for the type ArrayList<String> What is the proper wya to map ArrayList?
Your rest endpoint should look like the following:
#GetMapping("/card_brand/list")
public ResponseEntity<List<CardBrandsListDTO>> getCurruncy() {
return ResponseEntity.ok(cardBrandsListService.getCardBrandsList().stream()
.map(g -> new CardBrandsListDTO(g)).collect(Collectors.toList()));
You are calling entrySet(), which is used to get a Set of entries of a Map object (which you don't have). Additionally, inside the map function, your variable g is a String (since you are returning an ArrayList<String>), therefore you can directly supply that to the constructor. And you can directly set the correct type for the ResponseEntity as well.
UPDATE:
And you need the corresponding constructor:
public class CardBrandsListDTO {
private String card_brand;
public CarBrandsListDTO(String card_brand) {
this.car_brand = car_brand;
}
//getter and setter
}
By the way, I would advise you to rename the DTO (for understandability) and also the field inside it (to follow naming conventions)

Instantiating DynamoDBQueryExpression with generic classtypes

Edit: I was trying to simplify my problem at hand a little, but turns out, it created more confusion instead. Here's the real deal:
I am working with AWS's Java SDK for DynamoDB. Using the DynamoDBMapper class, I am trying to query DynamoDB to retrieve items from a particular table. I have several objects that map to my DynamoDB tables, and I was hoping to have a generic method that could accept the mapped objects, query the table, and return the item result.
Psudo-code:
#DynamoDBTable(tableName="testTable")
public class DBObject {
private String hashKey;
private String attribute1;
#DynamoDBHashKey(attributeName="hashKey")
public String getHashKey() { return this.hashKey; }
public void setHashKey(String hashKey)
#DynamoDBAttribute(attributeName="attribute1")
public String getAttribute1() { return this.attribute1; }
public void setAttribute1(String attribute1) { this.attribute1 = attribute1; }
}
public class DatabaseRetrieval {
public DatabaseRetrieval()
{
DBObject dbObject = new DBObject();
dbObject.setHashKey = "12345";
DBRetrievalAgent agent = new DBRetrievalAgent;
dbObject = agent.retrieveDBObject(dbObject.getClass(), dbObject);
}
}
public class DBRetrievalAgent {
public Object retrieveDBObject(Class<?> classType, Object dbObject)
{
DynamoDBQueryExpression<classType> temp = new DynamoDBQueryExpression<classType>().withHashKeyValues(dbObject);
return this.dynamoDBMapper.query(classType, temp);
}
}
Use a type witness within your method:
public <T> String getResult(Class<T> type) {
List<T> newList = new ArrayList<>();
//other code
}
Try this
ArrayList<T> newList = new ArrayList<>();
You can specify the type as T for your getResult() to make it generic (i.e., accepts any class) as shown below:
public <T> String getResult(T t) {
String result = "";
List<T> newList = new ArrayList<>();
// perform actions
return result;
}

Match List of objects against list of properties

I'm trying to use hamcrest matchers to match a list of objects against a list/array of their properties. For one property value this is not a problem, because I can do something like this:
assertThat(savedGroup.getMembers(),
containsInAnyOrder(hasProperty("name", is(NAMES[0]))));
For multiple property values I can use multiple hasProperty() calls
assertThat(savedGroup.getMembers(),
containsInAnyOrder(
hasProperty("name", is(NAMES[0])),
hasProperty("name", is(NAMES[1]))));
But is there a generic way to match against all values in the NAMES array?
The best way (IMO) to do this would be to combine the overloaded containsInAnyOrder Matcher along with a custom FeatureMatcher. Ultimately your code would look like this:
String[] expectedNames = new String[] { "John", "Bob", "Carol"};
assertThat(savedGroup.getMembers(), hasNames(expectedNames));
hasNames is implemented as follows:
private Matcher<Iterable<? extends Member>> hasNames(String[] expectedNames) {
return containsInAnyOrder(Arrays.stream(expectedNames).map(name -> name(name)).collect(Collectors.toList()));
}
And the final part is the call to name which generates a Matcher that will extract a property in a type-safe way from your object:
private Matcher<Member> name(String name) {
return new FeatureMatcher<Member, String>(equalTo(name), "name", "name") {
#Override
protected String featureValueOf(Member actual) {
return actual.getName();
}
};
}
The benefit of doing it this is way is that:
You get the benefit of type-safety instead of using hasProperty
Your test now describes what you actual want to match on, i.e. hasNames
The code produced is now more flexible and composable. Want to match a single objects name? All you now need to do is assertThat(member, has(name("Fred")))
You can get even more composability by moving the equalTo sub-matcher to be part of the hasNames call like this:
private Matcher<Iterable<? extends Member>> hasNames(String[] expectedNames) {
return containsInAnyOrder(Arrays.stream(expectedNames).map(name -> name(equalTo(name))).collect(Collectors.toList()));
}
private Matcher<Member> name(Matcher<String> nameMatcher) {
return new FeatureMatcher<Member, String>(nameMatcher, "name", "name") {
#Override
protected String featureValueOf(Member actual) {
return actual.getName();
}
};
}
One of containsInAnyOrder's overloads accepts a collection of matchers as its argument. Thus you could do something like this:
assertThat(
savedGroup.getMembers(),
containsInAnyOrder(
Stream.of(NAMES)
.map(name -> hasProperty("name", is(name)))
.collect(Collectors.toList())
));
(if using Java 8, otherwise would need to add a loop building up the collection)
Need to make some cleanup (description output), but I think it does solve your problem:
package org.example.matchers;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher;
public class ContainsArrayElementsInAnyOrder<T> extends TypeSafeMatcher<List<T>> {
private T[] toMatch;
public ContainsArrayElementsInAnyOrder(final T[] toMatch) {
this.toMatch = toMatch;
}
#Override
protected boolean matchesSafely(List<T> item) {
if(item.size() != toMatch.length) {
return false;
}
for (T t : toMatch) {
if(!item.contains(t)) {
return false;
}
}
return true;
}
#Override
public void describeMismatchSafely(List<T> item, Description mismatchDescription) {
mismatchDescription.appendValueList("[", ",", "]", item);
}
#Override
public void describeTo(Description description) {
description.appendValueList("[", ",", "]", toMatch);
}
#Factory
public static <T> ContainsArrayElementsInAnyOrder<T> containsArrayElementsInAnyOrder(T[] elements) {
return new ContainsArrayElementsInAnyOrder<T>(elements);
}
}
Test:
#Test
public void shouldContainsInAnyOrderSameElementsInArrayAsInList() {
final String[] NAME = new String[]{"name3", "name1", "name2"};
final List<String> result = new ArrayList<>(3);
result.add("name2");
result.add("name1");
result.add("name4");
assertThat(result, containsArrayElementsInAnyOrder(NAME));
}
Output if not match:
java.lang.AssertionError:
Expected: ["name3","name1","name2"]
but: ["name2","name1","name4"]
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at ..

How to create a Supplier with a toString returning the get?

I would like to make Map values resolvement lazy, so was thinking about providing a Supplier with a toString function. But below code does not compile:
A default method cannot override a method from java.lang.Object
Anyone an idea how to solve this in a neat way?
#FunctionalInterface
private static interface ToStringSupplier extends Supplier<String>
{
default public String toString() {
return get();
}
}
The reason I want this is that my consumers (which are in another repository) first can update their code:
From:
String value = (Strint)map.get(key);
To:
String value = map.get(key).toString();
After which I can change the implementation to a lazy approach:
From:
String value = expensiveCalculation();
map.put(key,value);
To:
Supplier<String> supplier () -> expensiveCalculation();
map.put(key, supplier);
I found below code working fine for my problem:
private static Object getToString(Supplier<String> s) {
return new Object()
{
#Override
public String toString() {
return s.get();
}
};
}
Supplier<String> supplier = () -> expensiveCalculation();
map.put(key, getToString(supplier));
As Louis Wasserman mentioned in the comments section of the question, it is not possible to override an instance method with a default one. It can be done with a new class that delegates the #toString call to the #get method of the provided supplier.
Here's how it can be done:
import java.util.Map;
import java.util.function.Supplier;
class Scratch {
public static final class ToStringSupplier implements Supplier<String> {
private final Supplier<String> supplier;
public ToStringSupplier(Supplier<String> supplier) {
if (supplier == null) {
throw new NullPointerException();
}
this.supplier = supplier;
}
#Override
public String toString() {
System.out.println("Invoked ToStringSupplier#toString.");
return get();
}
#Override
public String get() {
System.out.println("Invoked ToStringSupplier#get.");
return supplier.get();
}
}
public static void main(String[] args) {
final var supplier = new ToStringSupplier(() -> {
System.out.println("Invoked Supplier#get.");
return "The result of calculations.";
});
final var key = "key";
final var map = Map.of(key, supplier);
System.out.println("The map has been built.");
final var calculationResult = map.get(key).toString();
System.out.println(calculationResult);
System.out.flush();
}
}
The output is:
The map has been built.
Invoked ToStringSupplier#toString.
Invoked ToStringSupplier#get.
Invoked Supplier#get.
The result of calculations.
default is a reserved word use in switch statements
You probably want to use abstract

Categories