anirudha-wegilant / lambdaj

Automatically exported from code.google.com/p/lambdaj
Apache License 2.0
0 stars 0 forks source link

Sum over collection returns wrong value when done again #74

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I have JUnit test having lines:

final Set<CostItem> costItems = project.getItems();

// These asserts pass
assertEquals(11f, Lambda.sum(costItems , 
Lambda.on(CostItem.class).getCashOut()));
assertEquals(22f, Lambda.sum(costItems , 
Lambda.on(CostItem.class).getTravel()));
assertEquals(33f, Lambda.sum(costItems , Lambda.on(CostItem.class).getHours()));

// This fails because I assert same property 'getCashOut' AGAIN
assertEquals(11f, Lambda.sum(costItems , 
Lambda.on(CostItem.class).getCashOut()));

CostItem is an annotated Jpa entity, but the issue exists even if 
the object is not handle by Hibernate. So it should be just plain POJO.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
lambdaj-2.3.3-with-dependencies.jar
JUnit 4
JDK 1.6.0_18
Windows 7

Please provide any additional information below.

Original issue reported on code.google.com by j...@sci.fi on 22 Aug 2011 at 5:41

GoogleCodeExporter commented 8 years ago
Cannot reproduce.
Please provide a complete failing test case if possible.

Original comment by mario.fu...@gmail.com on 29 Jan 2012 at 10:35

GoogleCodeExporter commented 8 years ago
import java.util.HashSet;
import java.util.Set;

import ch.lambdaj.Lambda;

public class LambdaJTest {

    public static void main(String[] args) {

        Set<CostItem> costItems = new HashSet<CostItem>();

        CostItem costItem = new CostItem();
        costItem.setCashOut(5f);
        costItem.setTravel(10f);
        costItems.add(costItem);

        costItem = new CostItem();
        costItem.setCashOut(2f);
        costItem.setTravel(20f);
        costItems.add(costItem);

        costItem = new CostItem();
        costItem.setCashOut(3f);
        costItem.setTravel(30f);
        costItems.add(costItem);

        System.out.println(Lambda.sum(costItems , Lambda.on(CostItem.class).getCashOut()));
        System.out.println(Lambda.sum(costItems , Lambda.on(CostItem.class).getTravel()));

        // Prints out sum of travel, but should be cash out
        System.out.println(Lambda.sum(costItems , Lambda.on(CostItem.class).getCashOut()));

    }
}

import java.io.Serializable;

@SuppressWarnings("serial")
public class CostItem implements Serializable {

    private int _ordinal = 0;

    public static final String PROP_HOURS = "hours";
    private float _hours;

    public static final String PROP_TRAVEL = "travel";
    private float _travel;

    public static final String PROP_CASHOUT = "cashOut";
    private float _cashOut;

    public static final String PROP_MATERIALCOST = "materialCost";
    private float _materialCost;

    public static final String PROP_ENGINECOST = "engineCost";
    private float _engineCost;

    private long _nodeId;
    private String _externalResourceName;

    private int _role;

    public static final String PROP_DESCRIPTION = "description";
    private String _description;

    public long getNodeId() {
        return _nodeId;
    }
    public void setNodeId(long id) {
        _nodeId = id;
    }

    public int getOrdinal() {
        return _ordinal;
    }
    public void setOrdinal(int ordinal) {
        _ordinal = ordinal;
    }

    public float getHours() {
        return _hours;
    }
    public void setHours(float hours) {
        _hours = hours;
    }

    public float getTravel() {
        return _travel;
    }
    public void setTravel(float travel) {
        _travel = travel;
    }

    public float getCashOut() {
        return _cashOut;
    }
    public void setCashOut(float cashOut) {
        _cashOut = cashOut;
    }

    public float getMaterialCost() {
        return _materialCost;
    }
    public void setMaterialCost(float materialCost) {
        _materialCost = materialCost;
    }

    public float getEngineCost() {
        return _engineCost;
    }
    public void setEngineCost(float engineCost) {
        _engineCost = engineCost;
    }

    public String getExternalResourceName() {
        return _externalResourceName;
    }
    public void setExternalResourceName(String name) {
        _externalResourceName = name;
    }

    public boolean isExternalResourceItem() {
        return getExternalResourceName() != null;
    }

    public int getRole() {
        return _role;
    }
    public void setRole(int role) {
        _role = role;
    }

    public String getDescription() {
        return _description;
    }
    public void setDescription(String description) {
        _description = description;
    }
    public void appendDescription(String string) {
        _description = (_description == null ? "" : _description) + string;
    }

}

Original comment by j...@sci.fi on 30 Jan 2012 at 8:55

GoogleCodeExporter commented 8 years ago

Original comment by mario.fu...@gmail.com on 4 Feb 2012 at 10:40

GoogleCodeExporter commented 8 years ago
Fixed in release 2.4

Original comment by mario.fu...@gmail.com on 4 Feb 2012 at 10:53