/* * Utilities.java * * Created on January 13, 2003, 7:05 PM */ package project1; /** Static utilities involving oracles and omens. * * @author dickey */ public class Utilities { /** Tell which omen is the most favorable. * @param omens an array of omens, some or all of which may be null. * @return The most favorable omen, or null if the array contained no omens. */ public static IOmen chooseBestOmen(IOmen[] omens) { /* Note that this implementation does NOT refer directly to the concrete * omen, only to IOmen. */ int ocount; // scan the array until a non-null value is found, or until array end. for (ocount = 0; ocount < omens.length && omens[ocount] == null; ocount++) { } if (ocount >= omens.length) { //oops, fell of the end of the array -- must be no omens return null; } //We weren't at the end of the array, so must have non-null omen. IOmen bestOmen = omens[ocount]; assert bestOmen != null; //In fact, it will be the first non-null omen in the array. assert ocount == 0 || omens[ocount-1] == null; //Now see if there is a better one in the remainer of the array. for (ocount += 1; ocount < omens.length; ocount++) { IOmen nextOmen = omens[ocount]; if (nextOmen != null) { int comparisonResult = bestOmen.compareTo(nextOmen); if (comparisonResult < 0) { //the new omen is better than the old one bestOmen = omens[ocount]; } } } //end for loop return bestOmen; //might be null //end chooseBestOmen } //end utilities }