|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Yegor Bugayenko |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included |
| 14 | + * in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package io.zold.api; |
| 25 | + |
| 26 | +import java.util.Iterator; |
| 27 | + |
| 28 | +/** |
| 29 | + * Network implementation. |
| 30 | + * |
| 31 | + * @since 0.1 |
| 32 | + * @todo #5:30min We must figure out how to 'load' some network. Loading the |
| 33 | + * network will be loading a local JSON file that contains data on all |
| 34 | + * remote nodes that we know about; we must have a pre configured set of |
| 35 | + * remote nodes built in too. See whitepaper for details. |
| 36 | + */ |
| 37 | +public final class RtNetwork implements Network { |
| 38 | + |
| 39 | + /** |
| 40 | + * {@link Remote} nodes. |
| 41 | + */ |
| 42 | + private final Iterable<Remote> nodes; |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor. |
| 46 | + * @param remotes Remotes of the network |
| 47 | + */ |
| 48 | + RtNetwork(final Iterable<Remote> remotes) { |
| 49 | + this.nodes = remotes; |
| 50 | + } |
| 51 | + |
| 52 | + // @todo #5:30min Implement scoring algorithm when paying taxes. Scoring |
| 53 | + // algorithm must select the node with the highest score and with score |
| 54 | + // >= 16. There are some tests for the scoring algorithm in NetworkTest: |
| 55 | + // remove ignore tag from them after algorithm implementation. |
| 56 | + @Override |
| 57 | + public void push(final Wallet wallet) { |
| 58 | + this.nodes.forEach( |
| 59 | + remote -> remote.push(wallet) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // @todo #5:30min Implement pull method. Pulling a wallet from the |
| 64 | + // network should return all the wallets with that id in the |
| 65 | + // network merged together. After the implementation |
| 66 | + // NetworkTest.pullIsNotYetImplemented() have to be uncommented and |
| 67 | + // test if pull method is behaving correctly. |
| 68 | + @Override |
| 69 | + public Wallet pull(final Long id) { |
| 70 | + throw new UnsupportedOperationException("pull(id) not supported"); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Iterator<Remote> iterator() { |
| 75 | + return this.nodes.iterator(); |
| 76 | + } |
| 77 | +} |
0 commit comments