@Override public void updateBookmark(Bookmark bookmark) { String id = Objects.requireNonNull(bookmark).getId(); if (null == id || id.isEmpty()) { throw new IllegalArgumentException(); } // check if there is no different bookmark with this url Bookmark found = bookmarks.get(bookmark.getUrl()); if (null != found && !id.equals(found.getId())) { throw new AlreadyExistsException("bookmark with url " + bookmark.getUrl()); } deleteBookmark(id); bookmarks.put(bookmark.getUrl(), bookmark); }
@Override public Bookmark createBookmark(Bookmark bookmark) { if (null == bookmark) { throw new IllegalArgumentException("bookmark is null"); } if (null != bookmark.getId()) { throw new IllegalArgumentException("is is not null"); } if (null == bookmark.getUrl() || bookmark.getUrl().isEmpty()) { throw new IllegalArgumentException("bookmark url is not set"); } if (bookmarks.containsKey(bookmark.getUrl())) { throw new AlreadyExistsException("bookmark with url: " + bookmark.getUrl()); } bookmark.setId(String.valueOf(nextId.getAndIncrement())); bookmarks.put(bookmark.getUrl(), bookmark.clone()); return bookmark; }
/** * deletes the bookmark with the given id * * @param id id of the bookmark to delete * @throws NotFoundException if no bookmark is found for the given id */ @Override public void deleteBookmark(String id) { Bookmark bookmark = getBookmarkById(id); String url = bookmark.getUrl(); bookmarks.remove(url); }