FileDocCategorySizeDatePackage
PredicatedMapExample.javaAPI DocExample2625Wed May 18 09:39:00 BST 2005com.discursive.jccook.collections.predicate

PredicatedMapExample

public class PredicatedMapExample extends ValidCoachPredicate

Fields Summary
Map
teamMap
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		PredicatedMapExample example = new PredicatedMapExample();
		example.start();
	
public voidstart()

		// Create the Predicates
		ValidTeamPredicate validTeam = new ValidTeamPredicate();
		ValidCoachPredicate validCoach = new ValidCoachPredicate();
		
		// Tie two Predicates together into an AndPredicate
		AndPredicate valuePredicate = new AndPredicate( validTeam, validCoach );
		
		// Decorate a HashMap with a predicate on the value
		teamMap = PredicatedMap.decorate( new HashMap(), null, valuePredicate);
		
		// Manufacture some teams
		Team redSox = new Team( "Red Sox", new Coach( "Patrick", "Moloney") );
		Team yankees= new Team( "Yankees", new Coach( "David", "McGarry") );
		Team dodgers = new Team( "Dodgers", new Coach( "Nick", "Taylor") );
		Team twins = new Team( null, new Coach( "Patrick", "Moloney") );
		Team braves = new Team( "Braves", null );
		
		// The follow put calls should work fine
		teamMap.put( "RedSox", redSox );
		teamMap.put( "Yankees", yankees );
		teamMap.put( "Dodgers", dodgers );
		
		// This put should fail because the team name is null
		try {
			teamMap.put( "Twins", twins);
		} catch( IllegalArgumentException iae ) {
			System.out.println( "Twins put failed, as expected" );
		}

		// This put should fail because the coach is null
		try {
			teamMap.put( "Braves", braves);
		} catch( IllegalArgumentException iae ) {
			System.out.println( "Braves put failed, as expected" );
		}