Calling method in java 8

swayam swayam :

I have a method,

private String createSubjectColumnForOutgoing(Message message)
{
    //TODO : Changes for blocking messages of spam users


    if(message.getReceiverEnvelope() != null && message.getReceiverEnvelope().getUser() != null && message.getReceiverEnvelope().getUser().isBlocked())
    {
        return I18N.IN_REVIEW_BY_TEAM.msg();
    }


    return StringUtils.deSanitizeSpecialCharacters(message.getSubject());
}

and this method is called like this,

case OUTGOING:
            table.addGeneratedColumn(I18N.MESSAGETABLE_HEADER_SUBJECT.msg(), this::createSubjectColumnForOutgoing);
            break;

And the constructor in the class are,

public MessageTable(Directory directory, boolean withFilter, Device device)
{
    this(directory, new FilterConfiguration(withFilter), device);
}

public MessageTable(Directory directory, FilterConfiguration filterConfiguration, Device device)
{
    Objects.requireNonNull(directory);
    Objects.requireNonNull(device);

    this.directory = directory;

    dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(LocaleAware.super.getLocale());

    tableFooter = createTableFooter();

    openButton = createOpenButton();
    newButton = createNewButton();
    messageTable = createTable(device);

    tableFooter.addComponent(newButton, 0);
    tableFooter.addComponent(openButton, 1);

    final VerticalLayout layout = new VerticalLayout();
    layout.addComponent(createFilterComponent(filterConfiguration));
    layout.addComponents(tableFooter, messageTable);
    layout.setComponentAlignment(tableFooter, Alignment.MIDDLE_LEFT);

    rootLayout = layout;
    setCompositionRoot(rootLayout);

}

When calling this method createSubjectColumnForOutgoing there is no parameter passed, and it is working perfectly. I am not able to understand from where data is coming in Message object. I googled but not able to understand it. Please help. Thanks in advance.

Code of addGeneratedColumn

public void addGeneratedColumn(Object id, Function<BEANTYPE, ?> generatedColumn)
{
    String header = null;
    if(id instanceof String)
    {
        header = (String) id;
    }
    addGeneratedColumn(header, id, (source, itemId, columnId) -> generatedColumn.apply(itemId));
}
Eran :

this::createSubjectColumnForOutgoing is a method reference, not an execution of the createSubjectColumnForOutgoing method.

The table.addGeneratedColumn() method, to which you pass the method reference, may be calling the method of the functional interface implemented by this method reference. If it does, it passes a Message instance to it.

You haven't included the code of addGeneratedColumn(), so I don't know what type of functional interface it expects (perhaps a Function).

EDIT:

Following your edit, Function<BEANTYPE, ?> generatedColumn is the functional interface implemented by the method reference you pass to addGeneratedColumn(). This means that generatedColumn.apply(itemId) is the statement that executes the createSubjectColumnForOutgoing() method, and you can see that itemId is passed to the method. This means that itemId must be a Message instance.

Note that addGeneratedColumn(Object id, Function<BEANTYPE, ?> generatedColumn) doesn't execute the createSubjectColumnForOutgoing() method either. It passes a functional interface (implemented by a lambda expression) that can execute that method to a second addGeneratedColumn method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102599&siteId=1