stuff
This commit is contained in:
parent
7fbc3f722c
commit
22a9368697
|
@ -21,7 +21,7 @@ abstract public class Subcommand {
|
|||
commandExists(args[0]);
|
||||
return commands.get(args[0]).apply(args);
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
catch (IndexOutOfBoundsException | IllegalArgumentException e) {
|
||||
throw new CliError("Missing a value! " + getUsage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ public class LinkCommands extends Subcommand {
|
|||
commands.put("category", this::getByCategory);
|
||||
commands.put("user", this::getByUser);
|
||||
commands.put("tag", this::getByTag);
|
||||
commands.put("hosts", this::groupByHosts);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,7 +31,9 @@ public class LinkCommands extends Subcommand {
|
|||
getSubcommand() + " get " + System.lineSeparator() +
|
||||
getSubcommand() + " category aCategoryName " + System.lineSeparator() +
|
||||
getSubcommand() + " tag aTagName " + System.lineSeparator() +
|
||||
getSubcommand() + "user aUserName ";
|
||||
getSubcommand() + " user aUserName " + System.lineSeparator() +
|
||||
getSubcommand() + " hosts";
|
||||
|
||||
}
|
||||
|
||||
private String addLink(String[] args) {
|
||||
|
@ -41,18 +44,27 @@ public class LinkCommands extends Subcommand {
|
|||
}
|
||||
|
||||
private String getAll(String[] args) {
|
||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
|
||||
var firstLine = "All available Links:" + System.lineSeparator();
|
||||
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinks());
|
||||
}
|
||||
|
||||
private String getByCategory(String[] args) {
|
||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForCategory(args[1]));
|
||||
var firstLine = "All available Links for the category:" + System.lineSeparator();
|
||||
return firstLine + String.join(System.lineSeparator(), linkCliAdapter.getLinksForCategory(args[1]));
|
||||
}
|
||||
|
||||
private String getByUser(String[] args) {
|
||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForUser(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) {
|
||||
return String.join(System.lineSeparator(), linkCliAdapter.getLinksForTag(args[1]));
|
||||
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) {
|
||||
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) {
|
||||
System.out.println(runtimeException.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import link.LinkUseCase;
|
|||
import tag.TagName;
|
||||
import user.Username;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -39,4 +40,8 @@ public class LinkCliAdapter {
|
|||
public Set<String> getLinksForUser(String userName) {
|
||||
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.creator = fields[1];
|
||||
this.url = fields[2];
|
||||
this.categoryIds =
|
||||
this.categoryIds = fields.length > 3 ?
|
||||
(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))
|
||||
.map(TagEntity::new)).collect(Collectors.toSet());
|
||||
.map(TagEntity::new)).collect(Collectors.toSet()) : Set.of();
|
||||
}
|
||||
|
||||
public LinkEntity(Link link) {
|
||||
|
|
|
@ -10,11 +10,13 @@ import tag.TagName;
|
|||
import tag.TaggingUseCase;
|
||||
import user.Username;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LinkUseCase {
|
||||
|
||||
private final LinkRepository linkRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final TaggingUseCase taggingUseCase;
|
||||
|
@ -41,22 +43,30 @@ public class LinkUseCase {
|
|||
linkRepository.add(link);
|
||||
}
|
||||
|
||||
|
||||
public Set<LinkDto> getLinks() {
|
||||
return linkRepository.getAll().stream().map(this::convertLink).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
|
||||
public List<LinkDto> groupByHosts() {
|
||||
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());
|
||||
}
|
||||
|
@ -71,4 +81,5 @@ public class LinkUseCase {
|
|||
.orElseThrow(() -> new CategroyDoesNotExist(
|
||||
"A Category for a certain id does not exits. You must create it first."));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package link;
|
||||
|
||||
public class RandomLinkIdGenerator implements LinkIdGenerator {
|
||||
public class RandomLinkIdGenerator {
|
||||
|
||||
private final LinkRepository linkRepository;
|
||||
|
||||
public RandomLinkIdGenerator(LinkRepository linkRepository) {
|
||||
this.linkRepository = linkRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkId generateId() {
|
||||
var randomID = (int) (Math.random() * 10000);
|
||||
|
||||
|
|
|
@ -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,12 +2,15 @@ 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;
|
||||
|
@ -63,15 +66,36 @@ public class LinkRepository {
|
|||
}
|
||||
}
|
||||
|
||||
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());
|
||||
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());
|
||||
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