Skip to content

BaseList (and all other custom List implementations) refactor #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
556 changes: 277 additions & 279 deletions nostr-java-api/src/main/java/nostr/api/NIP01.java

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions nostr-java-api/src/main/java/nostr/api/Nostr.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import nostr.event.json.codec.FiltersListEncoder;
import nostr.event.json.codec.GenericEventDecoder;
import nostr.event.json.codec.GenericTagQueryEncoder;
import nostr.event.list.FiltersList;
import nostr.event.message.EventMessage;
import nostr.event.message.ReqMessage;
import nostr.id.Identity;
import nostr.util.NostrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -109,17 +109,17 @@ public void send(@NonNull Filters filters, @NonNull String subscriptionId) {
}

public void send(@NonNull Filters filters, @NonNull String subscriptionId, Map<String, String> relays) {
FiltersList filtersList = new FiltersList();
List<Filters> filtersList = new ArrayList<>();
filtersList.add(filters);

send(filtersList, subscriptionId, relays);
}

public void send(@NonNull FiltersList filtersList, @NonNull String subscriptionId) {
public void send(@NonNull List<Filters> filtersList, @NonNull String subscriptionId) {
send(filtersList, subscriptionId, getRelays());
}

public void send(@NonNull FiltersList filtersList, @NonNull String subscriptionId, Map<String, String> relays) {
public void send(@NonNull List<Filters> filtersList, @NonNull String subscriptionId, Map<String, String> relays) {

var context = new DefaultRequestContext();
context.setRelays(relays);
Expand Down Expand Up @@ -256,15 +256,15 @@ public static BaseTag decodeTag(@NonNull String json) {
* @param filtersList
* @param relay
*/
public static String encode(@NonNull FiltersList filtersList, Relay relay) {
public static String encode(@NonNull List<Filters> filtersList, Relay relay) {
final var enc = new FiltersListEncoder(filtersList);
return enc.encode();
}

/**
* @param filtersList
*/
public static String encode(@NonNull FiltersList filtersList) {
public static String encode(@NonNull List<Filters> filtersList) {
return Nostr.Json.encode(filtersList, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import nostr.event.BaseTag;
import nostr.event.Marker;
import nostr.event.impl.EphemeralEvent;
import nostr.event.impl.Filters;
import nostr.event.impl.MetadataEvent;
import nostr.event.impl.ParameterizedReplaceableEvent;
import nostr.event.impl.ReplaceableEvent;
import nostr.event.impl.TextNoteEvent;
import nostr.event.list.FiltersList;
import nostr.event.message.CloseMessage;
import nostr.event.message.EoseMessage;
import nostr.event.message.EventMessage;
Expand Down Expand Up @@ -144,7 +144,7 @@ public EventMessage create() {
public static class ReqMessageFactory extends MessageFactory<ReqMessage> {

private final String subscriptionId;
private final FiltersList filtersList;
private final List<Filters> filtersList;

@Override
public ReqMessage create() {
Expand Down
21 changes: 0 additions & 21 deletions nostr-java-base/src/main/java/nostr/base/FNostrList.java

This file was deleted.

28 changes: 0 additions & 28 deletions nostr-java-base/src/main/java/nostr/base/INostrList.java

This file was deleted.

1 change: 0 additions & 1 deletion nostr-java-event/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

exports nostr.event;
exports nostr.event.impl;
exports nostr.event.list;
exports nostr.event.message;
exports nostr.event.json.codec;
exports nostr.event.json.deserializer;
Expand Down
15 changes: 7 additions & 8 deletions nostr-java-event/src/main/java/nostr/event/impl/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import nostr.event.json.deserializer.CustomGenericTagQueryDeserializer;
import nostr.event.json.serializer.CustomGenericTagQuerySerializer;
import nostr.event.json.serializer.CustomIdEventListSerializer;
import nostr.event.list.EventList;
import nostr.event.list.KindList;
import nostr.event.list.PublicKeyList;

import java.util.List;

/**
*
Expand All @@ -34,23 +33,23 @@ public class Filters {
@Key
@JsonProperty("ids")
@JsonSerialize(using=CustomIdEventListSerializer.class)
private EventList<GenericEvent> events;
private List<GenericEvent> events;

@Key
@JsonProperty("authors")
private PublicKeyList<PublicKey> authors;
private List<PublicKey> authors;

@Key
private KindList kinds;
private List<Kind> kinds;

@Key
@JsonProperty("#e")
@JsonSerialize(using=CustomIdEventListSerializer.class)
private EventList<GenericEvent> referencedEvents;
private List<GenericEvent> referencedEvents;

@Key
@JsonProperty("#p")
private PublicKeyList<PublicKey> referencePubKeys;
private List<PublicKey> referencePubKeys;

@Key
private Long since;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import nostr.base.ElementAttribute;
import nostr.base.IDecoder;
import nostr.event.BaseMessage;
import nostr.event.impl.CanonicalAuthenticationEvent;
import nostr.event.impl.Filters;
import nostr.event.impl.GenericEvent;
import nostr.event.impl.GenericMessage;
import nostr.event.list.FiltersList;
import nostr.event.message.CanonicalAuthenticationMessage;
import nostr.event.message.CloseMessage;
import nostr.event.message.EoseMessage;
Expand All @@ -22,6 +21,7 @@
import nostr.event.message.RelayAuthenticationMessage;
import nostr.event.message.ReqMessage;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public T decode() {
var len = msgArr.length - 2;
var filtersArr = new Object[len];
System.arraycopy(msgArr, 2, filtersArr, 0, len);
var filtersList = mapper.convertValue(filtersArr, new TypeReference<FiltersList>() {
var filtersList = mapper.convertValue(filtersArr, new TypeReference<List<Filters>>() {
});
message = (T) new ReqMessage(arg.toString(), filtersList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String encode() {
} else if (message instanceof ReqMessage msg) {
arrayNode.add(msg.getSubscriptionId());
// Encode each filter individually and join them with a comma
List<Filters> filtersList = msg.getFiltersList().getList();
List<Filters> filtersList = msg.getFiltersList();
for (Filters f : filtersList) {
try {
FiltersEncoder filtersEncoder = new FiltersEncoder(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import lombok.Data;
import nostr.base.FEncoder;
import nostr.event.impl.Filters;
import nostr.event.list.FiltersList;
import nostr.util.NostrException;

import java.util.List;

@Data
public class FiltersListEncoder implements FEncoder<Filters> {

private final FiltersList filtersList;
private final List<Filters> filtersList;

public FiltersListEncoder(FiltersList filtersList) {
public FiltersListEncoder(List<Filters> filtersList) {
this.filtersList = filtersList;
}

Expand All @@ -33,7 +34,7 @@ protected String toJson() throws NostrException {
private String toJsonArray() throws NostrException {
try {
StringBuilder sb = new StringBuilder();
for (Object filter : getFiltersList().getList()) {
for (Object filter : getFiltersList()) {
if (!sb.isEmpty()) {
sb.append(",");
}
Expand All @@ -46,7 +47,7 @@ private String toJsonArray() throws NostrException {
}

private String toJsonCommaSeparated() throws NostrException {
JsonNode node = MAPPER.valueToTree(getFiltersList().getList());
JsonNode node = MAPPER.valueToTree(getFiltersList());
try {
return MAPPER.writeValueAsString(node);
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.NoArgsConstructor;
import nostr.event.impl.GenericEvent;
import nostr.event.json.codec.GenericEventDecoder;
import nostr.event.list.EventList;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

public class CustomEventListDeserializer<T extends EventList<U>, U extends GenericEvent> extends JsonDeserializer<T> {
private final Class<U> clazz;

public CustomEventListDeserializer() {
this.clazz = (Class<U>) GenericEvent.class;
}
@NoArgsConstructor
public class CustomEventListDeserializer<T extends List<U>, U extends GenericEvent> extends JsonDeserializer<T> {

@Override
public T deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
EventList<U> eventList = new EventList<>(clazz);
List<U> eventList = new ArrayList<>();
JsonNode node = jsonParser.readValueAsTree();
if (node.isArray()) {
for (JsonNode n : node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import nostr.event.impl.Filters;
import nostr.event.json.codec.FiltersDecoder;
import nostr.event.list.FiltersList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@NoArgsConstructor
public class CustomFiltersListDeserializer extends JsonDeserializer<FiltersList> {
public class CustomFiltersListDeserializer extends JsonDeserializer<List<Filters>> {
@Override
public FiltersList deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
public List<Filters> deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
JsonNode node = jsonParser.readValueAsTree();
return parseJson(node.toString());
}

public FiltersList parseJson(@NonNull String jsonString) throws IOException {
public List<Filters> parseJson(@NonNull String jsonString) throws IOException {
if (!jsonString.startsWith("[")) {
jsonString = "[" + jsonString.trim();
}
if (!jsonString.endsWith("]")) {
jsonString = jsonString + "]";
}

FiltersList filtersList = new FiltersList();
List<Filters> filtersList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(jsonString);
Iterator<JsonNode> elementsIterator = rootNode.elements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.NoArgsConstructor;
import nostr.base.GenericTagQuery;
import nostr.event.list.GenericTagQueryList;

import java.util.List;

/**
*
* @author eric
*/
public class CustomGenericTagQueryListDeserializer<T extends GenericTagQueryList<U>, U extends GenericTagQuery> extends JsonDeserializer<T> {
private final Class<U> clazz;

public CustomGenericTagQueryListDeserializer() {
this.clazz = (Class<U>) GenericTagQuery.class;
}

public CustomGenericTagQueryListDeserializer(Class<U> extendsGenericTagQuery) {
this.clazz = extendsGenericTagQuery;
}

@NoArgsConstructor
public class CustomGenericTagQueryListDeserializer<T extends List<U>, U extends GenericTagQuery> extends JsonDeserializer<T> {
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import nostr.event.list.KindList;
import nostr.event.Kind;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CustomKindListDeserializer extends JsonDeserializer<KindList> {
public class CustomKindListDeserializer extends JsonDeserializer<List<Kind>> {

@Override
public KindList deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
KindList kindList = new KindList();
public List<Kind> deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
List<Kind> kindList = new ArrayList<>();
JsonNode node = jsonParser.readValueAsTree();
if (node.isArray()) {
for (JsonNode n : node) {
kindList.add(Integer.decode(n.asText()));
kindList.add(Kind.valueOf(Integer.decode(n.asText())));
}
}
return kindList;
Expand Down
Loading