private boolean allHaveSameClassification ( String property , Iterable < State > examples ) { // Used to hold the value of the first attribute seen. OrdinalAttribute firstAttribute = null ; // Flag used to indicate that the test passed successfully. boolean success = true ; // Loop over all the examples. for ( State example : examples ) { OrdinalAttribute nextAttribute = ( OrdinalAttribute ) example . getProperty ( property ) ; // If this is the first example just store its attribute value. if ( firstAttribute == null ) { firstAttribute = nextAttribute ; } // Otherwise check if the attribute value does not match the first one in which case the test fails. else if ( ! nextAttribute . equals ( firstAttribute ) ) { success = false ; break ; } } // If the test passed then store the matching classification that all the examples have in a memeber variable // from where it can be accessed. if ( success ) { allClassification = firstAttribute ; } return success ; }
Tests if a property of a set of examples has the same value for all the examples . This algorithm works by iterating through the examples until two different values are found or the end of the collection is reached having found only one value .