small code improvements

This commit is contained in:
qvalentin 2022-05-27 14:00:25 +02:00
parent ba6a889d35
commit bb406ead06
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
9 changed files with 31 additions and 24 deletions

View File

@ -3,7 +3,6 @@ package main;
import category.CategoryRepository; import category.CategoryRepository;
import category.CategoryUseCase; import category.CategoryUseCase;
import category.RandomCategoryIdGenerator; import category.RandomCategoryIdGenerator;
import cli.CliError;
import cli.Subcommand; import cli.Subcommand;
import cli.category.CategoryCliAdapter; import cli.category.CategoryCliAdapter;
import cli.category.CategoryCommands; import cli.category.CategoryCommands;
@ -43,19 +42,26 @@ public class CommandHandler {
public void executeCommand(String[] args) { public void executeCommand(String[] args) {
if (args.length == 0) { if (args.length == 0) {
//TODO: print usage printUsage();
return;
} }
var command = subcommands.get(args[0]); var command = subcommands.get(args[0]);
if (command == null) { if (command == null) {
throw new CliError("Unknown Command"); System.out.println("Unkown subcommand!");
printUsage();
} }
var output = command.executeSubcommand(Arrays.copyOfRange(args, 1, args.length)); var output = command.executeSubcommand(Arrays.copyOfRange(args, 1, args.length));
System.out.println(output); System.out.println(output);
} }
private void printUsage() {
System.out.println("All available subcommands: ");
subcommands.forEach((name, subcommand) -> System.out.println(System.lineSeparator() + subcommand.getUsage()));
}
private void addSubcommands() { private void addSubcommands() {
CategoryRepository categoryRepository = CategoryRepository categoryRepository =

View File

@ -34,7 +34,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
} }
} }
public Set<T> getALl() { public Set<T> getAll() {
var lines = new ArrayList<String>(); var lines = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(this.file))) { try (BufferedReader br = new BufferedReader(new FileReader(this.file))) {
for (String line; (line = br.readLine()) != null; ) { for (String line; (line = br.readLine()) != null; ) {
@ -53,7 +53,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
@Override @Override
public void remove(T value) { public void remove(T value) {
var allEntities = this.getALl(); var allEntities = this.getAll();
this.removeAll(); this.removeAll();
allEntities.stream().filter(entity -> !entity.equals(value)).forEach(this::add); allEntities.stream().filter(entity -> !entity.equals(value)).forEach(this::add);

View File

@ -36,10 +36,10 @@ class GenericCSVDAOTest {
@Test @Test
public void addWorks() throws IOException { public void addWorks() throws IOException {
assertEquals(0, sut.getALl().size()); assertEquals(0, sut.getAll().size());
CategoryEntity entityToAdd = addDummyEntity("categoryName", 99); CategoryEntity entityToAdd = addDummyEntity("categoryName", 99);
var foundEntity = sut.getALl().stream().findFirst().orElseThrow(); var foundEntity = sut.getAll().stream().findFirst().orElseThrow();
assertEquals(entityToAdd, foundEntity); assertEquals(entityToAdd, foundEntity);
} }
@ -50,7 +50,7 @@ class GenericCSVDAOTest {
sut.remove(entityToAdd); sut.remove(entityToAdd);
assertEquals(0, sut.getALl().size()); assertEquals(0, sut.getAll().size());
} }
@ -58,10 +58,10 @@ class GenericCSVDAOTest {
public void removeAllWorks() throws IOException { public void removeAllWorks() throws IOException {
addDummyEntity("categoryName1", 101); addDummyEntity("categoryName1", 101);
addDummyEntity("categoryName2", 102); addDummyEntity("categoryName2", 102);
assertEquals(2, sut.getALl().size()); assertEquals(2, sut.getAll().size());
sut.removeAll(); sut.removeAll();
assertEquals(0, sut.getALl().size()); assertEquals(0, sut.getAll().size());
} }
private CategoryEntity addDummyEntity(String categoryName, int id) { private CategoryEntity addDummyEntity(String categoryName, int id) {

View File

@ -1,13 +1,10 @@
package persistence; package persistence;
import persistence.category.CategoryEntity;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
public interface GenericDAO<T> { public interface GenericDAO<T> {
public Set<T> getALl(); public Set<T> getAll();
void remove(T value); void remove(T value);

View File

@ -19,7 +19,7 @@ public class CSVCategoryPersistenceAdapter implements PersistenceAdapter<Categor
@Override @Override
public Set<Category> getAll() { public Set<Category> getAll() {
return categoryDAO.getALl().stream().map(CategoryEntity::toCategory).collect(Collectors.toSet()); return categoryDAO.getAll().stream().map(CategoryEntity::toCategory).collect(Collectors.toSet());
} }
@Override @Override

View File

@ -18,7 +18,7 @@ public class CSVCustomTagMatcherPersistenceAdapter implements PersistenceAdapter
@Override @Override
public Set<CustomTagMatcher> getAll() { public Set<CustomTagMatcher> getAll() {
return customTagMatcherDAO return customTagMatcherDAO
.getALl() .getAll()
.stream() .stream()
.map(CustomTagMatcherEntity::toCustomTagMatcher) .map(CustomTagMatcherEntity::toCustomTagMatcher)
.collect(Collectors.toSet()); .collect(Collectors.toSet());

View File

@ -17,7 +17,7 @@ public class CSVLinkPersistenceAdapter implements PersistenceAdapter<Link> {
@Override @Override
public Set<Link> getAll() { public Set<Link> getAll() {
return linkDAO.getALl().stream().map(LinkEntity::toLink).collect(Collectors.toSet()); return linkDAO.getAll().stream().map(LinkEntity::toLink).collect(Collectors.toSet());
} }
@Override @Override

View File

@ -8,4 +8,12 @@ import java.util.Set;
public record LinkDto(Username creator, LinkUrl url, Set<Category> categories, Set<Tag> tags) { public record LinkDto(Username creator, LinkUrl url, Set<Category> categories, Set<Tag> tags) {
@Override
public String toString() {
return
url +
" by " + creator +
" categories: " + categories +
" tags:" + tags;
}
} }

View File

@ -11,11 +11,6 @@ public class Tag {
this.name = name; this.name = name;
} }
public Tag(TagName name, String additionalData) {
this.name = name;
this.additionalData = Optional.ofNullable(additionalData);
}
public Tag(TagName name, Optional<String> additionalData) { public Tag(TagName name, Optional<String> additionalData) {
this.name = name; this.name = name;
this.additionalData = additionalData; this.additionalData = additionalData;
@ -35,6 +30,7 @@ public class Tag {
} }
public Tag addAdditionalData(String additionalData) { public Tag addAdditionalData(String additionalData) {
return new Tag(this.name, additionalData); this.additionalData = Optional.of(additionalData);
return this;
} }
} }