Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Adding automatic formatting of Java and Scala code
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorklang committed Sep 9, 2019
1 parent b38d835 commit beff2ff
Show file tree
Hide file tree
Showing 151 changed files with 5,501 additions and 4,372 deletions.
13 changes: 13 additions & 0 deletions .scalafmt.conf
@@ -0,0 +1,13 @@
version = 2.0.1

style = defaultWithAlign

align.tokens = [off]
align.openParenDefnSite = true
align.openParenCallSite = true
danglingParentheses = true
docstrings = JavaDoc
indentOperator = spray
maxColumn = 120
rewrite.rules = [RedundantBraces, RedundantParens, SortImports]
unindentTopLevelOperators = true
5 changes: 4 additions & 1 deletion build.sbt
Expand Up @@ -26,10 +26,13 @@ inThisBuild(Seq(
"scm:git@github.com:cloudstateio/cloudstate.git"
)),
developers := List(
Developer(id="jroper", name="James Roper", email="james@jazzy.id.au", url=url("https://jazzy.id.au"))
Developer(id="jroper", name="James Roper", email="james@jazzy.id.au", url=url("https://jazzy.id.au")),
Developer(id="viktorklang", name="Viktor Klang", email="viktor.klang@gmail.com", url=url("https://viktorklang.com")),
),

sonatypeProfileName := "io.cloudstate",

scalafmtOnCompile := true,
))

// Make sure the version doesn't change each time it gets built, this ensures we don't rebuild the native image
Expand Down
201 changes: 98 additions & 103 deletions docs/src/test/java/docs/user/eventsourced/ShoppingCartEntity.java
Expand Up @@ -11,114 +11,109 @@
import java.util.Map;
import java.util.stream.Collectors;


// #entity-class
@EventSourcedEntity(
persistenceId = "shopping-cart",
snapshotEvery = 20
)
@EventSourcedEntity(persistenceId = "shopping-cart", snapshotEvery = 20)
public class ShoppingCartEntity {
// #entity-class

// #entity-state
private final Map<String, Shoppingcart.LineItem> cart = new LinkedHashMap<>();
// #entity-state

// #constructing
private final String entityId;

public ShoppingCartEntity(@EntityId String entityId) {
this.entityId = entityId;
// #entity-class

// #entity-state
private final Map<String, Shoppingcart.LineItem> cart = new LinkedHashMap<>();
// #entity-state

// #constructing
private final String entityId;

public ShoppingCartEntity(@EntityId String entityId) {
this.entityId = entityId;
}
// #constructing

// #get-cart
@CommandHandler
public Shoppingcart.Cart getCart() {
return Shoppingcart.Cart.newBuilder().addAllItems(cart.values()).build();
}
// #get-cart

// #add-item
@CommandHandler
public Empty addItem(Shoppingcart.AddLineItem item, CommandContext ctx) {
if (item.getQuantity() <= 0) {
ctx.fail("Cannot add negative quantity of to item" + item.getProductId());
}
// #constructing

// #get-cart
@CommandHandler
public Shoppingcart.Cart getCart() {
return Shoppingcart.Cart.newBuilder()
.addAllItems(cart.values())
.build();
}
// #get-cart

// #add-item
@CommandHandler
public Empty addItem(Shoppingcart.AddLineItem item, CommandContext ctx) {
if (item.getQuantity() <= 0) {
ctx.fail("Cannot add negative quantity of to item" + item.getProductId());
}
ctx.emit(Domain.ItemAdded.newBuilder().setItem(
ctx.emit(
Domain.ItemAdded.newBuilder()
.setItem(
Domain.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build()
).build());
return Empty.getDefaultInstance();
}
// #add-item

// #item-added
@EventHandler
public void itemAdded(Domain.ItemAdded itemAdded) {
Shoppingcart.LineItem item = cart.get(itemAdded.getItem().getProductId());
if (item == null) {
item = convert(itemAdded.getItem());
} else {
item = item.toBuilder()
.setQuantity(item.getQuantity() + itemAdded.getItem().getQuantity())
.build();
}
cart.put(item.getProductId(), item);
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build())
.build());
return Empty.getDefaultInstance();
}
// #add-item

// #item-added
@EventHandler
public void itemAdded(Domain.ItemAdded itemAdded) {
Shoppingcart.LineItem item = cart.get(itemAdded.getItem().getProductId());
if (item == null) {
item = convert(itemAdded.getItem());
} else {
item =
item.toBuilder()
.setQuantity(item.getQuantity() + itemAdded.getItem().getQuantity())
.build();
}

private Shoppingcart.LineItem convert(Domain.LineItem item) {
return Shoppingcart.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build();
}
// #item-added

// #snapshot
@Snapshot
public Domain.Cart snapshot() {
return Domain.Cart.newBuilder()
.addAllItems(
cart.values().stream()
.map(this::convert)
.collect(Collectors.toList())
).build();
}

private Domain.LineItem convert(Shoppingcart.LineItem item) {
return Domain.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build();
}
// #snapshot

// #handle-snapshot
@SnapshotHandler
public void handleSnapshot(Domain.Cart cart) {
this.cart.clear();
for (Domain.LineItem item : cart.getItemsList()) {
this.cart.put(item.getProductId(), convert(item));
}
}
// #handle-snapshot

// #register
public static void main(String... args) {
new CloudState().registerEventSourcedEntity(
ShoppingCartEntity.class,
Shoppingcart.getDescriptor().findServiceByName("ShoppingCartService"),
Domain.getDescriptor()
).start();
cart.put(item.getProductId(), item);
}

private Shoppingcart.LineItem convert(Domain.LineItem item) {
return Shoppingcart.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build();
}
// #item-added

// #snapshot
@Snapshot
public Domain.Cart snapshot() {
return Domain.Cart.newBuilder()
.addAllItems(cart.values().stream().map(this::convert).collect(Collectors.toList()))
.build();
}

private Domain.LineItem convert(Shoppingcart.LineItem item) {
return Domain.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build();
}
// #snapshot

// #handle-snapshot
@SnapshotHandler
public void handleSnapshot(Domain.Cart cart) {
this.cart.clear();
for (Domain.LineItem item : cart.getItemsList()) {
this.cart.put(item.getProductId(), convert(item));
}
// #register
}
// #handle-snapshot

// #register
public static void main(String... args) {
new CloudState()
.registerEventSourcedEntity(
ShoppingCartEntity.class,
Shoppingcart.getDescriptor().findServiceByName("ShoppingCartService"),
Domain.getDescriptor())
.start();
}
// #register

}

0 comments on commit beff2ff

Please sign in to comment.