anirban
anirban

Reputation: 704

How to create object based on boolean condition?

I have an Item object having 4 String fields and 3 boolean fields. I have to construct this object based on the 3 boolean variables. The target is whenever any one of the boolean variable is true we have to create the object having that/those boolean variable set. If for any situation none of the boolean variables are true, we wont create the object. I am using a COR to check whether any of the boolean fields will be set or not based on some business logic. I was trying this with builder, but then I have to construct so many objects and later discard them when none of the boolean variables found true.

Can anyone have any better idea, to solve this kind of problem ?

Well thanks for the 2 delete flag for this question. Thank for the thoughts on this question as well. I did something to achieve what I want. Which is quite flexible I believe. Only part if there is a dependency on If loop, but that is acceptable since Report class can have extra boolean so when that class is changed, it's builder should be touched to cater that change. Rest this is flexible which I wanted. public class Report {

    private String acftNo;
    private Date plannedDate;
    private String plannedStn;
    private Integer mntncId;
    private Set<String> capableStations;
    private String routedStn;
    private boolean isRoutedNEQPlannedStn;  //Inconsistency     type 1
    private boolean isCapableAtPlannedStn;  //Inconsistency     type 2 
    private boolean isPlannedOrRoutedStationExists;  //Inconsistency     type 3/5   

    public Report(String acftNo, Integer mntncId) {
        super();
        this.acftNo = acftNo;
        this.mntncId = mntncId;
    }

    public Report(String acftNo, Date plannedDate, String plannedStn,
            Integer mntncId) {
        super();
        this.acftNo = acftNo;
        this.plannedDate = plannedDate;
        this.plannedStn = plannedStn;
        this.mntncId = mntncId;
    }

    //setters and getters. Removed for space.

    public static Report buildReport(Maintenance<?> task, Set<InconsistencyReport> enumSet) {
        Report temp = new Report(task.getAssignment().getAircraftNumber(),task.getAssignment().getMntncScheduleDate(),
                task.getAssignment().getStationCode(),task.getAssignment().getMntncId());
        temp.setCapableStations(InconsistencyReport.getCapableStations(task));
        for(InconsistencyReport ir : enumSet)
        {
            if(ir.compareTo(InconsistencyReport.ROUTED_STN_NEQ_PLANNED_STN)==0)
                temp.setRoutedNEQPlannedStn(true);
            if(ir.compareTo(InconsistencyReport.ITEM_NT_CAPABLE_AT_PLANNED_STN)==0)
                temp.setCapableAtPlannedStn(true);
            if(ir.compareTo(InconsistencyReport.NO_ROUTD_STN_ON_A_DATE)==0)
                temp.setPlannedOrRoutedStationExists(true);
        }
        return temp;
    }
}

calculateInconsitencyReport() method which will decide whether to create object or not.

public class InconsistencyReportChain {

    public enum InconsistencyReport implements InconsistencyReportIface {

        ROUTED_STN_NEQ_PLANNED_STN  {
            @Override
            public boolean findInconsistency(Maintenance<?> task ) {
                if(!validate(task))
                    return false;
                //some logic 
                    return true;
                return false;
            }
        },
        ITEM_NT_CAPABLE_AT_PLANNED_STN  {
            @Override
            public boolean findInconsistency(Maintenance<?> task) {
                if(!validate(task))
                    return false;
                //some logic
                    return true;
                return false;
            }
        },
        NO_ROUTD_STN_ON_A_DATE  {
            @Override
            public boolean findInconsistency(Maintenance<?> task) {
                if(!validate(task))
                    return false;
                //some logic 
                    return true
                return false; 
            }
        };

        @Override
        public boolean validate(Maintenance<?> task) {
            return !(null == task.getAssignment());
        }

        static Set<String> getCapableStations(Maintenance<?> task)
        {
            Set<String> capableStations = newHashSet();
            if(task.getCapStationList() != null)
            {
                capableStations.addAll(Arrays.asList(task.getCapStationList().split(StringConstants.COMMA_SPLIT_REGEX)));
            }
            if(task.getCapStationClassList() != null)
            {
                Map<String, List<String>> stationClassMap = CacheManager.get(STN_CLASS.name());
                List<String> stationClass = Arrays.asList(task.getCapStationClassList().split(StringConstants.COMMA_SPLIT_REGEX));
                for(String stnClass : stationClass)
                {
                    capableStations.addAll(stationClassMap.get(stnClass));
                }
            }
            return capableStations;
        }
    }

    public static Report calculateInconsitencyReport(Maintenance<?> task)   {
        Set<InconsistencyReport> enumSet = null;
        for(InconsistencyReport iReport : InconsistencyReport.values())
        {
            if(iReport.findInconsistency(task))
            {
                if(null==enumSet)
                    enumSet = EnumSet.of(iReport);
                else
                    enumSet.add(iReport);
            }
        }
        if(null!= enumSet && enumSet.size() > 0)
            return Report.buildReport(task,enumSet);
        return null;
    }
}

Helper Interface:

public interface InconsistencyReportIface {

    public boolean findInconsistency(Maintenance<?> task );

    public boolean validate(Maintenance<?> task );

}

Details of class logic is teared off because of security.

Upvotes: 1

Views: 2529

Answers (2)

Feillen
Feillen

Reputation: 129

From what I understand of your description:

a) you will have some bools that will determine wether you create a certain object or not.

b) you may have to include some more bools into the "check protocol"

c) you have to do this checking in a loop where

i/ you check for the bool variable

ii/ you check if the object had been created previously

I still don't quite get it yet, but.. that looks pretty straight forward to me. Let's say your bools are stored in a boolean array boolean[] bools and your strings in a string array String[] strings (which, btw, I don't know what they are used for). You are saying to check if every bool is true and then create an object based on that result.

boolean[] bools = new boolean[] { ... };
String[] strings = new String[] { ... };
boolean checks = false;
for(int i = 0; i<bools.length && !checks; i++)
    checks = bools[i];
//so far we will have processed if any of the bools was false, which was your condition
if(checks)
    Object object = new Object(); //create your desired object

I don't understand why you would need to check if the object has been constructed previously, though, so I didn't include it in my suggestion :P

Upvotes: 0

Neil Masson
Neil Masson

Reputation: 2689

What is the problem? Just create your object when one of your booleans is true.

if(bool1 || bool2 || bool3) {
    item = new Item(str1, str2, str3, str4, bool1, bool2, bool3);
}

Upvotes: 1

Related Questions