Kevin Santos Kevin Santos - 4 months ago 9
Java Question

convert lambda to function/method

I am trying to understand a program in java which has used lambda to return object into the reference variable of an interface. I want to convert lambda into simple java function, but not sure how to do it.

The program is as follows:-

public class AgentLicenseLazyModel extends CoreDataModel<AgentLicenseModel> {

public AgentLicenseLazyModel(final List<AgentLicenseModel> entities) {
super(AgentLicenseModel.class, entities, (sortField, sortOrder) -> {
return new AgentLicenseLazySorter(sortField, sortOrder);
});
}
}


Here is the abstract class:

public abstract class CoreDataModel<T extends AbstractEntityModel> extends LazyDataModel<T> {

private final Class<T> entityClass;
private final List<T> entities;
private final CoreDataSorterProducer<T> coreDataSorterProducer;

public CoreDataModel(final Class<T> entityClass, final List<T> entities, final CoreDataSorterProducer<T> coreDataSorterProducer) {
this.entityClass = entityClass;
this.entities = entities;
this.coreDataSorterProducer = coreDataSorterProducer;

if (entities != null) {
setRowCount(entities.size());
}
}


How to convert this program into simple java program without use of lambda for learning. Please help.

Edited:
Here is the CoreDataSorterProducer:

@FunctionalInterface
public interface CoreDataSorterProducer<T extends AbstractEntityModel> {

CoreDataSorter<T> produce(String sortField, SortOrder sortOrder);
}

Answer

The body of the lambda expression is the implementation of the single abstract method of the CoreDataSorterProducer<AgentLicenseModel> interface.

You can replace the lambda expression with an anonymous classs instance that implements CoreDataSorterProducer<AgentLicenseModel>.

public class AgentLicenseLazyModel extends CoreDataModel<AgentLicenseModel> {
    public AgentLicenseLazyModel(final List<AgentLicenseModel> entities) {
        super(AgentLicenseModel.class, entities, new CoreDataSorterProducer<AgentLicenseModel> () {

            public AgentLicenseLazySorter theMethodName (TheTypeOfSortField sortField, TheTypeOfSortOrder sortOrder) 
            {
                return new AgentLicenseLazySorter(sortField, sortOrder);
            }
        });
    }
}

Note that theMethodName, TheTypeOfSortField and TheTypeOfSortOrder should be replaced by the method name of the CoreDataSorterProducer interface and the types of its arguments.