javabag

How to check that the object isn't the last and how to handle it if it is?


I have a method that cycles through a Bag of geometry objects and gets specific attributes and compares the current attribute to the previous, if there is one, with the intention of storing the highest attribute and then assigning it to another geometry.

I've just realised that at some point I am going to reach the end of that Bag and I need my method to alert me that it has reached the last object in the bag.

How can I identify that the current object is the last in the bag?

int getLargestUnassignedWard() {
        Bag lsoaGeoms = centroidsLayer.getGeometries();

        System.out.println();
        System.out.println("Getting Largest Unassigned Wards!");

        int highestOSVI = -1;
        MasonGeometry myCopy = null;

        for (Object o : lsoaGeoms) {
            MasonGeometry masonGeometry = (MasonGeometry) o;
            int id = masonGeometry.getIntegerAttribute("ID");
            String lsoaID = masonGeometry.getStringAttribute("LSOA_NAME");
            int tempOSVI = masonGeometry.getIntegerAttribute("L_GL_OSVI_");
            Point highestWard = masonGeometry.geometry.getCentroid();
            System.out.println(lsoaID + " - OSVI rating: " + tempOSVI + ", ID: " + id);
            if (assignedWards.contains(id))
                continue;

            // tempOSVI = the attribute in the "L_GL_OSVI_" column - ints
            if (tempOSVI > highestOSVI) { // if temp is higher than highest
                highestOSVI = tempOSVI; // update highest to temp
                myCopy = masonGeometry; // update myCopy, which is a POLYGON
            }
        }

        if (myCopy == null) {
            System.out.println("ALERT: LSOA Baselayer is null!");
            return -1; // no ID to find if myCopy is null, so just return a fake value
        }

        int id = myCopy.getIntegerAttribute("ID"); // Here, id changes to the highestOSVI
        assignedWards.add(id); // add ID to the "assignedWards" ArrayList
        System.out.println("Highest OSVI Raiting is: " + myCopy.getIntegerAttribute("L_GL_OSVI_") + " for LSOA ID: "
                + id + " (" + myCopy.getStringAttribute("LSOA_NAME") + ")");
        System.out.println("Current list of Largest Unassigned Wards: " + assignedWards); // Prints out: the ID for the highestOSVI
        System.out.println();
        return myCopy.getIntegerAttribute("ROAD_ID"); // return Road_ID for the chosen LSOA to visit
    }

Solution

  • I've just realised that at some point I am going to reach the end of that Bag and I need my method to alert me that it has reached the last object in the bag.

    I don't see any reason you need to know that to do what you've said that loop does.

    But if you do need to know that within the loop, you can't use the enhanced for loop; you'll need to use the iterator directly:

    for (Iterator it = lsoaGeoms.iterator(); it.hasNext(); ) {
        MasonGeometry masonGeometry = (MasonGeometry)it.next();
        boolean isLast = !it.hasNext();
        // ...
    }
    

    Side note: I strongly recommend using type parameters rather than raw types and typecasts. For instance, lsoaGeoms would be a Bag<MasonGeometry>, etc.