} interface.
10 | * This class implements the /ticker/{id} endpoint as described.
11 | *
12 | * Instances of this class should be obtained through {@link CoinMarketCap#ticker(Long)}.
13 | *
14 | * @author Luca Camphuisen < Luca.Camphuisen@hva.nl >
15 | * @since 19-11-17
16 | */
17 | public class CurrencyTicker implements Ticker {
18 |
19 | private ApiConnector apiConnector;
20 |
21 | /**
22 | * Create the ticker with the specified market we want to fetch.
23 | *
24 | * @param marketId the market id which is obtained through listings.
25 | * @throws IllegalArgumentException when the {@code coinName} argumeter that was passed is either null or empty.
26 | * This does not check for invalid coin names.
27 | */
28 | public CurrencyTicker(long marketId) {
29 | apiConnector = new ApiConnector().path("ticker/" + marketId);
30 | }
31 |
32 | /**
33 | * Add a price quote for the given currency
34 | *
35 | * @param currency currency to convert price to
36 | * @return the current builder
37 | * @see REST API Documentation for more detailed documentation regarding the REST endpoint.
38 | */
39 | public CurrencyTicker convert(Currency currency) {
40 | apiConnector = apiConnector.queryParam("convert", currency.name());
41 | return this;
42 | }
43 |
44 | /**
45 | * Obtain current market information of the specified currency.
46 | *
47 | * @return a {@link CoinMarket} response containing market information
48 | * @see Ticker#get()
49 | * @see CoinMarketApiResponse
50 | */
51 | @Override
52 | public CoinMarketApiResponse getApiResponse() {
53 | return apiConnector.getApiResponse(CoinMarketApiResponse.class);
54 | }
55 |
56 | /**
57 | * Obtain current market information of the specified currency.
58 | *
59 | * @return a {@link CoinMarket} response containing market information
60 | * @see Ticker#get()
61 | * @see CoinMarket
62 | */
63 | @Override
64 | public CoinMarket get() {
65 | return getApiResponse().getData();
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/api/GlobalTicker.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.api;
2 |
3 | import com.lucadev.coinmarketcap.CoinMarketCap;
4 | import com.lucadev.coinmarketcap.Currency;
5 | import com.lucadev.coinmarketcap.model.CoinMarketList;
6 | import com.lucadev.coinmarketcap.model.CoinMarketListApiResponse;
7 |
8 | /**
9 | * A price ticker/fetcher for multiple markets. This class implements the {@link Ticker< CoinMarketList >} interface.
10 | * This class implements the /ticker/ endpoint as described.
11 | *
12 | * Instances of this class should be obtained through {@link CoinMarketCap#ticker()}.
13 | *
14 | * @author Luca Camphuisen < Luca.Camphuisen@hva.nl >
15 | * @since 19-11-17
16 | */
17 | public class GlobalTicker implements Ticker {
18 |
19 | private ApiConnector apiConnector;
20 |
21 | /**
22 | * Instantiate a new {@code GlobalTicker}.
23 | * It is recommended to use {@link CoinMarketCap#ticker()} to build this class.
24 | */
25 | public GlobalTicker() {
26 | apiConnector = new ApiConnector().path("/ticker");
27 | }
28 |
29 | /**
30 | * Configure the starting position.
31 | *
32 | * @param start results from rank {@code start} and up
33 | * @return the current builder.
34 | * @see REST API Documentation for more detailed documentation regarding the REST endpoint.
35 | */
36 | public GlobalTicker setStart(int start) {
37 | apiConnector = apiConnector.queryParam("start", start);
38 | return this;
39 | }
40 |
41 | /**
42 | * Configure the rank limit position.
43 | *
44 | * @param limit maximum amount of results/markets to get. Default is 100. Use 0 to get all results/markets.
45 | * @return the current builder.
46 | * @see REST API Documentation for more detailed documentation regarding the REST endpoint.
47 | */
48 | public GlobalTicker setLimit(int limit) {
49 | apiConnector = apiConnector.queryParam("limit", limit);
50 | return this;
51 | }
52 |
53 | /**
54 | * Add a price quote for the given currency
55 | *
56 | * @param currency currency to convert price to
57 | * @return the current builder
58 | * @see REST API Documentation for more detailed documentation regarding the REST endpoint.
59 | */
60 | public GlobalTicker convert(Currency currency) {
61 | apiConnector = apiConnector.queryParam("convert", currency.name());
62 | return this;
63 | }
64 |
65 | /**
66 | * Obtain market information of multiple markets.
67 | *
68 | * @return a {@link CoinMarketList} response containing our markets.
69 | * @see Ticker#get()
70 | * @see CoinMarketList
71 | */
72 | @Override
73 | public CoinMarketListApiResponse getApiResponse() {
74 | return apiConnector.getApiResponse(CoinMarketListApiResponse.class);
75 | }
76 |
77 | /**
78 | * Obtain market information of multiple markets.
79 | *
80 | * @return a {@link CoinMarketList} response containing our markets.
81 | * @see Ticker#get()
82 | * @see CoinMarketList
83 | */
84 | @Override
85 | public CoinMarketList get() {
86 | return new CoinMarketList(getApiResponse().getData().values());
87 | }
88 |
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/api/Ticker.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.api;
2 |
3 | import com.lucadev.coinmarketcap.model.ApiResponse;
4 |
5 | /**
6 | * A basic interface that offers methodes required to implement a ticker functionality.
7 | *
8 | * @param the type of response that will be returned.
9 | * @author Luca Camphuisen < Luca.Camphuisen@hva.nl >
10 | * @since 21-11-17
11 | */
12 | public interface Ticker, E> {
13 |
14 | /**
15 | * Obtain a response from the ticker implementation.
16 | *
17 | * @return a full api response containing our requested information.
18 | */
19 | T getApiResponse();
20 |
21 | /**
22 | * Obtain a usable model from the ticker implementation.
23 | *
24 | * @return a valid response containing our requested information.
25 | */
26 | E get();
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/ApiResponse.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.lucadev.coinmarketcap.api.ApiResponseException;
4 |
5 | import java.util.Map;
6 |
7 | /**
8 | * Default CMC response format
9 | *
10 | * @author Luca Camphuisen
11 | * @since 6-5-18
12 | */
13 | public abstract class ApiResponse {
14 |
15 | public static final String JSON_DATA_PROPERTY = "data";
16 | public static final String JSON_METADATA_PROPERTY = "metadata";
17 | public static final String METADATA_ERROR_KEY = "error";
18 | public static final String METADATA_TIMESTAMP_KEY = "timestamp";
19 | protected final Map metadata;
20 | protected final T data;
21 |
22 | /**
23 | * Constructs the response model.
24 | *
25 | * @param data the response data.
26 | * @param metadata the metadata object in the response. Inside a map.
27 | */
28 | public ApiResponse(T data, Map metadata) {
29 | this.data = data;
30 | this.metadata = metadata;
31 | if (hasError()) {
32 | throw new ApiResponseException(this);
33 | }
34 | }
35 |
36 | /**
37 | * Actual data we are probably interested in.
38 | *
39 | * @return
40 | */
41 | public T getData() {
42 | return data;
43 | }
44 |
45 | /**
46 | * Metadata info map
47 | *
48 | * @return
49 | */
50 | public Map getMetadata() {
51 | return metadata;
52 | }
53 |
54 | /**
55 | * Get the erro
56 | *
57 | * @return
58 | */
59 | public String getError() {
60 | return String.valueOf(metadata.get(METADATA_ERROR_KEY));
61 | }
62 |
63 | /**
64 | * Check if the response contains an error
65 | *
66 | * @return
67 | */
68 | public boolean hasError() {
69 | return metadata.containsKey(METADATA_ERROR_KEY) && metadata.get(METADATA_ERROR_KEY) != null;
70 | }
71 |
72 | /**
73 | * Unix timestamp of request
74 | *
75 | * @return
76 | */
77 | public long getTimestamp() {
78 | return ((Integer) metadata.get(METADATA_TIMESTAMP_KEY)).longValue();
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinListing.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 |
6 | /**
7 | * Model to represent a single coin listing.
8 | *
9 | * @author Luca Camphuisen
10 | * @since 6-5-18
11 | */
12 | public class CoinListing {
13 |
14 | private final long id;
15 | private final String name;
16 | private final String symbol;
17 | private final String websiteSlug;
18 |
19 | @JsonCreator
20 | public CoinListing(@JsonProperty("id") long id, @JsonProperty("name") String name,
21 | @JsonProperty("symbol") String symbol, @JsonProperty("website_slug") String websiteSlug) {
22 | this.id = id;
23 | this.name = name;
24 | this.symbol = symbol;
25 | this.websiteSlug = websiteSlug;
26 | }
27 |
28 | @Override
29 | public String toString() {
30 | return "CoinListing{" +
31 | "id=" + id +
32 | ", name='" + name + '\'' +
33 | ", symbol='" + symbol + '\'' +
34 | ", websiteSlug='" + websiteSlug + '\'' +
35 | '}';
36 | }
37 |
38 | public long getId() {
39 | return id;
40 | }
41 |
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | public String getSymbol() {
47 | return symbol;
48 | }
49 |
50 | public String getWebsiteSlug() {
51 | return websiteSlug;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinListingList.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import java.util.*;
4 | import java.util.function.Consumer;
5 | import java.util.function.Predicate;
6 | import java.util.function.UnaryOperator;
7 | import java.util.stream.Stream;
8 |
9 | /**
10 | * Wrapper to manage a list of Coinlistings
11 | *
12 | * @author Luca Camphuisen
13 | * @since 6-5-18
14 | */
15 | public class CoinListingList implements List {
16 |
17 | private final List coinListings;
18 |
19 | /**
20 | * Construct from a {@link List}
21 | *
22 | * @param coinListings
23 | */
24 | public CoinListingList(List coinListings) {
25 | this.coinListings = coinListings;
26 | }
27 |
28 | private Optional search(Predicate predicate) {
29 | return coinListings.stream().filter(predicate).findFirst();
30 | }
31 |
32 | /**
33 | * Search through listings
34 | *
35 | * @param id the listing id to search for
36 | * @return the {@link Optional} with our {@link CoinListing} result
37 | */
38 | public Optional getById(long id) {
39 | return search(listing -> listing.getId() == id);
40 | }
41 |
42 | /**
43 | * Search through listings
44 | *
45 | * @param name the name of the listing. Case insensitive
46 | * @return the {@link Optional} with our {@link CoinListing} result
47 | */
48 | public Optional getByName(String name) {
49 | return search(listing -> listing.getName().equalsIgnoreCase(name));
50 | }
51 |
52 | /**
53 | * Search through listings
54 | *
55 | * @param symbol the symbol of the listing. Case insensitive
56 | * @return the {@link Optional} with our {@link CoinListing} result
57 | */
58 | public Optional getBySymbol(String symbol) {
59 | return search(listing -> listing.getSymbol().equalsIgnoreCase(symbol));
60 | }
61 |
62 | /**
63 | * Search through listings
64 | *
65 | * @param websiteSlug the website slug of the listing. Case insensitive
66 | * @return the {@link Optional} with our {@link CoinListing} result
67 | */
68 | public Optional getByWebsiteSlug(String websiteSlug) {
69 | return search(listing -> listing.getWebsiteSlug().equalsIgnoreCase(websiteSlug));
70 | }
71 |
72 | @Override
73 | public int size() {
74 | return coinListings.size();
75 | }
76 |
77 | @Override
78 | public boolean isEmpty() {
79 | return coinListings.isEmpty();
80 | }
81 |
82 | @Override
83 | public boolean contains(Object o) {
84 | return coinListings.contains(o);
85 | }
86 |
87 | @Override
88 | public Iterator iterator() {
89 | return coinListings.iterator();
90 | }
91 |
92 | @Override
93 | public Object[] toArray() {
94 | return coinListings.toArray();
95 | }
96 |
97 | @Override
98 | public T[] toArray(T[] ts) {
99 | return coinListings.toArray(ts);
100 | }
101 |
102 | @Override
103 | public boolean add(CoinListing coinListing) {
104 | return coinListings.add(coinListing);
105 | }
106 |
107 | @Override
108 | public boolean remove(Object o) {
109 | return coinListings.remove(o);
110 | }
111 |
112 | @Override
113 | public boolean containsAll(Collection> collection) {
114 | return coinListings.containsAll(collection);
115 | }
116 |
117 | @Override
118 | public boolean addAll(Collection extends CoinListing> collection) {
119 | return coinListings.addAll(collection);
120 | }
121 |
122 | @Override
123 | public boolean addAll(int i, Collection extends CoinListing> collection) {
124 | return coinListings.addAll(i, collection);
125 | }
126 |
127 | @Override
128 | public boolean removeAll(Collection> collection) {
129 | return coinListings.removeAll(collection);
130 | }
131 |
132 | @Override
133 | public boolean retainAll(Collection> collection) {
134 | return coinListings.retainAll(collection);
135 | }
136 |
137 | @Override
138 | public void replaceAll(UnaryOperator unaryOperator) {
139 | coinListings.replaceAll(unaryOperator);
140 | }
141 |
142 | @Override
143 | public void sort(Comparator super CoinListing> comparator) {
144 | coinListings.sort(comparator);
145 | }
146 |
147 | @Override
148 | public void clear() {
149 | coinListings.clear();
150 | }
151 |
152 | @Override
153 | public boolean equals(Object o) {
154 | return coinListings.equals(o);
155 | }
156 |
157 | @Override
158 | public int hashCode() {
159 | return coinListings.hashCode();
160 | }
161 |
162 | @Override
163 | public CoinListing get(int i) {
164 | return coinListings.get(i);
165 | }
166 |
167 | @Override
168 | public CoinListing set(int i, CoinListing coinListing) {
169 | return coinListings.set(i, coinListing);
170 | }
171 |
172 | @Override
173 | public void add(int i, CoinListing coinListing) {
174 | coinListings.add(i, coinListing);
175 | }
176 |
177 | @Override
178 | public CoinListing remove(int i) {
179 | return coinListings.remove(i);
180 | }
181 |
182 | @Override
183 | public int indexOf(Object o) {
184 | return coinListings.indexOf(o);
185 | }
186 |
187 | @Override
188 | public int lastIndexOf(Object o) {
189 | return coinListings.lastIndexOf(o);
190 | }
191 |
192 | @Override
193 | public ListIterator listIterator() {
194 | return coinListings.listIterator();
195 | }
196 |
197 | @Override
198 | public ListIterator listIterator(int i) {
199 | return coinListings.listIterator(i);
200 | }
201 |
202 | @Override
203 | public List subList(int i, int i1) {
204 | return coinListings.subList(i, i1);
205 | }
206 |
207 | @Override
208 | public Spliterator spliterator() {
209 | return coinListings.spliterator();
210 | }
211 |
212 | @Override
213 | public boolean removeIf(Predicate super CoinListing> predicate) {
214 | return coinListings.removeIf(predicate);
215 | }
216 |
217 | @Override
218 | public Stream stream() {
219 | return coinListings.stream();
220 | }
221 |
222 | @Override
223 | public Stream parallelStream() {
224 | return coinListings.parallelStream();
225 | }
226 |
227 | @Override
228 | public void forEach(Consumer super CoinListing> consumer) {
229 | coinListings.forEach(consumer);
230 | }
231 | }
232 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinListingsApiResponse.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | /**
10 | * Model for apibase/listings endpoint
11 | *
12 | * @author Luca Camphuisen
13 | * @since 6-5-18
14 | */
15 | public class CoinListingsApiResponse extends ApiResponse {
16 |
17 | protected static final String KEY_METADATA_LISTING_COUNT = "num_cryptocurrencies";
18 |
19 | /**
20 | * Constructs the response model.
21 | *
22 | * @param data the response data.
23 | * @param metadata the metadata object in the response. Inside a map.
24 | */
25 | @JsonCreator
26 | public CoinListingsApiResponse(@JsonProperty(JSON_DATA_PROPERTY) List data,
27 | @JsonProperty(JSON_METADATA_PROPERTY) Map metadata) {
28 | super(new CoinListingList(data), metadata);
29 | }
30 |
31 | /**
32 | * @return amount of listings
33 | */
34 | public long count() {
35 | return Long.parseLong((String) getMetadata().get(KEY_METADATA_LISTING_COUNT));
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinMarket.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import com.lucadev.coinmarketcap.Currency;
6 |
7 | import javax.xml.bind.annotation.XmlRootElement;
8 | import java.util.Date;
9 | import java.util.Map;
10 |
11 | /**
12 | * Immutable model used to display information about a single market.
13 | * Variable names should be self-explanatory. If not read the API docs.
14 | *
15 | * @author Luca Camphuisen < Luca.Camphuisen@hva.nl >
16 | * @since 21-11-17
17 | */
18 | @XmlRootElement
19 | public final class CoinMarket {
20 |
21 | private final long id;
22 | private final String name;
23 | private final String symbol;
24 | private final String websiteSlug;
25 | private final int rank;
26 | private final double circulatingSupply;
27 | private final double totalSupply;
28 | private final double maxSupply;
29 | private final Map priceQuotes;
30 | private final long lastUpdated;
31 |
32 | /**
33 | * Constructor to instantiate a {@code CoinMarket}.
34 | * Parameters should be self-explanatory.
35 | *
36 | * @param id
37 | * @param name
38 | * @param symbol
39 | * @param rank
40 | */
41 | @JsonCreator
42 | public CoinMarket(@JsonProperty("id") long id, @JsonProperty("name") String name,
43 | @JsonProperty("symbol") String symbol, @JsonProperty("website_slug") String websiteSlug,
44 | @JsonProperty("rank") int rank, @JsonProperty("circulating_supply") double circulatingSupply,
45 | @JsonProperty("total_supply") double totalSupply,
46 | @JsonProperty("max_supply") double maxSupply,
47 | @JsonProperty("quotes") Map priceQuotes,
48 | @JsonProperty("last_updated") long lastUpdated) {
49 | this.id = id;
50 | this.name = name;
51 | this.symbol = symbol;
52 | this.websiteSlug = websiteSlug;
53 | this.rank = rank;
54 | this.circulatingSupply = circulatingSupply;
55 | this.totalSupply = totalSupply;
56 | this.maxSupply = maxSupply;
57 | this.priceQuotes = priceQuotes;
58 | this.lastUpdated = lastUpdated;
59 | }
60 |
61 | public long getId() {
62 | return id;
63 | }
64 |
65 | public String getName() {
66 | return name;
67 | }
68 |
69 | public String getSymbol() {
70 | return symbol;
71 | }
72 |
73 | public String getWebsiteSlug() {
74 | return websiteSlug;
75 | }
76 |
77 | public int getRank() {
78 | return rank;
79 | }
80 |
81 | public double getCirculatingSupply() {
82 | return circulatingSupply;
83 | }
84 |
85 | public double getTotalSupply() {
86 | return totalSupply;
87 | }
88 |
89 | public double getMaxSupply() {
90 | return maxSupply;
91 | }
92 |
93 | public Map getPriceQuotes() {
94 | return priceQuotes;
95 | }
96 |
97 | public PriceQuote getPriceQuote(Currency currency) {
98 | return getPriceQuotes().get(currency);
99 | }
100 |
101 | public PriceQuote getUSDPriceQuote() {
102 | return getPriceQuote(Currency.USD);
103 | }
104 |
105 | public long getLastUpdated() {
106 | return lastUpdated;
107 | }
108 |
109 | public Date getLastUpdatedDate() {
110 | return new Date(getLastUpdated());
111 | }
112 |
113 | @Override
114 | public String toString() {
115 | return "CoinMarket{" +
116 | "id=" + id +
117 | ", name='" + name + '\'' +
118 | ", symbol='" + symbol + '\'' +
119 | ", websiteSlug='" + websiteSlug + '\'' +
120 | ", rank=" + rank +
121 | ", circulatingSupply=" + circulatingSupply +
122 | ", totalSupply=" + totalSupply +
123 | ", maxSupply=" + maxSupply +
124 | ", priceQuotes=" + priceQuotes +
125 | ", lastUpdated=" + lastUpdated +
126 | '}';
127 | }
128 |
129 | @Override
130 | public boolean equals(Object o) {
131 | if (this == o) return true;
132 | if (o == null || getClass() != o.getClass()) return false;
133 |
134 | CoinMarket market = (CoinMarket) o;
135 |
136 | if (id != market.id) return false;
137 | if (rank != market.rank) return false;
138 | if (Double.compare(market.circulatingSupply, circulatingSupply) != 0) return false;
139 | if (Double.compare(market.totalSupply, totalSupply) != 0) return false;
140 | if (Double.compare(market.maxSupply, maxSupply) != 0) return false;
141 | if (lastUpdated != market.lastUpdated) return false;
142 | if (name != null ? !name.equals(market.name) : market.name != null) return false;
143 | if (symbol != null ? !symbol.equals(market.symbol) : market.symbol != null) return false;
144 | if (websiteSlug != null ? !websiteSlug.equals(market.websiteSlug) : market.websiteSlug != null) return false;
145 | return priceQuotes != null ? priceQuotes.equals(market.priceQuotes) : market.priceQuotes == null;
146 |
147 | }
148 |
149 | @Override
150 | public int hashCode() {
151 | int result;
152 | long temp;
153 | result = (int) (id ^ (id >>> 32));
154 | result = 31 * result + (name != null ? name.hashCode() : 0);
155 | result = 31 * result + (symbol != null ? symbol.hashCode() : 0);
156 | result = 31 * result + (websiteSlug != null ? websiteSlug.hashCode() : 0);
157 | result = 31 * result + rank;
158 | temp = Double.doubleToLongBits(circulatingSupply);
159 | result = 31 * result + (int) (temp ^ (temp >>> 32));
160 | temp = Double.doubleToLongBits(totalSupply);
161 | result = 31 * result + (int) (temp ^ (temp >>> 32));
162 | temp = Double.doubleToLongBits(maxSupply);
163 | result = 31 * result + (int) (temp ^ (temp >>> 32));
164 | result = 31 * result + (priceQuotes != null ? priceQuotes.hashCode() : 0);
165 | result = 31 * result + (int) (lastUpdated ^ (lastUpdated >>> 32));
166 | return result;
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinMarketApiResponse.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 |
6 | import java.util.Map;
7 |
8 | /**
9 | * API response for a single market
10 | *
11 | * @author Luca Camphuisen
12 | * @since 6-5-18
13 | */
14 | public class CoinMarketApiResponse extends ApiResponse {
15 |
16 | /**
17 | * Constructs the response model.
18 | *
19 | * @param data the response data.
20 | * @param metadata the metadata object in the response. Inside a map.
21 | */
22 | @JsonCreator
23 | public CoinMarketApiResponse(@JsonProperty(JSON_DATA_PROPERTY) CoinMarket data,
24 | @JsonProperty(JSON_METADATA_PROPERTY) Map metadata) {
25 | super(data, metadata);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinMarketList.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import java.util.*;
4 | import java.util.function.Consumer;
5 | import java.util.function.Predicate;
6 | import java.util.function.UnaryOperator;
7 | import java.util.stream.Stream;
8 |
9 | /**
10 | * Immutable model used to handle a response containing multiple marketList.
11 | *
12 | * @author Luca Camphuisen < Luca.Camphuisen@hva.nl >
13 | * @since 21-11-17
14 | */
15 | public class CoinMarketList implements List {
16 |
17 | private final List marketList;
18 |
19 | /**
20 | * Instantiate the model with the required arguments.
21 | *
22 | * @param markets a {@link List} of market models.
23 | */
24 | public CoinMarketList(Collection markets) {
25 | if (markets == null) {
26 | throw new NullPointerException("markets argument may not be null.");
27 | }
28 | this.marketList = Collections.unmodifiableList(new ArrayList<>(markets));
29 | }
30 |
31 | /**
32 | * Find a market in the market list by linear searching through the list.
33 | *
34 | * @param marketId the id of the market such as: bitcoin, litecoin
35 | * @return a valid {@link CoinMarket} or null if we could not find a market.
36 | */
37 | public CoinMarket getByName(String marketId) {
38 | if (marketId == null || marketId.isEmpty()) {
39 | return null;
40 | }
41 | return marketList.stream()
42 | .filter(market -> market.getName().equalsIgnoreCase(marketId))
43 | .findFirst().orElse(null);
44 | }
45 |
46 | /**
47 | * Find a market in the market list by linear searching through the list.
48 | *
49 | * @param symbol the symbol of the market such as BTC, LTC
50 | * @return a valid {@link CoinMarket} or null if we could not find a market.
51 | */
52 | public CoinMarket getBySymbol(String symbol) {
53 | if (symbol == null || symbol.isEmpty()) {
54 | return null;
55 | }
56 | return marketList.stream()
57 | .filter(market -> market.getSymbol().equalsIgnoreCase(symbol))
58 | .findFirst().orElse(null);
59 | }
60 |
61 | @Override
62 | public int size() {
63 | return marketList.size();
64 | }
65 |
66 | @Override
67 | public boolean isEmpty() {
68 | return marketList.isEmpty();
69 | }
70 |
71 | @Override
72 | public boolean contains(Object o) {
73 | return marketList.contains(o);
74 | }
75 |
76 | @Override
77 | public Iterator iterator() {
78 | return marketList.iterator();
79 | }
80 |
81 | @Override
82 | public Object[] toArray() {
83 | return marketList.toArray();
84 | }
85 |
86 | @Override
87 | public T[] toArray(T[] ts) {
88 | return marketList.toArray(ts);
89 | }
90 |
91 | @Override
92 | public boolean add(CoinMarket coinMarket) {
93 | return marketList.add(coinMarket);
94 | }
95 |
96 | @Override
97 | public boolean remove(Object o) {
98 | return marketList.remove(o);
99 | }
100 |
101 | @Override
102 | public boolean containsAll(Collection> collection) {
103 | return marketList.containsAll(collection);
104 | }
105 |
106 | @Override
107 | public boolean addAll(Collection extends CoinMarket> collection) {
108 | return marketList.addAll(collection);
109 | }
110 |
111 | @Override
112 | public boolean addAll(int i, Collection extends CoinMarket> collection) {
113 | return marketList.addAll(i, collection);
114 | }
115 |
116 | @Override
117 | public boolean removeAll(Collection> collection) {
118 | return marketList.removeAll(collection);
119 | }
120 |
121 | @Override
122 | public boolean retainAll(Collection> collection) {
123 | return marketList.retainAll(collection);
124 | }
125 |
126 | @Override
127 | public void replaceAll(UnaryOperator unaryOperator) {
128 | marketList.replaceAll(unaryOperator);
129 | }
130 |
131 | @Override
132 | public void sort(Comparator super CoinMarket> comparator) {
133 | marketList.sort(comparator);
134 | }
135 |
136 | @Override
137 | public void clear() {
138 | marketList.clear();
139 | }
140 |
141 | @Override
142 | public boolean equals(Object o) {
143 | return marketList.equals(o);
144 | }
145 |
146 | @Override
147 | public int hashCode() {
148 | return marketList.hashCode();
149 | }
150 |
151 | @Override
152 | public CoinMarket get(int i) {
153 | return marketList.get(i);
154 | }
155 |
156 | @Override
157 | public CoinMarket set(int i, CoinMarket coinMarket) {
158 | return marketList.set(i, coinMarket);
159 | }
160 |
161 | @Override
162 | public void add(int i, CoinMarket coinMarket) {
163 | marketList.add(i, coinMarket);
164 | }
165 |
166 | @Override
167 | public CoinMarket remove(int i) {
168 | return marketList.remove(i);
169 | }
170 |
171 | @Override
172 | public int indexOf(Object o) {
173 | return marketList.indexOf(o);
174 | }
175 |
176 | @Override
177 | public int lastIndexOf(Object o) {
178 | return marketList.lastIndexOf(o);
179 | }
180 |
181 | @Override
182 | public ListIterator listIterator() {
183 | return marketList.listIterator();
184 | }
185 |
186 | @Override
187 | public ListIterator listIterator(int i) {
188 | return marketList.listIterator(i);
189 | }
190 |
191 | @Override
192 | public List subList(int i, int i1) {
193 | return marketList.subList(i, i1);
194 | }
195 |
196 | @Override
197 | public Spliterator spliterator() {
198 | return marketList.spliterator();
199 | }
200 |
201 | @Override
202 | public boolean removeIf(Predicate super CoinMarket> predicate) {
203 | return marketList.removeIf(predicate);
204 | }
205 |
206 | @Override
207 | public Stream stream() {
208 | return marketList.stream();
209 | }
210 |
211 | @Override
212 | public Stream parallelStream() {
213 | return marketList.parallelStream();
214 | }
215 |
216 | @Override
217 | public void forEach(Consumer super CoinMarket> consumer) {
218 | marketList.forEach(consumer);
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/src/main/java/com/lucadev/coinmarketcap/model/CoinMarketListApiResponse.java:
--------------------------------------------------------------------------------
1 | package com.lucadev.coinmarketcap.model;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 |
6 | import java.util.Map;
7 |
8 | /**
9 | * @author Luca Camphuisen
10 | * @since 6-5-18
11 | */
12 | public class CoinMarketListApiResponse extends ApiResponse