I'm trying to implement a function to read files, but I cannot change the signature of the method. There are a lot of cross references in the code but maybe someone can enlighten me, I have been stuck for 3 days right now.
The first method I'm trying to pass into the function is the following:
Purchase newPurchase = null;
String[] purchases = textLine.split(",");
int foundBarcode = products.indexOf(getProductFromBarcode(products, Long.parseLong(purchases[0])));
products.indexOf(purchases);
newPurchase = new Purchase(
products.get(foundBarcode),
Integer.parseInt(purchases[1].trim())
);
Somehow I want to pass this function into my import file function.
public static <E> void importItemsFromFile(List<E> items, String filePath, Function<String,E> converter) {
int originalNumItems = items.size();
Scanner scanner = createFileScanner(filePath);
// TODO read all source lines from the scanner,
// convert each line to an item of type E and
// and add each item to the list
while (scanner.hasNext()) {
// input another line with author information
String line = scanner.nextLine();
// TODO convert the line to an instance of E
E newItem = converter.apply(line);
// TODO add the item to the list of items
items.add(newItem);
}
System.out.printf("Imported %d items from %s.\n", items.size() - originalNumItems, filePath);
}
I hope someone can help me and explain how to pass this function into the other function's converter parameter. Tried to do a lot of research on this topic but I'm still not able to find the answer. Please help me stackoverflow community!:D
CodePudding user response:
You need to declare this like this first inside a 'super - private function'
private Function<String, Purchase> doSomething() {
return textLine -> {
Purchase newPurchase = null;
String[] purchases = textLine.split(",");
int foundBarcode = products.indexOf(getProductFromBarcode(products, Long.parseLong(purchases[0])));
products.indexOf(purchases);
return new Purchase(products.get(foundBarcode), Integer.parseInt(purchases[1].trim())
};
}
Then before calling this importItemsFromFile() method, you declare this function as a variable
Function<String, Purchase> abcFunction = doSomething();
and then call this importItemsFromFile() function like this
importItemsFromFile(items, filePath, abcFunction);
The reason why this works is, lambda operator returns a Functional Interface, here it is "Function", if you pass 3 arguments, you will need a BiFunction<T, U, R>
for that.
Thats why these FunctionalInterfaces are sometimes called super private functions, as they are used inside another function.
And Hey give me extra points for this, as I even completed your half written function, and saved your 3 days time.
CodePudding user response:
I am a little lost by the top snippet, but all you need to do is define an implementation of the Function interface. You can do this simply by defining a class that implements the interface or you can specify a lambda that would do the same thing.
Assuming the top code block is what you want in your lambada you would do something like this,
(textLine) ->{
String[] purchases = textLine.split(",");
int foundBarcode = products.indexOf(getProductFromBarcode(
products, Long.parseLong(purchases[0])));
products.indexOf(purchases);
return new Purchase(
products.get(foundBarcode),
Integer.parseInt(purchases[1].trim())
)