Skip to content

Commit

Permalink
Merge pull request #170 from avlo/refactor_generics
Browse files Browse the repository at this point in the history
Generics Refactor
  • Loading branch information
tcheeric committed May 31, 2024
2 parents 6a1b776 + bd17fe2 commit 6934f8d
Show file tree
Hide file tree
Showing 47 changed files with 711 additions and 503 deletions.
10 changes: 8 additions & 2 deletions nostr-java-api/src/main/java/nostr/api/Nostr.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static String encode(@NonNull BaseEvent event) {
* @return
*/
public static String encode(@NonNull BaseEvent event, Relay relay) {
final var enc = new BaseEventEncoder(event, relay);
final var enc = new BaseEventEncoder(event);
return enc.encode();
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public static BaseTag decodeTag(@NonNull String json) {
* @param relay
*/
public static String encode(@NonNull FiltersList filtersList, Relay relay) {
final var enc = new FiltersListEncoder(filtersList, relay);
final var enc = new FiltersListEncoder(filtersList);
return enc.encode();
}

Expand Down Expand Up @@ -309,6 +309,12 @@ public static IElement decode(@NonNull String json, @NonNull Class clazz) {
case "nostr.event.BaseTag.class" -> {
return decodeTag(json);
}
default -> throw new AssertionError();
}
}

public static Filters decodeFilters(@NonNull String json, @NonNull Class clazz) {
switch (clazz.getName()) {
case "nostr.event.Filters.class" -> {
return decodeFilters(json);
}
Expand Down
10 changes: 10 additions & 0 deletions nostr-java-base/src/main/java/nostr/base/FEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nostr.base;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

public interface FEncoder<T> {
ObjectMapper MAPPER = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);

String encode();
}
21 changes: 21 additions & 0 deletions nostr-java-base/src/main/java/nostr/base/FNostrList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

package nostr.base;

import java.util.ArrayList;
import java.util.List;

public abstract class FNostrList<T> extends ArrayList<T> {

public boolean addAll(List<T> list) {
return super.addAll(list);
}

public List<T> getList() {
return super.stream().toList();
}

@Override
public int size() {
return super.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
*/
@Data
@NoArgsConstructor
public class GenericTagQuery implements IElement {
public class GenericTagQuery {

private String tagName;
private List<String> value;

@Override
@JsonIgnore
public Integer getNip() {
return 1;
Expand Down
20 changes: 14 additions & 6 deletions nostr-java-base/src/main/java/nostr/base/INostrList.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@

package nostr.base;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author squirrel
* @param <T>
*/
public interface INostrList<T> extends IElement {
public abstract class INostrList<T extends IElement> extends ArrayList<T> {

void add(T elt);
public boolean add(T... elt) {
return this.addAll(List.of(elt));
}

void addAll(INostrList<T> list);
public boolean addAll(List<T> list) {
return super.addAll(list);
}

List<T> getList();
public List<T> getList() {
return super.stream().toList();
}

int size();
public int size() {
return super.size();
}
}
12 changes: 4 additions & 8 deletions nostr-java-event/src/main/java/nostr/event/BaseMessage.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package nostr.event;

import lombok.Getter;
import nostr.base.IElement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

/**
*
* @author squirrel
*/
@Data
@AllArgsConstructor
@ToString
@Getter
public abstract class BaseMessage implements IElement {

private final String command;

protected BaseMessage() {
this.command = null;
protected BaseMessage(String command) {
this.command = command;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import nostr.event.AbstractEventContent;
import nostr.event.tag.PriceTag;

@Data
@EqualsAndHashCode(callSuper = false)
@Setter
@Getter
public class ClassifiedListing extends AbstractEventContent<ClassifiedListingEvent> {
@JsonIgnore
private String id;

@JsonProperty
private String title;
private final String title;

@JsonProperty
private String summary;
private final String summary;

@JsonProperty("published_at")
@EqualsAndHashCode.Exclude
Expand All @@ -28,7 +29,7 @@ public class ClassifiedListing extends AbstractEventContent<ClassifiedListingEve
private String location;

@JsonProperty("price")
private PriceTag priceTag;
private final PriceTag priceTag;

public ClassifiedListing(@NonNull String title, @NonNull String summary, @NonNull PriceTag priceTag) {
this.title = title;
Expand Down
153 changes: 68 additions & 85 deletions nostr-java-event/src/main/java/nostr/event/impl/Filters.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,68 @@

package nostr.event.impl;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import nostr.base.GenericTagQuery;
import nostr.base.annotation.Key;
import nostr.event.BaseEvent;
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;

/**
*
* @author squirrel
*/
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class Filters extends BaseEvent {

@Key
@JsonProperty("ids")
@JsonSerialize(using=CustomIdEventListSerializer.class)
private EventList events;

@Key
@JsonProperty("authors")
private PublicKeyList authors;

@Key
private KindList kinds;

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

@Key
@JsonProperty("#p")
private PublicKeyList referencePubKeys;

@Key
private Long since;

@Key
private Long until;

@Key
private Integer limit;

@Key(nip = 12)
@JsonSerialize(using=CustomGenericTagQuerySerializer.class)
@JsonDeserialize(using=CustomGenericTagQueryDeserializer.class)
private GenericTagQuery genericTagQuery;

@Override
public String toBech32() {
throw new UnsupportedOperationException("This operation is not supported.");
}

@JsonIgnore
@Override
public Integer getNip() {
return 1;
}

@Override
@JsonIgnore
public String getId() {
throw new UnsupportedOperationException("Not supported.");
}
}

package nostr.event.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import nostr.base.GenericTagQuery;
import nostr.base.PublicKey;
import nostr.base.annotation.Key;
import nostr.event.Kind;
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;

/**
*
* @author squirrel
*/
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class Filters {

@Key
@JsonProperty("ids")
@JsonSerialize(using=CustomIdEventListSerializer.class)
private EventList<GenericEvent> events;

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

@Key
private KindList kinds;

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

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

@Key
private Long since;

@Key
private Long until;

@Key
private Integer limit;

@Key(nip = 12)
@JsonSerialize(using=CustomGenericTagQuerySerializer.class)
@JsonDeserialize(using=CustomGenericTagQueryDeserializer.class)
private GenericTagQuery genericTagQuery;
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package nostr.event.json.codec;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import nostr.base.IEncoder;
import nostr.base.Relay;
import nostr.event.BaseEvent;
import nostr.util.NostrException;

/**
* @author guilhermegps
*
*/
@AllArgsConstructor
@Data
public class BaseEventEncoder implements IEncoder<BaseEvent> {
public class BaseEventEncoder<T extends BaseEvent> implements IEncoder<T> {

private final BaseEvent event;
private final Relay relay;
private final T event;

protected BaseEventEncoder() {
this.event = null;
this.relay = null;
}

public BaseEventEncoder(BaseEvent event) {
this(event, null);
public BaseEventEncoder(T event) {
this.event = event;
}

@Override
Expand Down
Loading

0 comments on commit 6934f8d

Please sign in to comment.