OSDN Git Service

a little modify
[bytom/bytom-java-sdk.git] / src / main / java / io / bytom / api / Balance.java
1 package io.bytom.api;
2
3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.common.Utils;
5 import io.bytom.exception.BytomException;
6 import io.bytom.http.Client;
7 import org.apache.log4j.Logger;
8
9 import java.lang.reflect.Type;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13
14 public class Balance {
15
16     /**
17      * account id
18      */
19     @SerializedName("account_id")
20     public String accountId;
21
22     /**
23      * name of account
24      */
25     @SerializedName("account_alias")
26     public String accountAlias;
27
28     /**
29      * sum of the unspent outputs.
30      * specified asset balance of account.
31      */
32     public long amount;
33
34     /**
35      * asset id
36      */
37     @SerializedName("asset_id")
38     public String assetId;
39
40     /**
41      * name of asset
42      */
43     @SerializedName("asset_alias")
44     public String assetAlias;
45
46     @SerializedName("asset_definition")
47     public Map<String, Object> definition;
48
49     private static Logger logger = Logger.getLogger(Balance.class);
50
51     /**
52      * Serializes the Balance into a form that is safe to transfer over the wire.
53      *
54      * @return the JSON-serialized representation of the Receiver object
55      */
56     public String toJson() {
57         return Utils.serializer.toJson(this);
58     }
59
60
61     public static class QueryBuilder {
62
63         /**
64          * Call list-Balances api
65          *
66          * @param client
67          * @return
68          * @throws BytomException
69          */
70         public List<Balance> list(Client client) throws BytomException {
71
72             Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Balance.class});
73             List<Balance> balanceList = client.request("list-balances", null, listType);
74             logger.info("list-balances:");
75             logger.info("size of :" + balanceList.size());
76             for (Balance result : balanceList) {
77                 logger.info(result.toJson());
78             }
79
80             return balanceList;
81         }
82
83         /**
84          * sum of all Asset alias amount
85          *
86          * @param client
87          * @param assetAlias
88          * @return
89          * @throws BytomException
90          */
91         public Balance listByAssetAlias(Client client, String assetAlias) throws BytomException {
92             List<Balance> balanceList = list(client);
93             Balance assetBalance = new Balance();
94             assetBalance.assetAlias = assetAlias;
95             long amount = 0;
96             for (Balance result : balanceList) {
97                 if (result.assetAlias.equals(assetAlias)) {
98                     amount += result.amount;
99                     assetBalance.assetId = result.assetId;
100                 }
101             }
102             assetBalance.amount = amount;
103
104             logger.info(assetBalance.toJson());
105
106             return assetBalance;
107         }
108
109         /**
110          * sum of all Account alias amount
111          *
112          * @param client
113          * @param accountAlias
114          * @return
115          * @throws BytomException
116          */
117         public List<Balance> listByAccountAlias(Client client, String accountAlias) throws BytomException {
118             List<Balance> balanceList = list(client);
119             List<Balance> accountBalance = new ArrayList<>();
120             for (Balance result : balanceList) {
121                 if (result.accountAlias.equals(accountAlias)) {
122                     accountBalance.add(result);
123                     logger.info(result.toJson());
124                 }
125             }
126             return accountBalance;
127         }
128     }
129 }