From f2da5be6fe76c38ee75272857eb5c06c1b0de023 Mon Sep 17 00:00:00 2001 From: colourful-land Date: Sun, 8 Apr 2018 20:54:01 +0800 Subject: [PATCH] allowing ticket (non-fungible) objects to be created parsed, instead of requiring a parsing after the object is created --- .gitignore | 4 +- .../crypto/alphawallet/entity/Ticket.java | 8 +-- .../repository/AssetDefinition.java | 11 ++-- .../repository/entity/NonFungibleToken.java | 49 ++++++++++++++++-- .../alphawallet/AssetDefinitionTest.java | 50 ++++--------------- 5 files changed, 67 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index e3da9a23e..7d7e8fd2a 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,6 @@ proguard/ ### Android Patch ### gen-external-apklibs +*.*# - -tn/trustnative.aar \ No newline at end of file +tn/trustnative.aar diff --git a/app/src/main/java/io/awallet/crypto/alphawallet/entity/Ticket.java b/app/src/main/java/io/awallet/crypto/alphawallet/entity/Ticket.java index f19cb516e..9e6ad6d96 100644 --- a/app/src/main/java/io/awallet/crypto/alphawallet/entity/Ticket.java +++ b/app/src/main/java/io/awallet/crypto/alphawallet/entity/Ticket.java @@ -30,9 +30,11 @@ public class Ticket extends Token implements Parcelable { public final List balanceArray; private List burnArray; - /* this is perhaps the least invasive way to carry XML asset-definition 3 levels down - * to where it is used, without breaking the existing design by incorporating callback - * which writes a huge arrays of List for different fields of each tokenID - weiwu + /* this is perhaps the least invasive way to carry XML + * asset-definition 3 levels down to where it is used, without + * breaking the existing design by incorporating callback which + * writes a huge arrays of List for different fields of each + * tokenID - weiwu */ public AssetDefinition piggybackedXMLDefinition; diff --git a/app/src/main/java/io/awallet/crypto/alphawallet/repository/AssetDefinition.java b/app/src/main/java/io/awallet/crypto/alphawallet/repository/AssetDefinition.java index ac946ca11..ccc923e91 100644 --- a/app/src/main/java/io/awallet/crypto/alphawallet/repository/AssetDefinition.java +++ b/app/src/main/java/io/awallet/crypto/alphawallet/repository/AssetDefinition.java @@ -151,12 +151,11 @@ public class AssetDefinition { * temporarily for the need to retrofit the class with J.B.'s design */ public void parseField(BigInteger tokenId, NonFungibleToken token) { - for(String key: fields.keySet()) { + for (String key : fields.keySet()) { FieldDefinition f = fields.get(key); - BigInteger value = tokenId.and(f.bitmask).shiftRight(f.bitshift); - token.setField(f.id, f.name, f.applyToFieldValue(value)); - //System.out.println("name :" + f.name + "\n"); - //System.out.println("value :" + value + "\n"); + BigInteger val = tokenId.and(f.bitmask).shiftRight(f.bitshift); + token.setAttribute(f.id, + new NonFungibleToken.Attribute(f.id, f.name, val, f.applyToFieldValue(val))); } } @@ -166,4 +165,4 @@ public class AssetDefinition { BigInteger value = tokenId.and(field.bitmask).shiftRight(field.bitshift); return field.applyToFieldValue(value); } -} \ No newline at end of file +} diff --git a/app/src/main/java/io/awallet/crypto/alphawallet/repository/entity/NonFungibleToken.java b/app/src/main/java/io/awallet/crypto/alphawallet/repository/entity/NonFungibleToken.java index f7ed3bb8d..b12893d72 100644 --- a/app/src/main/java/io/awallet/crypto/alphawallet/repository/entity/NonFungibleToken.java +++ b/app/src/main/java/io/awallet/crypto/alphawallet/repository/entity/NonFungibleToken.java @@ -1,11 +1,50 @@ package io.awallet.crypto.alphawallet.repository.entity; +import java.math.BigInteger; +import java.util.HashMap; + +import io.awallet.crypto.alphawallet.repository.AssetDefinition; + /** - * Created by weiwu on 1/3/18. + * Created by weiwu on 1/3/18. Each NonFungibleToken is a + * non-fungible token identified by a byte32 tokenID (other forms of + * IDs may be added if tests proves that they can be more efficient). */ -public interface NonFungibleToken { - void setField(String id, String name, String value); - String getFieldText(String id); - String getFieldName(String id); +public class NonFungibleToken { + public BigInteger id; + + public static final class Attribute { + public final String id; + public String name; + public String text; + public final BigInteger value; + public Attribute(String attributeId, String name, BigInteger value, String text) { + this.id = attributeId; + this.name = name; + this.text = text; + this.value = value; + } + } + + protected HashMap attributes; + + public Attribute getAttribute(String attributeId) { + return attributes.get(attributeId); + } + + public void setAttribute(String attributeId, Attribute attribute) { + attributes.put(attributeId, attribute); + } + + public NonFungibleToken(BigInteger tokenId, AssetDefinition ad){ + this(tokenId); + ad.parseField(tokenId, this); + } + + public NonFungibleToken(BigInteger tokenId) { + id = tokenId; + attributes = new HashMap(); + } + } diff --git a/app/src/test/java/io/awallet/crypto/alphawallet/AssetDefinitionTest.java b/app/src/test/java/io/awallet/crypto/alphawallet/AssetDefinitionTest.java index f3bf6a3ef..2e068dab6 100644 --- a/app/src/test/java/io/awallet/crypto/alphawallet/AssetDefinitionTest.java +++ b/app/src/test/java/io/awallet/crypto/alphawallet/AssetDefinitionTest.java @@ -6,10 +6,7 @@ import org.xml.sax.SAXException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.math.BigInteger; -import java.util.HashMap; -import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -20,48 +17,23 @@ import io.awallet.crypto.alphawallet.repository.AssetDefinition; import io.awallet.crypto.alphawallet.repository.entity.NonFungibleToken; public class AssetDefinitionTest { - - class Ticket implements NonFungibleToken { - Map properties; - Map propertyNames; - - Ticket() { - properties = new HashMap<>(); - propertyNames = new HashMap<>(); - } - - @Override - public void setField(String id, String name, String value) { - properties.put(id, value); - propertyNames.put(id, name); - } - - @Override - public String getFieldName(String id) { - return propertyNames.get(id); - } - - @Override - public String getFieldText(String id) { - return properties.get(id); - } - } + BigInteger[] ticketIDs = { + BigInteger.valueOf(0x010CCB53), BigInteger.valueOf(0x010CCB54), + BigInteger.valueOf(0x02020075), BigInteger.valueOf(0x02020076) + }; + File file = new File("src/main/assets/ticket.xml"); @Test public void AssetDefinitionShouldParse() throws IOException, SAXException { - File file = new File("src/main/assets/ticket.xml"); assertTrue(file.exists()); - InputStream in = new FileInputStream(file); - assertNotNull(in); - AssetDefinition ticketAsset = new AssetDefinition(in, "en"); + AssetDefinition ticketAsset = new AssetDefinition(new FileInputStream(file), "en"); assertFalse(ticketAsset.fields.isEmpty()); - Ticket ticket = new Ticket(); - ticketAsset.parseField(BigInteger.valueOf(838483), ticket); - assertEquals("Number", ticket.getFieldName("number")); - assertEquals(Integer.valueOf(838483 % 65536).toString(), ticket.getFieldText("number")); - /* Epoch, the following test only works from Singapore - assertEquals("Thu Jan 01 07:30:00 SGT 1970", ticket.getFieldText("time")); */ + NonFungibleToken ticket = new NonFungibleToken(ticketIDs[0], ticketAsset); + assertEquals("Number", ticket.getAttribute("number").name); + assertEquals(BigInteger.valueOf(0xCB53), ticket.getAttribute("number").value); + /* Epoch, the following test only works from Singapore */ + assertEquals("Thu Jan 01 07:30:00 SGT 1970", ticket.getAttribute("time").text); } }