Question

Allright, I have reached the mindscrew of the century and sat a few hours cogitating this one. I will quickly add an example here:

public class AdUnitSizes {

protected String environmentType;
@XmlElement(name = "size")
protected List<Size> sizes;
protected List<Companions> companions;

Now basically, if I were to structure this as an XML file, it would hypothetically represent something like this (Note, Companions is of type AdUnitSize[])

    <adUnitSizes>
    <size>
        <width>800</width>
        <height>600</height>
        <isAspectRatio>true</isAspectRatio>
    </size>
    <environmentType>BROWSER</environmentType>
    <companions>  <!--adunitsize[]-->
        <size>
            <width>800</width>
            <height>600</height>
            <isAspectRatio>true</isAspectRatio>
        </size>
        <environmentType>BROWSER</environmentType>
        <companions>
            <size>
                <width>800</width>
                <height>600</height>
                <isAspectRatio>true</isAspectRatio>
            </size>
            <environmentType>BROWSER</environmentType>
            <companions>...</companions>    
        </companions>               
    </companions>
</adUnitSizes>

Now, As I am trying to point out, AdUnitSizes contains 3 fields. Sizes, EnvironmentTypes and Companions. What I am trying to do in java now, using the object wrapper I have created (1st example) is to Iterate through each and every child Companions Object, And their children Companions Objects till the iteration can go no further (its unlikely in our system that Companions will go more than 3 levels deep, but I need to cater for this anyway)

This is what I have tried:

    AdUnitSize[] adUnitSizeArray = adUnit.getAdUnitSizes();
    if(adUnitSizeArray != null){
        List<AdUnitSizes> adUnitSizesList = adUnitWrapper.getAdUnitSizes();
        for(int i = 0; i < adUnitSizeArray.length; i++){
            AdUnitSizes adUnitSizes = new AdUnitSizes();

            aem.adservices.google.dfp6.om.Size size = new aem.adservices.google.dfp6.om.Size(); //Name clash with Google Size Class
            size.setHeight(adUnitSizeArray[i].getSize().getHeight());
            size.setWidth(adUnitSizeArray[i].getSize().getWidth());
            size.setIsAspectRatio(adUnitSizeArray[i].getSize().getIsAspectRatio());
            adUnitSizes.getSizes().add(size);

            adUnitSizes.setEnvironmentType(adUnitSizeArray[i].getEnvironmentType().getValue());

            List<Companions> companionsList = adUnitSizes.getCompanions();
            //Need to iterate through all of adUnitSizeArray's Companions, and its companions children and so forth
        }
    }

Any ideas? Am I approaching this wrong?

Was it helpful?

Solution

I'm not sure awhat you're doing with the companyList, but as Dave said, is a trivial exercise of recursion, so you can do:

    //(...)
    List<Companions> companionsList = adUnitSizes.getCompanions();
    iterateOverCompanions(companionsList);
}

private void iterateOverCompanions(List<Companions> companionsList) {
    for(Companions companion : companionsList) {
        //Do stuff with companion
        List<Companions> anotherCompanionList = companion.getCompanionsList();
        if (anotherCompanionList  != null && anotherCompanionList.size()>0) {
            iterateOverCompanions(anotherCompanionList);
        }
    }
}

EDIT: If you need to pass something to populate, you can just pass them as a parameter like:

iterateOverCompanions(List<Companions> companionsList, Size size, AnotherStuff stuff)

or set it as a member variable, if this is ok.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top