Skip to content

Commit

Permalink
supports config with global context
Browse files Browse the repository at this point in the history
  • Loading branch information
Salpadding committed Jan 30, 2020
1 parent c45ea7e commit 77560bc
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 3 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,55 @@ public class Main{

```

- Add global context to avoid duplicated @RLPEncoding and @RLPDecoding

```java
public class Main{
public static class LocalDateDecoder implements RLPDecoder<LocalDate> {
@Override
public LocalDate decode(RLPElement rlpElement) {
if(rlpElement.isNull()) return null;
int[] data = rlpElement.as(int[].class);
return LocalDate.of(data[0], data[1], data[2]);
}
}

public class LocalDateEncoder implements RLPEncoder<LocalDate> {
@Override
public RLPElement encode(LocalDate localDate) {
return RLPElement.readRLPTree(
new int[]{localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()}
);
}
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User{
private LocalDate birthDay;
}

public static void main(String[] args){
RLPContext context = RLPContext
.newInstance()
.withDecoder(LocalDate.class, new LocalDateDecoder())
.withEncoder(LocalDate.class, new LocalDateEncoder());
RLPMapper mapper = new RLPMapper().withContext(context);
User u = new User();
element = mapper.readRLPTree(u);
User decoded = mapper.decode(element, User.class);
assert decoded.birthDay == null;

u.birthDay = LocalDate.now();
byte[] encoded = mapper.encode(u);
decoded = mapper.decode(encoded, User.class);
System.out.println(decoded.birthDay.format(DateTimeFormatter.ISO_DATE));
}

}
```

## Benchmark

- see RLPTest.performanceDecode for benchmark
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'org.tdf'
version '1.1.17'
version '1.1.18'

sourceCompatibility = 1.8

Expand All @@ -22,7 +22,10 @@ ext{
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"

testCompile "commons-codec:commons-codec:${commonsCodecVersion}"
testCompile "com.madgag.spongycastle:core:${scastleVersion}" // for rlp encoding
testCompile "com.madgag.spongycastle:prov:${scastleVersion}" // for rlp encoding
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/tdf/rlp/RLPDecoder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.tdf.rlp;

import lombok.NonNull;

public interface RLPDecoder<T> {
T decode(RLPElement element);
/**
*
* @param element the element may equals to RLPItem.NULL
* @return
*/
T decode(@NonNull RLPElement element);

class None implements RLPDecoder<Object> {
@Override
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/tdf/rlp/RLPEncoder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.tdf.rlp;

import lombok.NonNull;

public interface RLPEncoder<T> {
RLPElement encode(T o);
/**
*
* @param o non-null object
* @return encoded result
*/
RLPElement encode(@NonNull T o);

class None implements RLPEncoder<Object> {
@Override
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/tdf/rlp/LocalDateDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.tdf.rlp;

import lombok.NonNull;

import java.time.LocalDate;

public class LocalDateDecoder implements RLPDecoder<LocalDate> {
@Override
public LocalDate decode(@NonNull RLPElement rlpElement) {
if(rlpElement.isNull()) return null;
int[] data = rlpElement.as(int[].class);
return LocalDate.of(data[0], data[1], data[2]);
}
}
15 changes: 15 additions & 0 deletions src/test/java/org/tdf/rlp/LocalDateEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.tdf.rlp;

import lombok.NonNull;

import java.time.LocalDate;
import java.util.Arrays;

public class LocalDateEncoder implements RLPEncoder<LocalDate> {
@Override
public RLPElement encode(@NonNull LocalDate localDate) {
return RLPElement.readRLPTree(
Arrays.asList(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth())
);
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/tdf/rlp/RLPTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tdf.rlp;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.binary.Hex;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -13,6 +16,8 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -1685,4 +1690,36 @@ public void testDecodeContainer() throws Exception{
assert dummmy4.dum != null;
assert dummmy4.dum.size() == 0;
}

@Test
public void testContext(){
RLPContext context = RLPContext
.newInstance()
.withDecoder(LocalDate.class, new LocalDateDecoder())
.withEncoder(LocalDate.class, new LocalDateEncoder());
RLPMapper mapper = new RLPMapper().withContext(context);
LocalDate now = LocalDate.now();
RLPElement element = mapper.readRLPTree(now);
for(long l : element.as(long[].class)){
System.out.println(l);
}
assert now.equals(mapper.decode(element, LocalDate.class));

User u = new User();
element = mapper.readRLPTree(u);
User decoded = mapper.decode(element, User.class);
assert decoded.birthDay == null;

u.birthDay = LocalDate.now();
byte[] encoded = mapper.encode(u);
decoded = mapper.decode(encoded, User.class);
System.out.println(decoded.birthDay.format(DateTimeFormatter.ISO_DATE));
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User{
private LocalDate birthDay;
}
}

0 comments on commit 77560bc

Please sign in to comment.