1

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.

anirban
  • 674
  • 1
  • 7
  • 21
  • 1
    `I have an Item object having 4 String fields and 3 boolean fields.` so the booleans are fields of your object, but you want to create the object depends on it's fields? The object is already there, isn't it? – Kent Dec 28 '15 at 12:44
  • Yes the booleans are part of that Item object which I want to create. Basically the 7 values I get from different areas, but I will create that Item object only if atleast one of the boolean of that to be constructed Item object is going to be true. This booleans also get calculated after some validation which I get out of an COR(chain of resp). – anirban Dec 28 '15 at 12:47
  • Report.buildReport() will be called once there is at least one value in enumSet, ie there is one boolean variable true. at the same time, I am free to add more boolean type to InconsistencyReport enum, and for that I don't have to change in calculateInconsitencyReport() logic. Only add the enum, and do a setter in BuildReport and create a boolean in Report class. – anirban Dec 28 '15 at 13:55

2 Answers2

1

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);
}
Neil Masson
  • 2,609
  • 1
  • 15
  • 23
  • The problem is there might need to add boolean in future then I have to touch this object constructed again. And further I am not having those bool1, bool2, bool3 at one go. I am checking the boolean values in a loop. So, If I go by your design I have to check everytime whether any one boolean is true is not, if true then whether I already constructe the object or not in previous loop iteration. If yes then setBool, else create object then setbool. Which will solve the purpose, but Is there anything better ? Why I cant think of any good pattern ? – anirban Dec 28 '15 at 12:51
0

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

Feillen
  • 109
  • 9
  • Well thanks for your thought. Yes your code will work, but I want something flexible. I achieved what I wanted, you can check that on edited question. – anirban Dec 28 '15 at 13:53