/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.inmemory.vehicle;

import java.util.List;

import org.nakedobjects.applib.AbstractFactoryAndRepository;
import org.nakedobjects.applib.Filter;
import org.nakedobjects.applib.annotation.Exploration;
import org.nakedobjects.applib.annotation.MemberOrder;

import com.pragprog.dhnako.carserv.dom.vehicle.Model;
import com.pragprog.dhnako.carserv.dom.vehicle.RegistrationNumber;
import com.pragprog.dhnako.carserv.dom.vehicle.Vehicle;
import com.pragprog.dhnako.carserv.dom.vehicle.VehicleRepository;

public class VehicleRepositoryInMemory 
		extends AbstractFactoryAndRepository 
		implements VehicleRepository {


	// {{ Identification
	public String iconName() {
		return "Vehicle";
	}
	// }}


	// {{ FindByRegistrationNumber
	@MemberOrder(sequence = "1")
	public Vehicle findByRegistrationNumber(
	        final RegistrationNumber registrationNumber) {
	    return firstMatch(Vehicle.class, new Filter<Vehicle>() {
	        public boolean accept(final Vehicle vehicle) {
	            return vehicle.getRegistrationNumber().equals(registrationNumber);
	        }});
	}
	// }}

	
	// {{ find Vehicles Without Recent Service
	@MemberOrder(sequence = "2")
    public List<Vehicle> findVehiclesWithoutRecentService(
    		final Model model) {
        return allMatches(Vehicle.class, new Filter<Vehicle>() {
            public boolean accept(Vehicle vehicle) {
                if (vehicle.getModel() != model) return false;
                return vehicle.isServiceOverdue();
            }});
    }
	// }}

	
	
    // {{ all Vehicles
	@Exploration
	@MemberOrder(sequence = "3")
	public List<Vehicle> allVehicles() {
		return allInstances(Vehicle.class);
	}
	// }}

}