/** * @param <T> either a HalResource or a HalLink * @param name the field name * @param res the resource to add to the representation * @param rs * @param multiRs */ private <T extends LinkOrResource> void add( String name, T res, Map<String, T> rs, Map<String, List<T>> multiRs) { if (res == null && ignoreNullResources) return; require(res != null, "Cannot add a null linked or embedded resource"); if (multiRs.containsKey(name)) { multiRs.get(name).add(res); } else if (rs.containsKey(name)) { List<T> ls = new LinkedList<>(); ls.add(rs.remove(name)); ls.add(res); multiRs.put(name, ls); } else { rs.put(name, res); } }
private <T extends LinkOrResource> void addMulti( String name, Collection<? extends T> res, Map<String, T> rs, Map<String, List<T>> multiRs) { List<T> resource = res == null ? new LinkedList<>() : new LinkedList<>(res); if (rs.containsKey(name)) resource.add(rs.get(name)); List<T> links = multiRs.get(name); if (links == null) { multiRs.put(name, resource); } else { links.addAll(resource); } }