Sitelet https://github.com/corda/samples-java/commit/cd2b41b474867e89dccd66a88f2368473d1b770e
Skip to content

Commit cd2b41b

Browse files
authored
ENT-6871: Clean up & formatting for tutorial-applestamp (#82)
Removed unused imports and applied formatting.
1 parent 5aef5e1 commit cd2b41b

13 files changed

Lines changed: 64 additions & 78 deletions

File tree

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.tutorial.contracts;
22

33
import com.tutorial.states.AppleStamp;
4-
import com.tutorial.states.BasketOfApples;
54
import net.corda.core.contracts.CommandData;
65
import net.corda.core.contracts.Contract;
76
import net.corda.core.transactions.LedgerTransaction;
87
import org.jetbrains.annotations.NotNull;
98

10-
import static net.corda.core.contracts.ContractsDSL.requireThat; //Domain Specific Language
11-
9+
import static net.corda.core.contracts.ContractsDSL.requireThat;
1210

1311
public class AppleStampContract implements Contract {
1412

@@ -22,17 +20,16 @@ public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentExceptio
2220
final CommandData commandData = tx.getCommands().get(0).getValue();
2321

2422
//Verify the transaction according to the intention of the transaction
25-
if (commandData instanceof AppleStampContract.Commands.Issue){
23+
if (commandData instanceof AppleStampContract.Commands.Issue) {
2624
AppleStamp output = tx.outputsOfType(AppleStamp.class).get(0);
2725
requireThat(require -> {
2826
require.using("This transaction should only have one AppleStamp state as output", tx.getOutputs().size() == 1);
2927
require.using("The output AppleStamp state should have clear description of the type of redeemable goods", !output.getStampDesc().equals(""));
3028
return null;
3129
});
32-
}else if(commandData instanceof BasketOfApplesContract.Commands.Redeem){
30+
} else if (commandData instanceof BasketOfApplesContract.Commands.Redeem) {
3331
//Transaction verification will happen in BasketOfApples Contract
34-
}
35-
else{
32+
} else {
3633
//Unrecognized Command type
3734
throw new IllegalArgumentException("Incorrect type of AppleStamp Commands");
3835
}
@@ -41,6 +38,7 @@ public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentExceptio
4138
// Used to indicate the transaction's intent.
4239
public interface Commands extends CommandData {
4340
//In our hello-world app, We will have two commands.
44-
class Issue implements AppleStampContract.Commands {}
41+
class Issue implements AppleStampContract.Commands {
42+
}
4543
}
4644
}

‎Basic/tutorial-applestamp/contracts/src/main/java/com/tutorial/contracts/BasketOfApplesContract.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ public class BasketOfApplesContract implements Contract {
1414
// This is used to identify our contract when building a transaction.
1515
public static final String ID = "com.tutorial.contracts.BasketOfApplesContract";
1616

17-
1817
@Override
1918
public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentException {
2019
//Extract the command from the transaction.
2120
final CommandData commandData = tx.getCommands().get(0).getValue();
2221

23-
if (commandData instanceof BasketOfApplesContract.Commands.packBasket){
22+
if (commandData instanceof BasketOfApplesContract.Commands.packBasket) {
2423
BasketOfApples output = tx.outputsOfType(BasketOfApples.class).get(0);
2524
requireThat(require -> {
2625
require.using("This transaction should only output one BasketOfApples state", tx.getOutputs().size() == 1);
2726
require.using("The output BasketOfApples state should have clear description of Apple product", !output.getDescription().equals(""));
2827
require.using("The output BasketOfApples state should have non zero weight", output.getWeight() > 0);
2928
return null;
3029
});
31-
}
32-
else if (commandData instanceof BasketOfApplesContract.Commands.Redeem) {
30+
} else if (commandData instanceof BasketOfApplesContract.Commands.Redeem) {
3331
//Retrieve the output state of the transaction
3432
AppleStamp input = tx.inputsOfType(AppleStamp.class).get(0);
3533
BasketOfApples output = tx.outputsOfType(BasketOfApples.class).get(0);
@@ -41,17 +39,18 @@ else if (commandData instanceof BasketOfApplesContract.Commands.Redeem) {
4139
require.using("The basket of apple has to weight more than 0", output.getWeight() > 0);
4240
return null;
4341
});
44-
}
45-
else{
42+
} else {
4643
//Unrecognized Command type
4744
throw new IllegalArgumentException("Incorrect type of BasketOfApples Commands");
4845
}
4946
}
5047

5148
// Used to indicate the transaction's intent.
5249
public interface Commands extends CommandData {
53-
class packBasket implements BasketOfApplesContract.Commands {}
54-
class Redeem implements BasketOfApplesContract.Commands {}
50+
class packBasket implements BasketOfApplesContract.Commands {
51+
}
5552

53+
class Redeem implements BasketOfApplesContract.Commands {
54+
}
5655
}
5756
}

‎Basic/tutorial-applestamp/contracts/src/main/java/com/tutorial/contracts/TemplateContract.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void verify(LedgerTransaction tx) {
4040
// Used to indicate the transaction's intent.
4141
public interface Commands extends CommandData {
4242
//In our hello-world app, We will only have one command.
43-
class Send implements Commands {}
43+
class Send implements Commands {
44+
}
4445
}
4546
}

‎Basic/tutorial-applestamp/contracts/src/test/java/com/tutorial/contracts/ContractTests.java‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212

1313
import static net.corda.testing.node.NodeTestUtils.ledger;
1414

15-
1615
public class ContractTests {
1716
private final MockServices ledgerServices = new MockServices(Arrays.asList("com.tutorial.contracts"));
18-
TestIdentity alice = new TestIdentity(new CordaX500Name("Alice", "TestLand", "US"));
19-
TestIdentity bob = new TestIdentity(new CordaX500Name("Alice", "TestLand", "US"));
17+
TestIdentity alice = new TestIdentity(new CordaX500Name("Alice", "TestLand", "US"));
18+
TestIdentity bob = new TestIdentity(new CordaX500Name("Alice", "TestLand", "US"));
2019

2120
//Template Tester
2221
@Test
2322
public void issuerAndRecipientCannotHaveSameEmail() {
24-
TemplateState state = new TemplateState("Hello-World",alice.getParty(),bob.getParty());
23+
TemplateState state = new TemplateState("Hello-World", alice.getParty(), bob.getParty());
2524
ledger(ledgerServices, l -> {
2625
l.transaction(tx -> {
2726
tx.input(TemplateContract.ID, state);
@@ -40,9 +39,9 @@ public void issuerAndRecipientCannotHaveSameEmail() {
4039

4140
//Basket of Apple cordapp testers
4241
@Test
43-
public void StampIssuanceCanOnlyHaveOneOutput(){
44-
AppleStamp stamp = new AppleStamp("FUji4072", alice.getParty(),bob.getParty(),new UniqueIdentifier());
45-
AppleStamp stamp2 = new AppleStamp("HoneyCrispy7864", alice.getParty(),bob.getParty(),new UniqueIdentifier());
42+
public void StampIssuanceCanOnlyHaveOneOutput() {
43+
AppleStamp stamp = new AppleStamp("FUji4072", alice.getParty(), bob.getParty(), new UniqueIdentifier());
44+
AppleStamp stamp2 = new AppleStamp("HoneyCrispy7864", alice.getParty(), bob.getParty(), new UniqueIdentifier());
4645

4746
ledger(ledgerServices, l -> {
4847
l.transaction(tx -> {
@@ -61,24 +60,22 @@ public void StampIssuanceCanOnlyHaveOneOutput(){
6160
}
6261

6362
@Test
64-
public void StampMustHaveDescription(){
65-
AppleStamp stamp = new AppleStamp("", alice.getParty(),bob.getParty(),new UniqueIdentifier());
66-
AppleStamp stamp2 = new AppleStamp("FUji4072", alice.getParty(),bob.getParty(),new UniqueIdentifier());
63+
public void StampMustHaveDescription() {
64+
AppleStamp stamp = new AppleStamp("", alice.getParty(), bob.getParty(), new UniqueIdentifier());
65+
AppleStamp stamp2 = new AppleStamp("FUji4072", alice.getParty(), bob.getParty(), new UniqueIdentifier());
6766

6867
ledger(ledgerServices, l -> {
6968
l.transaction(tx -> {
7069
tx.output(AppleStampContract.ID, stamp);
71-
tx.command(Arrays.asList(alice.getPublicKey(),bob.getPublicKey()), new AppleStampContract.Commands.Issue());
70+
tx.command(Arrays.asList(alice.getPublicKey(), bob.getPublicKey()), new AppleStampContract.Commands.Issue());
7271
return tx.fails(); //fails because of having inputs
7372
});
7473
l.transaction(tx -> {
7574
tx.output(AppleStampContract.ID, stamp2);
76-
tx.command(Arrays.asList(alice.getPublicKey(),bob.getPublicKey()), new AppleStampContract.Commands.Issue());
75+
tx.command(Arrays.asList(alice.getPublicKey(), bob.getPublicKey()), new AppleStampContract.Commands.Issue());
7776
return tx.verifies();
7877
});
7978
return null;
8079
});
8180
}
82-
83-
8481
}

‎Basic/tutorial-applestamp/contracts/src/test/java/com/tutorial/contracts/StateTests.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ public void AppleStampStateHasFieldOfCorrectType() throws NoSuchFieldException {
2525
AppleStamp.class.getDeclaredField("holder");
2626
assert (AppleStamp.class.getDeclaredField("issuer").getType().equals(Party.class));
2727
}
28-
2928
}

‎Basic/tutorial-applestamp/workflows/src/main/java/com/tutorial/flows/CreateAndIssueAppleStamp.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
import com.tutorial.states.AppleStamp;
66
import net.corda.core.contracts.UniqueIdentifier;
77
import net.corda.core.flows.*;
8+
import net.corda.core.identity.CordaX500Name;
89
import net.corda.core.identity.Party;
910
import net.corda.core.transactions.SignedTransaction;
1011
import net.corda.core.transactions.TransactionBuilder;
1112

12-
import java.util.ArrayList;
1313
import java.util.Arrays;
14-
import net.corda.core.identity.CordaX500Name;
1514

1615
public class CreateAndIssueAppleStamp {
1716

1817
@InitiatingFlow
1918
@StartableByRPC
20-
public static class CreateAndIssueAppleStampInitiator extends FlowLogic<SignedTransaction>{
19+
public static class CreateAndIssueAppleStampInitiator extends FlowLogic<SignedTransaction> {
2120

2221
private String stampDescription;
2322
private Party holder;
2423

25-
public CreateAndIssueAppleStampInitiator(String stampDescription, Party holder) {
24+
public CreateAndIssueAppleStampInitiator(String stampDescription, Party holder) {
2625
this.stampDescription = stampDescription;
2726
this.holder = holder;
2827
}
@@ -37,13 +36,13 @@ public SignedTransaction call() throws FlowException {
3736

3837
//Building the output AppleStamp state
3938
UniqueIdentifier uniqueID = new UniqueIdentifier();
40-
AppleStamp newStamp = new AppleStamp(this.stampDescription,this.getOurIdentity(),this.holder,uniqueID);
39+
AppleStamp newStamp = new AppleStamp(this.stampDescription, this.getOurIdentity(), this.holder, uniqueID);
4140

4241
//Compositing the transaction
4342
TransactionBuilder txBuilder = new TransactionBuilder(notary)
4443
.addOutputState(newStamp)
4544
.addCommand(new AppleStampContract.Commands.Issue(),
46-
Arrays.asList(getOurIdentity().getOwningKey(),holder.getOwningKey()));
45+
Arrays.asList(getOurIdentity().getOwningKey(), holder.getOwningKey()));
4746

4847
// Verify that the transaction is valid.
4948
txBuilder.verify(getServiceHub());
@@ -62,7 +61,7 @@ public SignedTransaction call() throws FlowException {
6261
}
6362

6463
@InitiatedBy(CreateAndIssueAppleStampInitiator.class)
65-
public static class CreateAndIssueAppleStampResponder extends FlowLogic<Void>{
64+
public static class CreateAndIssueAppleStampResponder extends FlowLogic<Void> {
6665

6766
//private variable
6867
private FlowSession counterpartySession;
@@ -96,7 +95,6 @@ protected void checkTransaction(SignedTransaction stx) throws FlowException {
9695
return null;
9796
}
9897
}
99-
10098
}
10199

102100
//flow start CreateAndIssueAppleStampInitiator stampDescription: Fuji0472, holder: Peter

‎Basic/tutorial-applestamp/workflows/src/main/java/com/tutorial/flows/PackageApples.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.tutorial.contracts.BasketOfApplesContract;
55
import com.tutorial.states.BasketOfApples;
66
import net.corda.core.flows.*;
7+
import net.corda.core.identity.CordaX500Name;
78
import net.corda.core.identity.Party;
89
import net.corda.core.transactions.SignedTransaction;
910
import net.corda.core.transactions.TransactionBuilder;
1011

1112
import java.util.Collections;
12-
import net.corda.core.identity.CordaX500Name;
1313

1414
public class PackageApples {
1515

@@ -34,7 +34,7 @@ public SignedTransaction call() throws FlowException {
3434
final Party notary = getServiceHub().getNetworkMapCache().getNotary(CordaX500Name.parse("O=Notary,L=London,C=GB"));
3535

3636
//Create the output object
37-
BasketOfApples basket = new BasketOfApples(this.appleDescription,this.getOurIdentity(),this.weight);
37+
BasketOfApples basket = new BasketOfApples(this.appleDescription, this.getOurIdentity(), this.weight);
3838

3939
//Building transaction
4040
TransactionBuilder txBuilder = new TransactionBuilder(notary)
@@ -51,7 +51,6 @@ public SignedTransaction call() throws FlowException {
5151
return subFlow(new FinalityFlow(signedTransaction, Collections.emptyList()));
5252
}
5353
}
54-
5554
}
5655

5756
//flow start PackApplesInitiator appleDescription: Fuji4072, weight: 10

‎Basic/tutorial-applestamp/workflows/src/main/java/com/tutorial/flows/RedeemApples.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.tutorial.flows;
22

33
import co.paralleluniverse.fibers.Suspendable;
4-
import com.tutorial.contracts.AppleStampContract;
54
import com.tutorial.contracts.BasketOfApplesContract;
65
import com.tutorial.states.AppleStamp;
76
import com.tutorial.states.BasketOfApples;
@@ -21,7 +20,7 @@ public class RedeemApples {
2120

2221
@InitiatingFlow
2322
@StartableByRPC
24-
public static class RedeemApplesInitiator extends FlowLogic<SignedTransaction>{
23+
public static class RedeemApplesInitiator extends FlowLogic<SignedTransaction> {
2524

2625
private Party buyer;
2726
private UniqueIdentifier stampId;
@@ -61,7 +60,7 @@ public SignedTransaction call() throws FlowException {
6160
.addInputState(BasketOfApplesStateAndRef)
6261
.addOutputState(output, BasketOfApplesContract.ID)
6362
.addCommand(new BasketOfApplesContract.Commands.Redeem(),
64-
Arrays.asList(getOurIdentity().getOwningKey(),this.buyer.getOwningKey()));
63+
Arrays.asList(getOurIdentity().getOwningKey(), this.buyer.getOwningKey()));
6564

6665
// Verify that the transaction is valid.
6766
txBuilder.verify(getServiceHub());
@@ -82,7 +81,7 @@ public SignedTransaction call() throws FlowException {
8281
}
8382

8483
@InitiatedBy(RedeemApplesInitiator.class)
85-
public static class RedeemApplesResponder extends FlowLogic<Void>{
84+
public static class RedeemApplesResponder extends FlowLogic<Void> {
8685
//private variable
8786
private FlowSession counterpartySession;
8887

@@ -104,6 +103,5 @@ protected void checkTransaction(SignedTransaction stx) throws FlowException {
104103
return null;
105104
}
106105
}
107-
108106
}
109107
//flow start RedeemApplesInitiator buyer: Peter, stampId:

‎Basic/tutorial-applestamp/workflows/src/main/java/com/tutorial/flows/TemplateInitiator.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.tutorial.contracts.TemplateContract;
55
import com.tutorial.states.TemplateState;
66
import net.corda.core.flows.*;
7+
import net.corda.core.identity.CordaX500Name;
78
import net.corda.core.identity.Party;
89
import net.corda.core.transactions.SignedTransaction;
910
import net.corda.core.transactions.TransactionBuilder;
@@ -12,7 +13,6 @@
1213
import java.util.Arrays;
1314
import java.util.List;
1415
import java.util.stream.Collectors;
15-
import net.corda.core.identity.CordaX500Name;
1616

1717
// ******************
1818
// * TemplateInitiator flow *
@@ -23,17 +23,18 @@ public class TemplateInitiator extends FlowLogic<SignedTransaction> {
2323

2424
// We will not use these ProgressTracker for this Hello-World sample
2525
private final ProgressTracker progressTracker = new ProgressTracker();
26+
2627
@Override
2728
public ProgressTracker getProgressTracker() {
2829
return progressTracker;
2930
}
3031

3132
//private variables
32-
private Party sender ;
33+
private Party sender;
3334
private Party receiver;
3435

3536
//public constructor
36-
public TemplateInitiator(Party sendTo){
37+
public TemplateInitiator(Party sendTo) {
3738
this.receiver = sendTo;
3839
}
3940

@@ -50,14 +51,14 @@ public SignedTransaction call() throws FlowException {
5051
final Party notary = getServiceHub().getNetworkMapCache().getNotary(CordaX500Name.parse("O=Notary,L=London,C=GB"));
5152

5253
//Compose the State that carries the Hello World message
53-
final TemplateState output = new TemplateState(msg,sender,receiver);
54+
final TemplateState output = new TemplateState(msg, sender, receiver);
5455

5556
// Step 3. Create a new TransactionBuilder object.
5657
final TransactionBuilder builder = new TransactionBuilder(notary);
5758

5859
// Step 4. Add the iou as an output state, as well as a command to the transaction builder.
5960
builder.addOutputState(output);
60-
builder.addCommand(new TemplateContract.Commands.Send(), Arrays.asList(this.sender.getOwningKey(),this.receiver.getOwningKey()) );
61+
builder.addCommand(new TemplateContract.Commands.Send(), Arrays.asList(this.sender.getOwningKey(), this.receiver.getOwningKey()));
6162

6263

6364
// Step 5. Verify and sign it with our KeyPair.
@@ -66,7 +67,7 @@ public SignedTransaction call() throws FlowException {
6667

6768

6869
// Step 6. Collect the other party's signature using the SignTransactionFlow.
69-
List<Party> otherParties = output.getParticipants().stream().map(el -> (Party)el).collect(Collectors.toList());
70+
List<Party> otherParties = output.getParticipants().stream().map(el -> (Party) el).collect(Collectors.toList());
7071
otherParties.remove(getOurIdentity());
7172
List<FlowSession> sessions = otherParties.stream().map(el -> initiateFlow(el)).collect(Collectors.toList());
7273

0 commit comments

Comments
 (0)