Java Reference
In-Depth Information
Collections.unmodifiableCollection(seats.values());
}
@Lock(READ)
public int getSeatPrice(int seatId) throws
NoSuchSeatException {
return getSeat(seatId).getPrice();
}
@Lock(WRITE)
public void buyTicket(int seatId) throws
SeatBookedException, NoSuchSeatException {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new SeatBookedException("Seat " + seatId
+ " already booked!");
}
addSeat(seat.getBookedSeat());
}
@Lock(READ)
private Seat getSeat(int seatId) throws
NoSuchSeatException {
final Seat seat = seats.get(seatId);
if (seat == null) {
throw new NoSuchSeatException("Seat " + seatId
+ " does not exist!");
}
return seat;
}
}
Let's see our application code in detail; the void method setupTheatre is invoked as
soon as the application is deployed and takes care of assembling the theatre seats, creating
a simple map of the Seat objects. Seat identifiers are key factors in this map. This hap-
pens right after deployment because our bean is annotated with @Singleton and
@Startup that force the container to initialize the bean during startup. Each Seat ob-
ject is constructed using a set of three field constructors, which includes the seat ID, its
Search WWH ::




Custom Search