Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package su.vistar.drivers.dao.driver; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.PostConstruct; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import su.vistar.drivers.dao.categories.CategoriesDAO; import su.vistar.drivers.dao.exceptions.NotFoundException; import su.vistar.drivers.model.Category; import su.vistar.drivers.model.Driver; import su.vistar.drivers.model.Gender; /** * * @author aleksandr-gorovoi */ @Repository public class DriversMapDAO implements DriversDAO { /** * Logger */ private Log logger = LogFactory.getLog(this.getClass()); private Map<Integer, Driver> driversMap; /** * id */ int next_id = 0; /** * ? ? id */ private int getNextId() { return ++next_id; } @Autowired CategoriesDAO categoriesDAO; public Collection<Driver> getDriversList() { logger.info(" ?? "); return driversMap.values(); } public Driver getDriver(int id) { logger.info(String.format(" ? ? id %d", id)); if (driversMap.containsKey(id)) { return driversMap.get(id); } else { logger.info(String.format(" ? id %d ??", id)); throw new NotFoundException(); } } public Driver addDriver(Driver driver) { logger.info(" ? "); int id = this.getNextId(); driver.setId(id); driversMap.put(id, driver); logger.info(String.format(" , id %d", id)); return driver; } public Driver updateDriver(int id, Driver updated) { logger.info(String.format(" ? ? id %d", id)); Driver oldDriver = this.getDriver(id); oldDriver.setBirthdate(updated.getBirthdate()); oldDriver.setCategory(updated.getCategory()); oldDriver.setGender(updated.getGender()); oldDriver.setName(updated.getName()); oldDriver.setPatronimyc(updated.getPatronimyc()); oldDriver.setSurname(updated.getSurname()); return oldDriver; } public void deleteDriver(int id) { logger.info(String.format(" ? ? id %d ", id)); if (driversMap.containsKey(id)) { driversMap.remove(id); logger.info(String.format("? ? id %d ", id)); } else { logger.info(String.format(" ? id %d ??", id)); throw new NotFoundException(); } } private int randomInt(int max) { return (int) Math.round(Math.random() * max); } private void fill(int driverCount) { final String[] surnames = new String[] { "", "", "", "" }; final String[] names = new String[] { "", "", "?", "" }; final String[] patronimycs = new String[] { "", "", "?", "" }; List<Category> allCategories = new ArrayList<>(categoriesDAO.getCategoriesList()); for (int i = 0; i < driverCount; i++) { Driver newDriver = new Driver(); newDriver.setSurname(surnames[randomInt(surnames.length - 1)]); newDriver.setName(names[randomInt(names.length - 1)]); newDriver.setPatronimyc(patronimycs[randomInt(patronimycs.length - 1)]); Date birthdate = new Date(); birthdate.setTime(randomInt(820452203) * 1000L); newDriver.setBirthdate(birthdate); newDriver.setGender(Gender.MALE); newDriver.setCategory(allCategories.get(randomInt(allCategories.size() - 1))); this.addDriver(newDriver); } } @PostConstruct private void init() { fill(10); } public DriversMapDAO() { driversMap = new HashMap<Integer, Driver>(); } }