commit 44: adc0b4871fe8
parent 43: b4029fbec56f
branch: default
tags: tip
[am, ap, pairwithus] New Behaviour: Clerk can fill in details on any object, Clerk will complain when method does not exist, and when the Object being filled in complains. S02E10 S02E11
AntonyMarcano
6 months ago
r44:adc0b4871fe8 59 loc 2.2 KB embed / history / annotate / raw /
package org.jnarrate.fit.fixture.expertise;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jnarrate.action.ActorPlayingThe;
import org.jnarrate.expertise.Librarian;
import org.jnarrate.fit.fixture.entityname.CharacterName;
import org.jnarrate.fit.fixture.entityname.Demographic;
import org.jnarrate.fit.fixture.entityname.RoleName;
import org.jnarrate.fit.fixture.expertise.misuse.UncastCharacterComplaint;
import org.jnarrate.fit.fixture.expertise.misuse.UnspecifiedDemographicComplaint;

public class CastingDirector {

  private final Librarian librarian;
  private List<Demographic> theseDemographics = new ArrayList<Demographic>();
  private final Map<CharacterName,ActorPlayingThe<?>> dressingRoom;

  public CastingDirector() {
    this(new Librarian(), new HashMap<CharacterName,ActorPlayingThe<?>>() );
  }
  
  public CastingDirector(Librarian librarian, Map<CharacterName,ActorPlayingThe<?>> dressingRoom) {
    this.librarian = librarian;
    this.dressingRoom = dressingRoom;
  }

  public void theActorsAreFrom(List<Demographic> theseDemographics) {
    this.theseDemographics = theseDemographics;
  }
  
  public void castThe(CharacterName characterName, RoleName roleName) {
    if(weDontKnowWhereToFindTheActors()) {throw new UnspecifiedDemographicComplaint();}
    dressingRoom.put(characterName, ActorPlayingThe.roleInTheCharacterOf(the(roleName, theseDemographics)));
  }
  
  @SuppressWarnings("unchecked") // because the ROLE gets checked when the actor gets into the action
  public <ROLE> ActorPlayingThe<ROLE> whoIsPlaying(CharacterName characterName) {
    ActorPlayingThe<ROLE> actor = (ActorPlayingThe<ROLE>) dressingRoom.get(characterName);
    if(actor == null) {throw new UncastCharacterComplaint(characterName);}
    return actor;
  }

  public void thatsAWrap() {
    for (ActorPlayingThe<?> actor : dressingRoom.values()) {
      actor.goHome();
    }
  }
  
  private <T> T the(RoleName roleName, List<Demographic> fromTheseDemographics) {
    return librarian.findMeA(roleName, fromTheseDemographics);
  }

  private boolean weDontKnowWhereToFindTheActors() {
    return theseDemographics.size() < 1;
  }
}