stuff
This commit is contained in:
parent
7fbc3f722c
commit
22a9368697
|
@ -21,7 +21,7 @@ abstract public class Subcommand {
|
||||||
commandExists(args[0]);
|
commandExists(args[0]);
|
||||||
return commands.get(args[0]).apply(args);
|
return commands.get(args[0]).apply(args);
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException e) {
|
catch (IndexOutOfBoundsException | IllegalArgumentException e) {
|
||||||
throw new CliError("Missing a value! " + getUsage());
|
throw new CliError("Missing a value! " + getUsage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class CategoryCommands extends Subcommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUsage() {
|
public String getUsage() {
|
||||||
return "Usage:" + System.lineSeparator() + getSubcommand() + "add categoryName" + System.lineSeparator() + getSubcommand() + "get";
|
return "Usage:" + System.lineSeparator() + getSubcommand() + " add categoryName" + System.lineSeparator() + getSubcommand() + " get";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,52 +7,64 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LinkCommands extends Subcommand {
|
public class LinkCommands extends Subcommand {
|
||||||
|
|
||||||
private final LinkCliAdapter linkCliAdapter;
|
private final LinkCliAdapter linkCliAdapter;
|
||||||
|
|
||||||
public LinkCommands(LinkCliAdapter linkCliAdapter) {
|
public LinkCommands(LinkCliAdapter linkCliAdapter) {
|
||||||
this.linkCliAdapter = linkCliAdapter;
|
this.linkCliAdapter = linkCliAdapter;
|
||||||
commands.put("add", this::addLink);
|
commands.put("add", this::addLink);
|
||||||
commands.put("get", this::getAll);
|
commands.put("get", this::getAll);
|
||||||
commands.put("category", this::getByCategory);
|
commands.put("category", this::getByCategory);
|
||||||
commands.put("user", this::getByUser);
|
commands.put("user", this::getByUser);
|
||||||
commands.put("tag", this::getByTag);
|
commands.put("tag", this::getByTag);
|
||||||
}
|
commands.put("hosts", this::groupByHosts);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSubcommand() {
|
public String getSubcommand() {
|
||||||
return "link";
|
return "link";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUsage() {
|
public String getUsage() {
|
||||||
return "Usage: " + System.lineSeparator() +
|
return "Usage: " + System.lineSeparator() +
|
||||||
getSubcommand() + "add http://example.org yourUsername [category1 category2 .. categoryN]" + System.lineSeparator() +
|
getSubcommand() + " add http://example.org yourUsername [category1 category2 .. categoryN]" + System.lineSeparator() +
|
||||||
getSubcommand() + "get " + System.lineSeparator() +
|
getSubcommand() + " get " + System.lineSeparator() +
|
||||||
getSubcommand() + "category aCategoryName " + System.lineSeparator() +
|
getSubcommand() + " category aCategoryName " + System.lineSeparator() +
|
||||||
getSubcommand() + "tag aTagName " + System.lineSeparator() +
|
getSubcommand() + " tag aTagName " + System.lineSeparator() +
|
||||||
getSubcommand() + "user aUserName ";
|
getSubcommand() + " user aUserName " + System.lineSeparator() +
|
||||||
}
|
getSubcommand() + " hosts";
|
||||||
|
|
||||||
private String addLink(String[] args) {
|
}
|
||||||
linkCliAdapter.addLink(args[1],
|
|
||||||
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
|
|
||||||
args[2]);
|
|
||||||
return "Added the new Link";
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getAll(String[] args) {
|
private String addLink(String[] args) {
|
||||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
|
linkCliAdapter.addLink(args[1],
|
||||||
}
|
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
|
||||||
|
args[2]);
|
||||||
|
return "Added the new Link";
|
||||||
|
}
|
||||||
|
|
||||||
private String getByCategory(String[] args) {
|
private String getAll(String[] args) {
|
||||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForCategory(args[1]));
|
var firstLine = "All available Links:" + System.lineSeparator();
|
||||||
}
|
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinks());
|
||||||
|
}
|
||||||
|
|
||||||
private String getByUser(String[] args) {
|
private String getByCategory(String[] args) {
|
||||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForUser(args[1]));
|
var firstLine = "All available Links for the category:" + System.lineSeparator();
|
||||||
}
|
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinksForCategory(args[1]));
|
||||||
|
}
|
||||||
|
|
||||||
private String getByTag(String[] args) {
|
private String getByUser(String[] args) {
|
||||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForTag(args[1]));
|
var firstLine = "All available Links for the user:" + System.lineSeparator();
|
||||||
}
|
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinksForUser(args[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getByTag(String[] args) {
|
||||||
|
var firstLine = "All available Links for the tag:" + System.lineSeparator();
|
||||||
|
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinksForTag(args[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String groupByHosts(String[] strings) {
|
||||||
|
var firstLine = "All available Links grouped by the host names:" + System.lineSeparator();
|
||||||
|
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.groupByHosts());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ public class TagCommands extends Subcommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTags(String[] args) {
|
private String getTags(String[] args) {
|
||||||
return String.join(System.lineSeparator(), customTagsCliAdapter.getAllTagNames());
|
var firstLine = "All available Tags:" + System.lineSeparator();
|
||||||
|
return firstLine + String.join(System.lineSeparator(), customTagsCliAdapter.getAllTagNames());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,5 @@ public class Main {
|
||||||
catch (RuntimeException runtimeException) {
|
catch (RuntimeException runtimeException) {
|
||||||
System.out.println(runtimeException.getMessage());
|
System.out.println(runtimeException.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import link.LinkUseCase;
|
||||||
import tag.TagName;
|
import tag.TagName;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -39,4 +40,8 @@ public class LinkCliAdapter {
|
||||||
public Set<String> getLinksForUser(String userName) {
|
public Set<String> getLinksForUser(String userName) {
|
||||||
return linkUseCase.getLinksForUser(new Username(userName)).stream().map(LinkDto::toString).collect(Collectors.toSet());
|
return linkUseCase.getLinksForUser(new Username(userName)).stream().map(LinkDto::toString).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> groupByHosts() {
|
||||||
|
return linkUseCase.groupByHosts().stream().map(LinkDto::toString).collect(Collectors.toList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,13 +33,13 @@ public class LinkEntity implements CSVSerializable {
|
||||||
this.id = Integer.parseInt(fields[0]);
|
this.id = Integer.parseInt(fields[0]);
|
||||||
this.creator = fields[1];
|
this.creator = fields[1];
|
||||||
this.url = fields[2];
|
this.url = fields[2];
|
||||||
this.categoryIds =
|
this.categoryIds = fields.length > 3 ?
|
||||||
(Arrays.stream(fields[3].split(CSVSerializable.listSeperator)).map(Integer::parseInt)).collect(
|
(Arrays.stream(fields[3].split(CSVSerializable.listSeperator)).map(Integer::parseInt)).collect(
|
||||||
Collectors.toSet());
|
Collectors.toSet()) : Set.of();
|
||||||
|
|
||||||
this.tags = (Arrays
|
this.tags = fields.length > 4 ? (Arrays
|
||||||
.stream(fields[4].split(CSVSerializable.listSeperator))
|
.stream(fields[4].split(CSVSerializable.listSeperator))
|
||||||
.map(TagEntity::new)).collect(Collectors.toSet());
|
.map(TagEntity::new)).collect(Collectors.toSet()) : Set.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkEntity(Link link) {
|
public LinkEntity(Link link) {
|
||||||
|
|
|
@ -10,65 +10,76 @@ import tag.TagName;
|
||||||
import tag.TaggingUseCase;
|
import tag.TaggingUseCase;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LinkUseCase {
|
public class LinkUseCase {
|
||||||
private final LinkRepository linkRepository;
|
|
||||||
private final CategoryRepository categoryRepository;
|
|
||||||
private final TaggingUseCase taggingUseCase;
|
|
||||||
private final RandomLinkIdGenerator linkIdGenerator;
|
|
||||||
|
|
||||||
public LinkUseCase(LinkRepository linkRepository,
|
private final LinkRepository linkRepository;
|
||||||
CategoryRepository categoryRepository,
|
private final CategoryRepository categoryRepository;
|
||||||
TaggingUseCase taggingUseCase,
|
private final TaggingUseCase taggingUseCase;
|
||||||
RandomLinkIdGenerator linkIdGenerator) {
|
private final RandomLinkIdGenerator linkIdGenerator;
|
||||||
this.linkRepository = linkRepository;
|
|
||||||
this.categoryRepository = categoryRepository;
|
|
||||||
this.taggingUseCase = taggingUseCase;
|
|
||||||
this.linkIdGenerator = linkIdGenerator;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
public LinkUseCase(LinkRepository linkRepository,
|
||||||
if (!OnlineCheck.isReachable(url)) {
|
CategoryRepository categoryRepository,
|
||||||
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
|
TaggingUseCase taggingUseCase,
|
||||||
}
|
RandomLinkIdGenerator linkIdGenerator) {
|
||||||
var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
|
this.linkRepository = linkRepository;
|
||||||
var tags = taggingUseCase.getTagsFor(url);
|
this.categoryRepository = categoryRepository;
|
||||||
var id = linkIdGenerator.generateId();
|
this.taggingUseCase = taggingUseCase;
|
||||||
var link = new Link(id, creator, url, categoryIds, tags);
|
this.linkIdGenerator = linkIdGenerator;
|
||||||
linkRepository.add(link);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public void addLink(LinkUrl url, Set<CategoryName> categoryNames, Username creator) {
|
||||||
|
if (!OnlineCheck.isReachable(url)) {
|
||||||
|
throw new URLIsNotReachable("The url " + url + " does not seem online. Check if it is correct and you have internet access.");
|
||||||
|
}
|
||||||
|
var categoryIds = categoryNames.stream().map(categoryRepository::getIdByName).collect(Collectors.toSet());
|
||||||
|
var tags = taggingUseCase.getTagsFor(url);
|
||||||
|
var id = linkIdGenerator.generateId();
|
||||||
|
var link = new Link(id, creator, url, categoryIds, tags);
|
||||||
|
linkRepository.add(link);
|
||||||
|
}
|
||||||
|
|
||||||
public Set<LinkDto> getLinks() {
|
public Set<LinkDto> getLinks() {
|
||||||
return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
|
return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
|
public Set<LinkDto> getLinksForCategory(CategoryName categoryName) {
|
||||||
return linkRepository.getByCategory(categoryRepository.getIdByName(categoryName).id()).stream().map(this::convertLink).collect(Collectors.toSet());
|
return linkRepository
|
||||||
}
|
.getByCategory(categoryRepository.getIdByName(categoryName))
|
||||||
|
.stream()
|
||||||
|
.map(this::convertLink)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
public Set<LinkDto> getLinksForTag(TagName tagName) {
|
public Set<LinkDto> getLinksForTag(TagName tagName) {
|
||||||
return linkRepository.getByTag(tagName).stream().map(this::convertLink).collect(Collectors.toSet());
|
return linkRepository.getByTag(tagName).stream().map(this::convertLink).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
public Set<LinkDto> getLinksForUser(Username username) {
|
|
||||||
return linkRepository.getByUser(username).stream().map(this::convertLink).collect(Collectors.toSet());
|
|
||||||
}
|
|
||||||
|
|
||||||
private LinkDto convertLink(Link link) {
|
public Set<LinkDto> getLinksForUser(Username username) {
|
||||||
return new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
|
return linkRepository.getByUser(username).stream().map(this::convertLink).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Category> getCategoriesOf(Link link) {
|
public List<LinkDto> groupByHosts() {
|
||||||
return link.getCategoryIds().stream().map(getCategoryForId()).collect(Collectors.toSet());
|
return linkRepository.groupByHost().stream().map(this::convertLink).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private LinkDto convertLink(Link link) {
|
||||||
|
return new LinkDto(link.getCreator(), link.getUrl(), getCategoriesOf(link), link.getTags());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Category> getCategoriesOf(Link link) {
|
||||||
|
return link.getCategoryIds().stream().map(getCategoryForId()).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<CategoryId, Category> getCategoryForId() {
|
||||||
|
return id -> categoryRepository
|
||||||
|
.getById(id)
|
||||||
|
.orElseThrow(() -> new CategroyDoesNotExist(
|
||||||
|
"A Category for a certain id does not exits. You must create it first."));
|
||||||
|
}
|
||||||
|
|
||||||
private Function<CategoryId, Category> getCategoryForId() {
|
|
||||||
return id -> categoryRepository
|
|
||||||
.getById(id)
|
|
||||||
.orElseThrow(() -> new CategroyDoesNotExist(
|
|
||||||
"A Category for a certain id does not exits. You must create it first."));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package link;
|
package link;
|
||||||
|
|
||||||
public class RandomLinkIdGenerator implements LinkIdGenerator {
|
public class RandomLinkIdGenerator {
|
||||||
|
|
||||||
private final LinkRepository linkRepository;
|
private final LinkRepository linkRepository;
|
||||||
|
|
||||||
public RandomLinkIdGenerator(LinkRepository linkRepository) {
|
public RandomLinkIdGenerator(LinkRepository linkRepository) {
|
||||||
this.linkRepository = linkRepository;
|
this.linkRepository = linkRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public LinkId generateId() {
|
public LinkId generateId() {
|
||||||
var randomID = (int) (Math.random() * 10000);
|
var randomID = (int) (Math.random() * 10000);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package link;
|
||||||
|
|
||||||
import category.CategoryId;
|
import category.CategoryId;
|
||||||
import tag.Tag;
|
import tag.Tag;
|
||||||
|
import tag.TagName;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -34,10 +35,22 @@ public class Link {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasCategoryId(CategoryId categoryId) {
|
||||||
|
return categoryIds.contains(categoryId);
|
||||||
|
}
|
||||||
|
|
||||||
public Set<CategoryId> getCategoryIds() {
|
public Set<CategoryId> getCategoryIds() {
|
||||||
return categoryIds;
|
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() {
|
public Set<Tag> getTags() {
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,76 +2,100 @@ package link;
|
||||||
|
|
||||||
import abstraction.CustomSetPersistenceDecorator;
|
import abstraction.CustomSetPersistenceDecorator;
|
||||||
import abstraction.PersistenceAdapter;
|
import abstraction.PersistenceAdapter;
|
||||||
|
import category.CategoryId;
|
||||||
import datastructures.set.CustomSet;
|
import datastructures.set.CustomSet;
|
||||||
import exeptions.LinkAlreadyExists;
|
import exeptions.LinkAlreadyExists;
|
||||||
import exeptions.LinkDoesNotExist;
|
import exeptions.LinkDoesNotExist;
|
||||||
import tag.TagName;
|
import tag.TagName;
|
||||||
import user.Username;
|
import user.Username;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LinkRepository {
|
public class LinkRepository {
|
||||||
|
|
||||||
final private CustomSet<Link> links;
|
final private CustomSet<Link> links;
|
||||||
|
|
||||||
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
|
public LinkRepository(PersistenceAdapter<Link> linkPersistenceAdapter) {
|
||||||
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
|
this.links = new CustomSetPersistenceDecorator<>(linkPersistenceAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Link> getById(LinkId id) {
|
public Optional<Link> getById(LinkId id) {
|
||||||
return links.find(link -> link.getId().equals(id));
|
return links.find(link -> link.getId().equals(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Link> getByUrl(LinkUrl url) {
|
public Optional<Link> getByUrl(LinkUrl url) {
|
||||||
return links.find(link -> link.getUrl().equals(url));
|
return links.find(link -> link.getUrl().equals(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeById(LinkId id) {
|
public void removeById(LinkId id) {
|
||||||
getById(id).ifPresentOrElse(this::remove, () -> {
|
getById(id).ifPresentOrElse(this::remove, () -> {
|
||||||
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
|
throw new LinkDoesNotExist("Tried removing a link with id " + id + " but it does not exist.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeByUrl(LinkUrl url) {
|
public void removeByUrl(LinkUrl url) {
|
||||||
getByUrl(url).ifPresentOrElse(this::remove, () -> {
|
getByUrl(url).ifPresentOrElse(this::remove, () -> {
|
||||||
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
|
throw new LinkDoesNotExist("Tried removing a link with url " + url + " but it does not exist.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Link link) {
|
public void add(Link link) {
|
||||||
checkDuplicates(link);
|
checkDuplicates(link);
|
||||||
links.add(link);
|
links.add(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Link> getAll() {
|
public Set<Link> getAll() {
|
||||||
return links.getSet();
|
return links.getSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void remove(Link link) {
|
private void remove(Link link) {
|
||||||
links.remove(link);
|
links.remove(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkDuplicates(Link newLink) {
|
private void checkDuplicates(Link newLink) {
|
||||||
if (this.getById(newLink.getId()).isPresent()) {
|
if (this.getById(newLink.getId()).isPresent()) {
|
||||||
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
|
throw new LinkAlreadyExists("A link with the id " + newLink.getId() + " already exitsts");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.getByUrl(newLink.getUrl()).isPresent()) {
|
if (this.getByUrl(newLink.getUrl()).isPresent()) {
|
||||||
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
|
throw new LinkAlreadyExists("A link with the url " + newLink.getUrl() + " already exitsts");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Link> getByCategory(int id) {
|
public Set<Link> getByCategory(CategoryId id) {
|
||||||
return links.stream().filter(it -> it.getCategoryIds().contains(id)).collect(Collectors.toSet());
|
return links.stream().filter(link -> link.hasCategoryId(id)).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Link> getByTag(TagName tagName) {
|
public Set<Link> getByTag(TagName tagName) {
|
||||||
return links.stream().filter(it -> it.getTags().stream().anyMatch(tag -> tag.getName().equals(tagName))).collect(Collectors.toSet());
|
return links.stream().filter(link -> link.hasTagName(tagName)).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Link> getByUser(Username username) {
|
public Set<Link> getByUser(Username username) {
|
||||||
return links.stream().filter(it -> it.getCreator().equals(username)).collect(Collectors.toSet());
|
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;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return url.toString();
|
return url.toString();
|
||||||
|
|
Reference in a new issue