Skip to content
This repository has been archived by the owner on Aug 5, 2023. It is now read-only.

polish whitespace and warnings #1

Merged
merged 1 commit into from
Oct 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
bin
build
*~
gradle.properties
.gradle/
.gradle
.classpath
.project
.settings
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package org.springsource.samples.montyhall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = "org.springsource.samples.montyhall",
excludeFilters = {@Filter(Controller.class)})
public class AppConfig {


}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class GameController {

@Autowired
public GameController(GameRepository repo,
GameResourceAssembler assembler,
DoorResourceAssembler doorAssembler,
DoorsResourceAssembler doorsAssembler,
HistoryResourceAssembler historyAssembler,
ClickStreamService clicks) {
GameResourceAssembler assembler,
DoorResourceAssembler doorAssembler,
DoorsResourceAssembler doorsAssembler,
HistoryResourceAssembler historyAssembler,
ClickStreamService clicks) {
this.repository = repo;
this.gameResourceAssembler = assembler;
this.doorResourceAssembler = doorAssembler;
Expand All @@ -58,7 +58,7 @@ public GameController(GameRepository repo,
}

@RequestMapping(method=RequestMethod.POST)
public HttpEntity createGame() {
public HttpEntity<GameResource> createGame() {
Game game = new Game();
this.repository.storeGame(game);
HttpHeaders headers = new HttpHeaders();
Expand All @@ -73,7 +73,7 @@ public HttpEntity<GameResource> show(@PathVariable Long id) {
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<GameResource>(headers, HttpStatus.NOT_FOUND);
}

GameResource resource = this.gameResourceAssembler.toResource(game);
return new ResponseEntity<GameResource>(resource, HttpStatus.OK);
}
Expand All @@ -85,7 +85,7 @@ public HttpEntity<DoorsResource> showDoors(@PathVariable Long id) {
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<DoorsResource>(headers, HttpStatus.NOT_FOUND);
}

DoorsResource resource = this.doorsResourceAssembler.toResource(game);
return new ResponseEntity<DoorsResource>(resource, HttpStatus.OK);
}
Expand All @@ -97,24 +97,24 @@ public HttpEntity<DoorResource> showDoor(@PathVariable Long gameId, @PathVariabl
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<DoorResource>(headers, HttpStatus.NOT_FOUND);
}

Door door = game.getDoors().get((int)(doorId - 1));
DoorResource resource = this.doorResourceAssembler.toResource(game,door);
return new ResponseEntity<DoorResource>(resource, HttpStatus.OK);
}

@RequestMapping(value="/{gameId}/doors/{doorId}", method=RequestMethod.PUT)
public HttpEntity<DoorResource> updateDoor(@PathVariable Long gameId,
@PathVariable Long doorId,
@RequestBody DoorStatusResource doorResource) {
@PathVariable Long doorId,
@RequestBody DoorStatusResource doorResource) {
Game game = this.repository.findById(gameId);
if (game == null || doorId < 1 || doorId > 3) {
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<DoorResource>(headers, HttpStatus.NOT_FOUND);
}

Door door = game.getDoors().get((int)(doorId - 1));

try {
if (doorResource.status == DoorStatus.SELECTED) {
game.select(door);
Expand All @@ -126,7 +126,7 @@ public HttpEntity<DoorResource> updateDoor(@PathVariable Long gameId,
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<DoorResource>(headers, HttpStatus.CONFLICT);
}

DoorResource resource = this.doorResourceAssembler.toResource(game,door);
return new ResponseEntity<DoorResource>(resource, HttpStatus.OK);
}
Expand All @@ -138,35 +138,35 @@ public HttpEntity<HistoryResource> showHistory(@PathVariable Long id) {
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<HistoryResource>(headers, HttpStatus.NOT_FOUND);
}

HistoryResource resource = this.historyResourceAssembler.toResource(game);
return new ResponseEntity<HistoryResource>(resource, HttpStatus.OK);
}


/** and a handler method for the click stream data */
@RequestMapping(value="/{id}/clicks", method=RequestMethod.POST)
public HttpEntity recordClickStreamData(@PathVariable Long id,
@RequestBody ClickStreamResource clicks) {
public HttpEntity<GameResource> recordClickStreamData(@PathVariable Long id,
@RequestBody ClickStreamResource clicks) {
Game game = this.repository.findById(id);
if (game == null) {
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<GameResource>(headers, HttpStatus.NOT_FOUND);
}

this.clickStreamService.recordClickStream(game,clicks);

HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<GameResource>(headers, HttpStatus.CREATED);
}

/** OPTIONS processing for CORS */
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(value="/**", method=RequestMethod.OPTIONS)
public HttpEntity handleOptionsRequest() {
// a CORS preflight request will be handled by our interceptor
HttpHeaders headers = new HttpHeaders();
headers.add("Allow","GET, HEAD, POST, PUT, OPTIONS");
return new ResponseEntity(headers,HttpStatus.OK);
return new ResponseEntity(headers, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.hateoas.Identifiable;

public class Door implements Identifiable<Integer> {

private final Prize prize;
private final int id;
private DoorStatus status = DoorStatus.CLOSED;
Expand Down Expand Up @@ -34,16 +35,16 @@ public void setStatus(DoorStatus status) {
break;
case SELECTED :
if (this.status == DoorStatus.OPENED) {
illegalTransitionAttempted = true;
illegalTransitionAttempted = true;
}
break;
break;
case OPENED :
break;
}
if (illegalTransitionAttempted) {
throw new IllegalStateException("Cannot transition to " + status + " from " + this.status);
}
this.status = status;
this.status = status;
}

public void reveal() {
Expand Down
94 changes: 48 additions & 46 deletions src/main/java/org/springsource/samples/montyhall/domain/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class Game implements Identifiable<Long> {
private Door[] doors = new Door[3];
private List<GameEvent> history = new ArrayList<GameEvent>();


public Game() {
int doorWithJuergen = new Random().nextInt(3);
doors[0] = (doorWithJuergen == 0) ? new Door(1,Prize.JUERGEN) : new Door(1,Prize.SMALL_FURRY_ANIMAL);
Expand Down Expand Up @@ -45,55 +44,58 @@ public List<GameEvent> getHistory() {
return this.history;
}


/** Select a door, and return the door opened by the host */
public Door select(Door door) {
if (this.status != GameStatus.AWAITING_INITIAL_SELECTION) {
throw new IllegalStateException("Can't select a door from state " + this.status);
}

Door toSelect = this.doors[door.getId() - 1];
toSelect.setStatus(DoorStatus.SELECTED);
GameEvent selectEvent = door.getId() == 1 ? GameEvent.SELECTED_DOOR_ONE
: (door.getId() == 2 ? GameEvent.SELECTED_DOOR_TWO : GameEvent.SELECTED_DOOR_THREE);
this.history.add(selectEvent);

int doorToReveal = new Random().nextInt(3);
while ( (this.doors[doorToReveal] == toSelect) || (this.doors[doorToReveal].getPrize() == Prize.JUERGEN) ) {
doorToReveal = new Random().nextInt(3);
}
Door toReveal = this.doors[doorToReveal];
toReveal.reveal();
GameEvent revealEvent = doorToReveal == 0 ? GameEvent.REVEALED_DOOR_ONE
: (doorToReveal == 1 ? GameEvent.REVEALED_DOOR_TWO : GameEvent.REVEALED_DOOR_THREE);
this.history.add(revealEvent);
this.status = GameStatus.AWAITING_FINAL_SELECTION;
return toReveal;
if (this.status != GameStatus.AWAITING_INITIAL_SELECTION) {
throw new IllegalStateException("Can't select a door from state " + this.status);
}

Door toSelect = this.doors[door.getId() - 1];
toSelect.setStatus(DoorStatus.SELECTED);
GameEvent selectEvent = door.getId() == 1
? GameEvent.SELECTED_DOOR_ONE
: (door.getId() == 2 ? GameEvent.SELECTED_DOOR_TWO : GameEvent.SELECTED_DOOR_THREE);
this.history.add(selectEvent);

int doorToReveal = new Random().nextInt(3);
while ( (this.doors[doorToReveal] == toSelect) || (this.doors[doorToReveal].getPrize() == Prize.JUERGEN) ) {
doorToReveal = new Random().nextInt(3);
}
Door toReveal = this.doors[doorToReveal];
toReveal.reveal();
GameEvent revealEvent = doorToReveal == 0
? GameEvent.REVEALED_DOOR_ONE
: (doorToReveal == 1 ? GameEvent.REVEALED_DOOR_TWO : GameEvent.REVEALED_DOOR_THREE);
this.history.add(revealEvent);
this.status = GameStatus.AWAITING_FINAL_SELECTION;
return toReveal;
}

public GameStatus open(Door door) {
if (this.status != GameStatus.AWAITING_FINAL_SELECTION) {
throw new IllegalStateException("Can't open a door from state " + this.status);
}
Door toOpen = this.doors[door.getId() -1];
toOpen.setStatus(DoorStatus.OPENED);
Prize prize = toOpen.getPrize();
if (prize == Prize.JUERGEN) {
this.status = GameStatus.WON;
}
else {
this.status = GameStatus.LOST;
}

GameEvent openEvent = (door.getId() == 1 ? GameEvent.OPENED_DOOR_ONE
: (door.getId() == 2 ? GameEvent.OPENED_DOOR_TWO : GameEvent.OPENED_DOOR_THREE));
this.history.add(openEvent);
if (this.status == GameStatus.WON) {
this.history.add(GameEvent.WON);
} else {
this.history.add(GameEvent.LOST);
}

return this.status;
if (this.status != GameStatus.AWAITING_FINAL_SELECTION) {
throw new IllegalStateException("Can't open a door from state " + this.status);
}
Door toOpen = this.doors[door.getId() -1];
toOpen.setStatus(DoorStatus.OPENED);
Prize prize = toOpen.getPrize();
if (prize == Prize.JUERGEN) {
this.status = GameStatus.WON;
}
else {
this.status = GameStatus.LOST;
}

GameEvent openEvent = (door.getId() == 1
? GameEvent.OPENED_DOOR_ONE
: (door.getId() == 2 ? GameEvent.OPENED_DOOR_TWO : GameEvent.OPENED_DOOR_THREE));
this.history.add(openEvent);
if (this.status == GameStatus.WON) {
this.history.add(GameEvent.WON);
} else {
this.history.add(GameEvent.LOST);
}

return this.status;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ public class InMemoryGameRepository implements GameRepository {
private Map<Long,Game> games = Collections.synchronizedMap(new HashMap<Long,Game>());

public Game findById(Long id) {
return this.games.get(id);
return this.games.get(id);
}

public void storeGame(Game game) {
if (game.getId() == null) {
synchronized(games) {
Long newId = new Random().nextLong();
while ((newId <= 0) || this.games.containsKey(newId)) {
newId = new Random().nextLong();
if (game.getId() == null) {
synchronized(games) {
Long newId = new Random().nextLong();
while ((newId <= 0) || this.games.containsKey(newId)) {
newId = new Random().nextLong();
}
game.setId(newId);
games.put(newId,game);
}
}
game.setId(newId);
games.put(newId,game);
}
}
}

}

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.springsource.samples.montyhall.resource;

import org.springframework.hateoas.ResourceSupport;
import org.codehaus.jackson.annotate.JsonProperty;

import org.springsource.samples.montyhall.domain.DoorStatus;

/** Used when mapping incoming POST requests with Click Stream data */
public class ClickStreamResource {

@JsonProperty("data")
public String data;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


public class DoorResource extends ResourceSupport {

@JsonProperty("status")
DoorStatus status;

@JsonProperty("content")
String content;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;
import org.springsource.samples.montyhall.controller.GameController;
import org.springsource.samples.montyhall.domain.Game;
import org.springsource.samples.montyhall.domain.Door;
import org.springsource.samples.montyhall.domain.DoorStatus;
import org.springsource.samples.montyhall.domain.Prize;

@Component
public class DoorResourceAssembler {

public DoorResourceAssembler() {
public DoorResourceAssembler() {
}

public DoorResource toResource(Game game, Door door) {
DoorResource resource = new DoorResource();
resource.status = door.getStatus();
Expand All @@ -27,4 +25,5 @@ public DoorResource toResource(Game game, Door door) {
resource.add(linkTo(GameController.class).slash(game).slash("doors").slash(door).withSelfRel());
return resource;
}

}