0

I'm very surprise by a non expected behaviour of a class I'm building (kind of Composite pattern).

Here's the point and the classes involved :

  • IGroupable is an interface with 2 methods

    public interface IGroupable {

    BigDecimal getTotal();
    Boolean isManuallySettlable();
    

    }

  • Event class implements IGroupable

    @Entity @Table(name="V_ALL_BUSINESS_EVENTS") public class EventView implements IGroupable, Serializable {

    @Id
    @Column(name="UNIQUE_ID")
    private long eventId;
    ...
    
    @Override
    public BigDecimal getTotal() {
        return this.eventGrossAmount;
    }
    
    @Override
    public Boolean isManuallySettlable() {
        return (this.eventIsManuallySettlable == null || this.eventIsManuallySettlable == false) ? false : true; 
    }
    
  • AbstractGroup abstract class implements IGroupable :

    public abstract class AbstractGroup implements IGroupable, Serializable {

    private static final long serialVersionUID = 1L;
    
    protected int nbRows;
    protected transient List<IGroupable> groups;
    ...
    
  • CurrencyGroup, BuyerPlatformGroup and CounterpartExecDateGroup all extend AbstractGroup, so each has a List<IGroupable> groups property

    public class CurrencyGroup extends AbstractGroup {

    protected String currency;
    
    // --------------------------------------------------------
    
    public CurrencyGroup(String currency) {
        this.currency = currency;
        super.groups = new ArrayList<>();
    }
    ...
    
    @Override
    public BigDecimal getTotal() {
        BigDecimal total = new BigDecimal(0);
        total = this.groups.stream()
            .map(groupable -> groupable.getTotal())
            .reduce(total, BigDecimal::add);
        return total;
    }
    
    @Override
    public Boolean isManuallySettlable() {
        boolean isSettlable = true;
        for(IGroupable g : groups) {
            if(!g.isManuallySettlable()) {
                isSettlable = false;
            }
        }
        return isSettlable;
    }
    

    public class BuyerPlatformGroup extends AbstractGroup {

    protected String buyerName;
    protected String buyerRTS;
    protected String platform;
    
    // --------------------------------------------------------
    
    public BuyerPlatformGroup(String buyerName, String buyerRTS, String platform) {
        this.buyerName = buyerName;
        this.buyerRTS = buyerRTS;
        this.platform = platform;
        super.groups = new ArrayList<>();
    }
    
    ...
    
    @Override
    public BigDecimal getTotal() {
        BigDecimal total = new BigDecimal(0);
        total = this.groups.stream()
            .map(groupable -> groupable.getTotal())
            .reduce(total, BigDecimal::add);
        return total;
    }
    
    @Override
    public Boolean isManuallySettlable() {
        boolean isSettlable = true;
        for(IGroupable g : groups) {
            if(!g.isManuallySettlable()) {
                isSettlable = false;
            }
        }
        return isSettlable;
    }
    

    }

    public class CounterpartExecDateGroup extends AbstractGroup {

    protected String counterpart;
    protected Date execDate;
    protected BigDecimal amount;
    protected String step;
    protected String operationType;
    
    // --------------------------------------------------------
    
    public CounterpartExecDateGroup() {
        super.groups = new ArrayList<>();
    }
    ...
    
    @Override
    public BigDecimal getTotal() {
        BigDecimal total = new BigDecimal(0);
        total = groups.stream()
            .map(groupable -> groupable.getTotal())
            .reduce(total, BigDecimal::add);
        return total;
    }
    
    @Override
    public Boolean isManuallySettlable() {
        boolean isSettlable = true;
        for(IGroupable g : groups) {
            if(!g.isManuallySettlable()) {
                isSettlable = false;
            }
        }
        return isSettlable;
    }
    

    }

-> they all inherit from 'AbstractGroup' and also all are 'IGroupable' objects

@Test
    public void allIGroupableTest() {

        List<EventView> events = eventDao.findAll();
        EventView event = (EventView) events.get(0);

        CurrencyGroup ccyGrp = new CurrencyGroup("EUR");
        BuyerPlatformGroup buyerGrp = new BuyerPlatformGroup(event.getEventBuyerName(), event.getEventRTSCode(), event.getEventPlatform());
        CounterpartExecDateGroup counterpartGrp = new CounterpartExecDateGroup();

        ccyGrp.addGroup(buyerGrp); // OK
        buyerGrp.addGroup(counterpartGrp); // OK
        counterpartGrp.setGroups(events); **// ERROR**

But I've got an error, telling me : The method setGroups(List<IGroupable>) in the type AbstractGroup is not applicable for the arguments (List<EventView>).

Why does it behave this way ? As the Event class implements IGroupable interface, a list of IGroupable objects should be able to accept a list of Events, isn't it ?

Lovegiver
  • 413
  • 6
  • 22
  • 5
    please show us your code instead of describing it. It will be much easier to understand the problem this way. – Eran Dec 04 '18 at 09:16
  • 1
    Also, try to format your question such that class names and inline code are in backticks, and don't use plain apostrophes as these cause confusion. And of course, use the `{}` button for any block of code. – RealSkeptic Dec 04 '18 at 09:18
  • 1
    `EventView` is not `Event`, is it? – Florian Albrecht Dec 04 '18 at 09:18
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Dec 04 '18 at 09:20

0 Answers0