allowing ticket (non-fungible) objects to be created parsed, instead of requiring a parsing after the object is created

pull/77/head
colourful-land 7 years ago
parent b7e7ad755c
commit f2da5be6fe
  1. 4
      .gitignore
  2. 8
      app/src/main/java/io/awallet/crypto/alphawallet/entity/Ticket.java
  3. 11
      app/src/main/java/io/awallet/crypto/alphawallet/repository/AssetDefinition.java
  4. 49
      app/src/main/java/io/awallet/crypto/alphawallet/repository/entity/NonFungibleToken.java
  5. 50
      app/src/test/java/io/awallet/crypto/alphawallet/AssetDefinitionTest.java

4
.gitignore vendored

@ -43,6 +43,6 @@ proguard/
### Android Patch ###
gen-external-apklibs
*.*#
tn/trustnative.aar
tn/trustnative.aar

@ -30,9 +30,11 @@ public class Ticket extends Token implements Parcelable
{
public final List<Integer> balanceArray;
private List<Integer> 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;

@ -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);
}
}
}

@ -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<String, Attribute> 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();
}
}

@ -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<String,String> properties;
Map<String,String> 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);
}
}

Loading…
Cancel
Save