more CSV stuff

This commit is contained in:
qvalentin 2022-05-15 11:37:46 +02:00
parent e9e2256e82
commit 78d7972d28
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
5 changed files with 109 additions and 10 deletions

View File

@ -0,0 +1,36 @@
package persistence.customTags;
import abstraction.PersistenceAdapter;
import persistence.GenericDAO;
import tag.matcherImplementations.CustomTagMatcher;
import java.util.Set;
import java.util.stream.Collectors;
public class CSVCustomTagMatcherPersistenceAdapter implements PersistenceAdapter<CustomTagMatcher> {
private final GenericDAO<CustomTagMatcherEntity> customTagMatcherDAO;
public CSVCustomTagMatcherPersistenceAdapter(GenericDAO<CustomTagMatcherEntity> customTagMatcherDAO) {
this.customTagMatcherDAO = customTagMatcherDAO;
}
@Override
public Set<CustomTagMatcher> getAll() {
return customTagMatcherDAO
.getALl()
.stream()
.map(CustomTagMatcherEntity::toCustomTagMatcher)
.collect(Collectors.toSet());
}
@Override
public void add(CustomTagMatcher value) {
customTagMatcherDAO.add(new CustomTagMatcherEntity(value));
}
@Override
public void remove(CustomTagMatcher value) {
customTagMatcherDAO.remove(new CustomTagMatcherEntity(value));
}
}

View File

@ -0,0 +1,58 @@
package persistence.customTags;
import persistence.csv.CSVSerializable;
import tag.TagName;
import tag.matcherImplementations.CustomTagMatcher;
public class CustomTagMatcherEntity implements CSVSerializable {
private final String name;
private final String regexString;
public CustomTagMatcherEntity(CustomTagMatcher customTagMatcher) {
this.name = customTagMatcher.getTagName().name();
this.regexString = customTagMatcher.getRegex().toString();
}
public CustomTagMatcherEntity(String[] fields) {
this.name = fields[0];
this.regexString = fields[1];
}
public CustomTagMatcherEntity(String name, String regexString) {
this.name = name;
this.regexString = regexString;
}
public CustomTagMatcher toCustomTagMatcher() {
return new CustomTagMatcher(new TagName(name), regexString);
}
@Override
public String[] getHeaders() {
return new String[]{"name", "regexString"};
}
@Override
public String toCSVString() {
return name + CSVSerializable.seperator + regexString;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomTagMatcherEntity that = (CustomTagMatcherEntity) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return regexString != null ? regexString.equals(that.regexString) : that.regexString == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (regexString != null ? regexString.hashCode() : 0);
return result;
}
}

View File

@ -21,12 +21,12 @@ public class CSVLinkPersistenceAdapter implements PersistenceAdapter<Link> {
}
@Override
public void add(Link value) {
public void add(Link link) {
linkDAO.remove(new LinkEntity(link));
}
@Override
public void remove(Link value) {
public void remove(Link link) {
linkDAO.add(new LinkEntity(link));
}
}

View File

@ -9,6 +9,7 @@ import persistence.tag.TagEntity;
import user.Username;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -64,7 +65,7 @@ public class LinkEntity implements CSVSerializable {
@Override
public String toCSVString() {
return Integer.toString(id) + CSVSerializable.seperator + creator + CSVSerializable.seperator + url + CSVSerializable.seperator + String.join(
return id + CSVSerializable.seperator + creator + CSVSerializable.seperator + url + CSVSerializable.seperator + String.join(
CSVSerializable.listSeperator,
categoryIds
.stream()
@ -84,11 +85,11 @@ public class LinkEntity implements CSVSerializable {
LinkEntity that = (LinkEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (creator != null ? !creator.equals(that.creator) : that.creator != null) return false;
if (url != null ? !url.equals(that.url) : that.url != null) return false;
if (categoryIds != null ? !categoryIds.equals(that.categoryIds) : that.categoryIds != null) return false;
return tags != null ? tags.equals(that.tags) : that.tags == null;
if (!Objects.equals(id, that.id)) return false;
if (!Objects.equals(creator, that.creator)) return false;
if (!Objects.equals(url, that.url)) return false;
if (!Objects.equals(categoryIds, that.categoryIds)) return false;
return Objects.equals(tags, that.tags);
}
@Override

View File

@ -18,6 +18,10 @@ public class CustomTagMatcher extends TagMatcher {
this.tagName = name;
}
public Pattern getRegex() {
return regex;
}
@Override
public OptionalTag ifMatches(LinkUrl linkUrl) {
Matcher matcher = regex.matcher(linkUrl.toString());