Skip to content

Commit

Permalink
refactored two copies of populateCustomers into one
Browse files Browse the repository at this point in the history
  • Loading branch information
eitansuez committed Sep 23, 2017
1 parent 37963f5 commit 131ded3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.pivotal.bookshop.buslogic;

import io.pivotal.bookshop.domain.Address;
import io.pivotal.bookshop.domain.Customer;
import lombok.extern.log4j.Log4j2;
import org.apache.geode.cache.Region;

import java.util.HashMap;
import java.util.Map;

@Log4j2
class CustomerLoader {

static void populateCustomerRegion(Region<Long, Customer> customerRegion) {
Map<Long, Customer> customerMap = new HashMap<>();
Customer cust1 = Customer.builder().customerNumber(5540)
.firstName("Lula").lastName("Wax")
.address(Address.builder()
.addressLine1("123 Main St")
.city("Topeka").state("KS").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5540L, cust1);

Customer cust2 = Customer.builder().customerNumber(5541)
.firstName("Tom").lastName("Mcginns")
.address(Address.builder()
.addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5541L, cust2);

Customer cust3 = Customer.builder().customerNumber(5542)
.firstName("Peter").lastName("Fernandez")
.address(Address.builder()
.addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5542L, cust3);

Customer cust4 = Customer.builder().customerNumber(5543)
.firstName("Jenny").lastName("Tsai")
.address(Address.builder()
.addressLine1("123 Main St")
.city("Topeka").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5543L, cust4);

customerRegion.putAll(customerMap);
log.info("************ Loaded customers data ****************");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.apache.geode.cache.Region;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static io.pivotal.bookshop.buslogic.CustomerLoader.populateCustomerRegion;

@Log4j2
public class SampleDataEviction {
Expand All @@ -23,61 +23,13 @@ public static void main(String[] args) throws InterruptedException {
Region<Long, Customer> customerRegion = cache.getRegion("Customer");

printRegionContents(customerRegion);
populateCustomer(customerRegion);
populateCustomerRegion(customerRegion);
insertOneMoreCustomer(customerRegion);
printRegionContents(customerRegion);
}

}

private static void populateCustomer(Region<Long, Customer> customerRegion) {
Map<Long, Customer> customer = new HashMap<>();
Customer cust1 = Customer.builder().customerNumber(5540)
.firstName("Lula").lastName("Wax")
.address(Address.builder()
.addressLine1("123 Main St")
.city("Topeka").state("KS").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME")
.build())
.bookOrder(17699L)
.build();
customer.put(5540L, cust1);

Customer cust2 = Customer.builder().customerNumber(5541)
.firstName("Tom").lastName("Mcginns")
.address(Address.builder().addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME")
.build())
.bookOrder(17699L)
.build();
customer.put(5541L, cust2);

Customer cust3 = Customer.builder().customerNumber(5542)
.firstName("Peter").lastName("Fernandez")
.address(Address.builder().addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME")
.build())
.bookOrder(17699L)
.build();

customer.put(5542L, cust3);

Customer cust4 = Customer.builder().customerNumber(5543)
.firstName("Jenny").lastName("Tsai")
.address(Address.builder().addressLine1("123 Main St")
.city("Topeka").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME")
.build())
.bookOrder(17699L)
.build();
customer.put(5543L, cust4);

customerRegion.putAll(customer);
log.info("************ Loaded customers data ****************");
}

private static void insertOneMoreCustomer(Region<Long, Customer> customerRegion) {
Customer cust5 = Customer.builder().customerNumber(5544)
.firstName("Fifth").lastName("Customer")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.pivotal.bookshop.buslogic;

import io.pivotal.bookshop.domain.Address;
import io.pivotal.bookshop.domain.Customer;
import lombok.extern.log4j.Log4j2;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.Region;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Log4j2
public class SampleDataExpiration {
Expand All @@ -22,7 +19,7 @@ public static void main(String[] args) throws InterruptedException {

Region<Long , Customer> customerRegion = cache.getRegion("Customer");

// TODO-06a: call populateCustomerRegion() to populate the customer region with sample data
// TODO-06a: call CustomerLoader.populateCustomerRegion() to populate the customer region with sample data

// TODO-06b: Show the contents by calling retrieveAndPrintRegionData for the customer region

Expand Down Expand Up @@ -61,51 +58,6 @@ private static void updateCustomer(Region<Long, Customer> customerRegion, long c
log.info("************ Updated customer data ****************\n");
}

private static void populateCustomerRegion(Region<Long, Customer> customerRegion) {
Map<Long, Customer> customerMap = new HashMap<>();
Customer cust1 = Customer.builder().customerNumber(5540)
.firstName("Lula").lastName("Wax")
.address(Address.builder()
.addressLine1("123 Main St")
.city("Topeka").state("KS").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5540L, cust1);

Customer cust2 = Customer.builder().customerNumber(5541)
.firstName("Tom").lastName("Mcginns")
.address(Address.builder()
.addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5541L, cust2);

Customer cust3 = Customer.builder().customerNumber(5542)
.firstName("Peter").lastName("Fernandez")
.address(Address.builder()
.addressLine1("123 Main St")
.city("San Francisco").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5542L, cust3);

Customer cust4 = Customer.builder().customerNumber(5543)
.firstName("Jenny").lastName("Tsai")
.address(Address.builder()
.addressLine1("123 Main St")
.city("Topeka").state("CA").postalCode("50505")
.country("US").phoneNumber("423 555-3322").addressTag("HOME").build())
.bookOrder(17699L)
.build();
customerMap.put(5543L, cust4);

customerRegion.putAll(customerMap);
log.info("************ Server 1 : Loaded customers data ****************\n");
}

/**
* TODO-05: Review this code.
Expand Down

0 comments on commit 131ded3

Please sign in to comment.