public QueryResult<Route> queryByCriteria(RouteQueryCriteria criteria, int limit, int offset) { List<Route> routes = new ArrayList<Route>(fakeDb.values()); if (criteria != null) { for (Map.Entry<Integer, Route> entry : fakeDb.entrySet()) { Route route = entry.getValue(); if (criteria.getId() > 0 && criteria.getId() != entry.getKey()) { routes.remove(route); } if (criteria.getState() != null && !criteria.getState().equals(route.getState())) { routes.remove(route); } // 模糊匹配 if (criteria.getName() != null && !route.getName().contains(criteria.getName())) { routes.remove(route); } // 模糊匹配 if (criteria.getIncharge() != null && !route.getIncharge().contains(criteria.getIncharge())) { routes.remove(route); } } } return new QueryResult<Route>(routes, routes.size()); }
public int insert(Route route) { if (queryByName(route.getName()) != null) { throw new RuntimeException("Object already exists"); } route.setId(id.getAndIncrement()); fakeDb.put(route.getId(), route); return route.getId(); }