stuff
This commit is contained in:
parent
7fbc3f722c
commit
22a9368697
12 changed files with 228 additions and 144 deletions
|
@ -2,6 +2,7 @@ package link;
|
|||
|
||||
import category.CategoryId;
|
||||
import tag.Tag;
|
||||
import tag.TagName;
|
||||
import user.Username;
|
||||
|
||||
import java.util.Set;
|
||||
|
@ -34,10 +35,22 @@ public class Link {
|
|||
return url;
|
||||
}
|
||||
|
||||
public boolean hasCategoryId(CategoryId categoryId) {
|
||||
return categoryIds.contains(categoryId);
|
||||
}
|
||||
|
||||
public Set<CategoryId> getCategoryIds() {
|
||||
return categoryIds;
|
||||
}
|
||||
|
||||
public boolean hasTagName(TagName tagName) {
|
||||
return tags.stream().anyMatch(tag -> tag.getName().equals(tagName));
|
||||
}
|
||||
|
||||
public boolean wasCreatedBy(Username username) {
|
||||
return this.creator.equals(username);
|
||||
}
|
||||
|
||||
public Set<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
|
|
@ -2,76 +2,100 @@ package link;
|
|||
|
||||
import abstraction.CustomSetPersistenceDecorator;
|
||||
import abstraction.PersistenceAdapter;
|
||||
import category.CategoryId;
|
||||
import datastructures.set.CustomSet;
|
||||
import exeptions.LinkAlreadyExists;
|
||||
import exeptions.LinkDoesNotExist;
|
||||
import tag.TagName;
|
||||
import user.Username;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LinkRepository {
|
||||
|
||||
final private CustomSet<Link> links;
|
||||
final private CustomSet<Link> links;
|
||||
|
||||
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
|
||||
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
|
||||
}
|
||||
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
|
||||
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
|
||||
}
|
||||
|
||||
public Optional<Link> getById(LinkId id) {
|
||||
return links.find(link -> link.getId().equals(id));
|
||||
}
|
||||
public Optional<Link> getById(LinkId id) {
|
||||
return links.find(link -> link.getId().equals(id));
|
||||
}
|
||||
|
||||
public Optional<Link> getByUrl(LinkUrl url) {
|
||||
return links.find(link -> link.getUrl().equals(url));
|
||||
}
|
||||
public Optional<Link> getByUrl(LinkUrl url) {
|
||||
return links.find(link -> link.getUrl().equals(url));
|
||||
}
|
||||
|
||||
public void removeById(LinkId id) {
|
||||
getById(id).ifPresentOrElse(this::remove, () -> {
|
||||
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
|
||||
});
|
||||
}
|
||||
public void removeById(LinkId id) {
|
||||
getById(id).ifPresentOrElse(this::remove, () -> {
|
||||
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
|
||||
});
|
||||
}
|
||||
|
||||
public void removeByUrl(LinkUrl url) {
|
||||
getByUrl(url).ifPresentOrElse(this::remove, () -> {
|
||||
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
|
||||
});
|
||||
}
|
||||
public void removeByUrl(LinkUrl url) {
|
||||
getByUrl(url).ifPresentOrElse(this::remove, () -> {
|
||||
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
|
||||
});
|
||||
}
|
||||
|
||||
public void add(Link link) {
|
||||
checkDuplicates(link);
|
||||
links.add(link);
|
||||
}
|
||||
public void add(Link link) {
|
||||
checkDuplicates(link);
|
||||
links.add(link);
|
||||
}
|
||||
|
||||
public Set<Link> getAll() {
|
||||
return links.getSet();
|
||||
}
|
||||
public Set<Link> getAll() {
|
||||
return links.getSet();
|
||||
}
|
||||
|
||||
private void remove(Link link) {
|
||||
links.remove(link);
|
||||
}
|
||||
private void remove(Link link) {
|
||||
links.remove(link);
|
||||
}
|
||||
|
||||
private void checkDuplicates(Link newLink) {
|
||||
if (this.getById(newLink.getId()).isPresent()) {
|
||||
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
|
||||
}
|
||||
private void checkDuplicates(Link newLink) {
|
||||
if (this.getById(newLink.getId()).isPresent()) {
|
||||
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
|
||||
}
|
||||
|
||||
if (this.getByUrl(newLink.getUrl()).isPresent()) {
|
||||
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
|
||||
}
|
||||
}
|
||||
if (this.getByUrl(newLink.getUrl()).isPresent()) {
|
||||
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Link> getByCategory(int id) {
|
||||
return links.stream().filter(it -> it.getCategoryIds().contains(id)).collect(Collectors.toSet());
|
||||
}
|
||||
public Set<Link> getByCategory(CategoryId id) {
|
||||
return links.stream().filter(link -> link.hasCategoryId(id)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Set<Link> getByTag(TagName tagName) {
|
||||
return links.stream().filter(it -> it.getTags().stream().anyMatch(tag -> tag.getName().equals(tagName))).collect(Collectors.toSet());
|
||||
}
|
||||
public Set<Link> getByTag(TagName tagName) {
|
||||
return links.stream().filter(link -> link.hasTagName(tagName)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Set<Link> getByUser(Username username) {
|
||||
return links.stream().filter(it -> it.getCreator().equals(username)).collect(Collectors.toSet());
|
||||
}
|
||||
public Set<Link> getByUser(Username username) {
|
||||
return links.stream().filter(link -> link.wasCreatedBy(username)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public List<Link> groupByHost() {
|
||||
var input = new ArrayList<>(links.stream().toList());
|
||||
var result = new ArrayList<Link>();
|
||||
|
||||
boolean changed = false;
|
||||
while (!input.isEmpty()) {
|
||||
input.removeAll(result);
|
||||
changed = false;
|
||||
for (Link link : input) {
|
||||
if (result.stream().anyMatch(resultLink -> resultLink.getUrl().hostEquals(link.getUrl()))) {
|
||||
result.add(link);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (!changed && !input.isEmpty()) {
|
||||
result.add(input.get(0));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,25 @@ public class LinkUrl {
|
|||
return url;
|
||||
}
|
||||
|
||||
public boolean hostEquals(LinkUrl other) {
|
||||
return this.url.getHost().equals(other.getUrl().getHost());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
LinkUrl linkUrl = (LinkUrl) o;
|
||||
|
||||
return url != null ? url.toString().equals(linkUrl.url.toString()) : linkUrl.url == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return url != null ? url.toString().hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return url.toString();
|
||||
|
|
Reference in a new issue