Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.27.6</version>
<version>0.36.1</version>
<configuration>
<excludes combine.children="append">
<exclude>checkstyle:/src/site/resources/.*</exclude>
Expand Down
104 changes: 6 additions & 98 deletions src/main/java/io/zold/api/Copies.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,30 @@
*/
package io.zold.api;

import io.zold.api.Copies.Copy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;

/**
* Multiple copies of a Wallet.
*
* @since 1.0
*/
public final class Copies extends IterableEnvelope<Copy> {

/**
* Ctor.
*
* @param id Id of the wallet to pull
* @param remotes Remote nodes
*/
Copies(final long id, final Iterable<Remote> remotes) {
super(new IterableOf<>(() -> copies(id, remotes).iterator()));
}

/**
* Builds copies from remotes.
* @param id Wallet's id
* @param remotes List of remotes
* @return Iterable Iterable of Copy
* @throws IOException If fails
*/
private static Iterable<Copy> copies(final long id,
final Iterable<Remote> remotes) throws IOException {
final List<Copy> copies = new ArrayList<>(10);
Expand All @@ -50,103 +42,19 @@ private static Iterable<Copy> copies(final long id,
}
}
if (!updated) {
copies.add(new Copies.Copy(wallet, remote));
copies.add(new Copy(wallet, remote));
}
}
return new IterableOf<>(copies.iterator());
}

/**
* Checks if content of two wallets is equal.
* @param first First wallet
* @param second Second wallet
* @return Boolean Boolean
* @throws IOException If fails
* @todo #56:30min Compare the entire content of two wallets. In addition
* to id, compare RSA key and all transactions one by one. Entire content
* of each transaction should be compared.
*/
// @todo #56:30min Compare the entire content of two wallets. In addition to
// id, compare RSA key and all transactions one by one. Entire content of
// each transaction should be compared.
private static boolean equalWallets(final Wallet first,
final Wallet second) throws IOException {
return first.id() == second.id() && new ListOf<>(
first.ledger()
).size() == new ListOf<>(second.ledger()).size();
}

/**
* One copy of a {@link Wallet}.
* @since 1.0
*/
static final class Copy implements Comparable<Copy> {

/**
* The wallet.
*/
private final Wallet wlt;

/**
* The remote nodes where the wallet was found.
*/
private final Iterable<Remote> remotes;

/**
* Ctor.
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Remote... remotes) {
this(wallet, new IterableOf<>(remotes));
}

/**
* Ctor.
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Iterable<Remote> remotes) {
this.wlt = wallet;
this.remotes = remotes;
}

@Override
public int compareTo(final Copy other) {
return this.score().compareTo(other.score());
}

@Override
public boolean equals(final Object obj) {
return obj instanceof Copy
&& this.compareTo((Copy) obj) == 0;
}

@Override
public int hashCode() {
return new ListOf<>(this.score().suffixes()).size();
}

/**
* Creates new Copy instance with additional remote.
* @param remote Remote
* @return Copy Copy
*/
Copies.Copy with(final Remote remote) {
return new Copies.Copy(this.wallet(), new Joined<>(remote, this.remotes));
}

/**
* The wallet.
* @return The wallet
*/
Wallet wallet() {
return this.wlt;
}

/**
* The summary of the score of all the remote nodes.
* @return The score
*/
Score score() {
return new Score.Summed(new Mapped<>(Remote::score, this.remotes));
}
}
}
93 changes: 93 additions & 0 deletions src/main/java/io/zold/api/Copy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2018-2026 Zerocracy
* SPDX-License-Identifier: MIT
*/
package io.zold.api;

import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;

/**
* One copy of a {@link Wallet}.
*
* @since 1.0
*/
final class Copy implements Comparable<Copy> {

/**
* The wallet.
*/
private final Wallet wlt;

/**
* The remote nodes where the wallet was found.
*/
private final Iterable<Remote> remotes;

/**
* Ctor.
*
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Remote... remotes) {
this(wallet, new IterableOf<>(remotes));
}

/**
* Ctor.
*
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Iterable<Remote> remotes) {
this.wlt = wallet;
this.remotes = remotes;
}

@Override
public int compareTo(final Copy other) {
return this.score().compareTo(other.score());
}

@Override
public boolean equals(final Object obj) {
return obj instanceof Copy
&& this.compareTo((Copy) obj) == 0;
}

@Override
public int hashCode() {
return new ListOf<>(this.score().suffixes()).size();
}

/**
* Creates new Copy instance with additional remote.
*
* @param remote Remote
* @return Copy Copy
*/
Copy with(final Remote remote) {
return new Copy(this.wallet(), new Joined<>(remote, this.remotes));
}

/**
* The wallet.
*
* @return The wallet
*/
Wallet wallet() {
return this.wlt;
}

/**
* The summary of the score of all the remote nodes.
*
* @return The score
*/
Score score() {
return new Score.Summed(new Mapped<>(Remote::score, this.remotes));
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/zold/api/CpTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* Computed Transaction.
*
* @since 1.0
* @todo #54:30min Implement the computation of the transaction string
* based on the white paper. The unit tests should also be updated to
Expand All @@ -20,9 +21,9 @@ public final class CpTransaction extends TransactionEnvelope {

/**
* Ctor.
*
* @param amt Amount to pay in zents
* @param bnf Wallet ID of beneficiary
* @checkstyle ConstructorsCodeFreeCheck (13 lines)
*/
CpTransaction(final long amt, final long bnf) {
super(
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/io/zold/api/Loaded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2018-2026 Zerocracy
* SPDX-License-Identifier: MIT
*/
package io.zold.api;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

/**
* Loaded remotes.
*
* @since 1.0
*/
final class Loaded implements Iterable<Remote> {

/**
* JSON file.
*/
private final Path file;

/**
* Descriptor loader.
*/
private final Function<JsonObject, Remote> loader;

/**
* New loaded remotes.
*
* @param file JSON file
* @param loader Descriptor loader
* @throws IOException If the file cannot be read
*/
Loaded(
final Path file,
final Function<JsonObject, Remote> loader
) throws IOException {
this.file = file;
this.loader = loader;
}

@Override
public Iterator<Remote> iterator() {
try {
return this.load().iterator();
} catch (final IOException err) {
throw new UncheckedIOException(err);
}
}

private Iterable<Remote> load() throws IOException {
final List<Remote> remotes = new ArrayList<>(0);
try (
JsonReader reader = Json.createReader(
Files.newBufferedReader(this.file, StandardCharsets.UTF_8)
)
) {
reader.readArray().stream()
.map(value -> (JsonObject) value)
.map(this.loader)
.forEach(remotes::add);
}
return remotes;
}
}
3 changes: 3 additions & 0 deletions src/main/java/io/zold/api/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

/**
* Network of remote nodes.
*
* @since 0.1
*/
public interface Network extends Iterable<Remote> {

/**
* Push the wallet to the network.
*
* @param wallet The wallet
*/
void push(Wallet wallet);

/**
* Pull a wallet from the network.
*
* @param id The wallet's {@link Wallet#id() id}
* @return The wallet
* @throws IOException If an IO error occurs
Expand Down
Loading
Loading