Java - Guava表没有返回值

时间:2015-11-18 20:52:16

标签: java data-structures guava

我正在使用Google的Guava库作为Table类。

我的表定义如下:

    private Table<ThreePartKey, DateTime, UnitDay> table = TreeBasedTable.create();

ThreePartKey是一个包含3个字符串值作为字段的对象。它被用作表中的Row索引。 DateTime是一个Joda Time对象,用作表的Column索引。 UnitDay是一个普通的旧java对象,它是表的值。

我填充了表格,后来在我的代码中我想访问表格的特定单元格。我使用以下代码:

UnitDay tempUnitDay = table.get(threePartKey, dateTime);

但是tempUnitDay为null。我在Eclipse调试模式下运行它,并且threePartKey和dateTime都不为null。表中不应该有空值。

有谁知道我做错了什么?

编辑:

@ TR1我想到了这一点,但它应该有一个列/行的值。我创建了一个ThreePartKeys列表和一个DateTimes列表,然后从这些列表中创建了一个表,其中一个空的UnitDay作为值。然后我再次返回迭代相同的值以填充UnitDay值。

以下是我的ThreePartKey类的代码:

public class ThreePartKey implements Comparable<ThreePartKey> {

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((facilityCode == null) ? 0 : facilityCode.hashCode());
    result = prime
            * result
            + ((facilityGroupCode == null) ? 0 : facilityGroupCode
                    .hashCode());
    result = prime * result
            + ((unitCode == null) ? 0 : unitCode.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ThreePartKey other = (ThreePartKey) obj;
    if (facilityCode == null) {
        if (other.facilityCode != null)
            return false;
    } else if (!facilityCode.equals(other.facilityCode))
        return false;
    if (facilityGroupCode == null) {
        if (other.facilityGroupCode != null)
            return false;
    } else if (!facilityGroupCode.equals(other.facilityGroupCode))
        return false;
    if (unitCode == null) {
        if (other.unitCode != null)
            return false;
    } else if (!unitCode.equals(other.unitCode))
        return false;
    return true;
}

private String facilityGroupCode;
private String facilityCode;
private String unitCode;

public void setFacilityGroupCode (String facilityGroupCode) {
    this.facilityGroupCode = facilityGroupCode;
}

public void setFacilityCode (String facilityCode) {
    this.facilityCode = facilityCode;
}

public void setUnitCode (String unitCode) {
    this.unitCode = unitCode;
}

public String getFacilityGroupCode () {
    return facilityGroupCode;
}

public String getFacilityCode () {
    return facilityCode;
}

public String getUnitCode () {
    return unitCode;
}

@Override
public int compareTo(ThreePartKey o) {
    return  (100*(unitCode.compareTo(o.unitCode))) + 
            (10*(facilityCode.compareTo(o.unitCode))) + 
            (facilityGroupCode.compareTo(o.facilityGroupCode));
}

}

这是我创建表格的代码:

public void setUpTable () {
   for (DateTime day : uniqueDays) {
     for (ThreePartKey unit : trueUniqueUnits) {
        UnitDay unitDay = new UnitDay();
        String unitCode = unit.getUnitCode();
        unitDay.setDay(day);
        unitDay.setUnitCode(unit.getUnitCode());
        unitDay.setFacilityCode(unit.getFacilityCode());
        unitDay.setFacilityGroupCode(unit.getFacilityGroupCode());
        table.put(unit, day, unitDay);
     }
   }
}


public void populateTable () { //FIX THIS TO INCLUDE ThreePartKey
    for (CensusHour hour : allHours) {
        DateTime minHour = hour.getDateTaken().withTimeAtStartOfDay();
        DateTime day = hour.getDateTaken().withTimeAtStartOfDay();
        String unit = hour.getUnitCode();
        ThreePartKey key = new ThreePartKey();
        key.setFacilityCode(hour.getFacilityCode());
        key.setFacilityGroupCode(hour.getFacilityGroupCode());
        key.setUnitCode(hour.getUnitCode());

        UnitDay tempUnitDay = table.get(key, day);

        tempUnitDay.setFacilityId(hour.getFacilityId());
        tempUnitDay.setFacilityName(hour.getFacilityName());
        tempUnitDay.setUnitId(hour.getUnitId());
        tempUnitDay.setUnitName(hour.getUnitName());
        tempUnitDay.setPortalCensusFileId(hour.getPortalCensusFileId());
        tempUnitDay.setDay(minHour);
        tempUnitDay.setFacilityCode(hour.getFacilityCode());
        tempUnitDay.setFacilityGroupCode(hour.getFacilityGroupCode());
        tempUnitDay.setFacilityGroupName(hour.getFacilityGroupName());


        int objectHour = hour.getHourOfDay();
        double occupancy = hour.getOccupancy();

        switch (objectHour) {
        case 0: tempUnitDay.setCensusHour0(occupancy);
                break;
        case 1: tempUnitDay.setCensusHour1(occupancy);
                break;
        case 2: tempUnitDay.setCensusHour2(occupancy);
                break;
        case 3: tempUnitDay.setCensusHour3(occupancy);
                break;
        case 4: tempUnitDay.setCensusHour4(occupancy);
                break;
        case 5: tempUnitDay.setCensusHour5(occupancy);
                break;
        case 6: tempUnitDay.setCensusHour6(occupancy);
                break;
        case 7: tempUnitDay.setCensusHour7(occupancy);
                break;
        case 8: tempUnitDay.setCensusHour8(occupancy);
                break;
        case 9: tempUnitDay.setCensusHour9(occupancy);
                break;
        case 10: tempUnitDay.setCensusHour10(occupancy);
                break;
        case 11: tempUnitDay.setCensusHour11(occupancy);
                break;
        case 12: tempUnitDay.setCensusHour12(occupancy);
                break;
        case 13: tempUnitDay.setCensusHour13(occupancy);
                break;
        case 14: tempUnitDay.setCensusHour14(occupancy);
                break;
        case 15: tempUnitDay.setCensusHour15(occupancy);
                break;
        case 16: tempUnitDay.setCensusHour16(occupancy);
                break;
        case 17: tempUnitDay.setCensusHour17(occupancy);
                break;
        case 18: tempUnitDay.setCensusHour18(occupancy);
                break;
        case 19: tempUnitDay.setCensusHour19(occupancy);
                break;
        case 20: tempUnitDay.setCensusHour20(occupancy);
                break;
        case 21: tempUnitDay.setCensusHour21(occupancy);
                break;
        case 22: tempUnitDay.setCensusHour22(occupancy);
                break;
        case 23: tempUnitDay.setCensusHour23(occupancy);
                break;
        }       
        table.put(key, day, tempUnitDay);
    }
}

0 个答案:

没有答案