diff --git a/src/main/java/com/gs/collections/kata/Company.java b/src/main/java/com/gs/collections/kata/Company.java index f0ce862..a5e51eb 100644 --- a/src/main/java/com/gs/collections/kata/Company.java +++ b/src/main/java/com/gs/collections/kata/Company.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public MutableList getCustomers() public MutableList getOrders() { - Assert.fail("Refactor this code to use GS Collections as part of Exercise 4"); + Assert.fail("Refactor this code to use GS Collections as part of Exercise 3"); MutableList orders = FastList.newList(); for (Customer customer : this.customers) { @@ -89,7 +89,7 @@ public Customer getCustomerNamed(String name) /** * Use a {@link Predicate} to find a {@link Customer} with the name given. */ - Assert.fail("Implement this method as part of Exercise 3"); + Assert.fail("Implement this method as part of Exercise 2"); return null; } } diff --git a/src/main/java/com/gs/collections/kata/Customer.java b/src/main/java/com/gs/collections/kata/Customer.java index 6bd339f..94ca35f 100644 --- a/src/main/java/com/gs/collections/kata/Customer.java +++ b/src/main/java/com/gs/collections/kata/Customer.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,32 +30,19 @@ */ public class Customer { - public static final Function TO_NAME = new Function() - { - @Override - public String valueOf(Customer customer) - { - Assert.fail("Replace with the implementation of the Function."); - return null; - } + public static final Function TO_NAME = customer -> { + Assert.fail("Replace with the implementation of the Function."); + return null; }; public static final Function TO_CITY = null; - public static final Function TO_TOTAL_ORDER_VALUE = - new Function() - { - @Override - public Double valueOf(Customer customer) - { - return customer.getTotalOrderValue(); - } - }; + public static final Function TO_TOTAL_ORDER_VALUE = Customer::getTotalOrderValue; private final String name; private final String city; - private final List orders = new ArrayList(); + private final List orders = new ArrayList<>(); public Customer(String name, String city) { @@ -85,14 +72,7 @@ public void addOrder(Order anOrder) public double getTotalOrderValue() { - MutableList orderValues = ListIterate.collect(this.orders, new Function() - { - @Override - public Double valueOf(Order order) - { - return order.getValue(); - } - }); + MutableList orderValues = ListIterate.collect(this.orders, Order::getValue); return orderValues.injectInto(0.0, AddFunction.DOUBLE_TO_DOUBLE); } } diff --git a/src/main/java/com/gs/collections/kata/LineItem.java b/src/main/java/com/gs/collections/kata/LineItem.java index 34bf26d..68d3582 100644 --- a/src/main/java/com/gs/collections/kata/LineItem.java +++ b/src/main/java/com/gs/collections/kata/LineItem.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,14 +23,7 @@ */ public class LineItem { - public static final Function TO_NAME = new Function() - { - @Override - public String valueOf(LineItem lineItem) - { - return lineItem.name; - } - }; + public static final Function TO_NAME = LineItem::getName; private String name; private final double value; diff --git a/src/main/java/com/gs/collections/kata/Order.java b/src/main/java/com/gs/collections/kata/Order.java index b8325dd..2b4b1d0 100644 --- a/src/main/java/com/gs/collections/kata/Order.java +++ b/src/main/java/com/gs/collections/kata/Order.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,50 +21,24 @@ import java.util.List; import com.gs.collections.api.block.function.Function; -import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.impl.block.function.AddFunction; import com.gs.collections.impl.collection.mutable.CollectionAdapter; import com.gs.collections.impl.utility.Iterate; /** * Has a number, a {@link Customer}, a {@link List} of {@link LineItem}s, and a boolean that states whether or not the order - * has been delivered. There is a class variable that contains the next order number. + * has been delivered. There is a class variable that contains the next order number. */ public class Order { - public static final Function TO_VALUE = - new Function() - { - @Override - public Double valueOf(Order order) - { - return order.getValue(); - } - }; - - public static final Predicate IS_DELIVERED = new Predicate() - { - @Override - public boolean accept(Order order) - { - return order.isDelivered; - } - }; - - public static final Function> TO_LINE_ITEMS = - new Function>() - { - @Override - public Iterable valueOf(Order order) - { - return order.lineItems; - } - }; + public static final Function TO_VALUE = Order::getValue; + + public static final Function> TO_LINE_ITEMS = Order::getLineItems; private static int nextOrderNumber = 1; private final int orderNumber; - private final List lineItems = new ArrayList(); + private final List lineItems = new ArrayList<>(); private boolean isDelivered; public Order() @@ -106,14 +80,7 @@ public String toString() public double getValue() { - Collection itemValues = Iterate.collect(this.lineItems, new Function() - { - @Override - public Double valueOf(LineItem lineItem) - { - return lineItem.getValue(); - } - }); + Collection itemValues = Iterate.collect(this.lineItems, LineItem::getValue); return CollectionAdapter.adapt(itemValues).injectInto(0.0, AddFunction.DOUBLE_TO_DOUBLE); } diff --git a/src/main/java/com/gs/collections/kata/Supplier.java b/src/main/java/com/gs/collections/kata/Supplier.java index ab35332..ec9e7c9 100644 --- a/src/main/java/com/gs/collections/kata/Supplier.java +++ b/src/main/java/com/gs/collections/kata/Supplier.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,25 +23,9 @@ */ public class Supplier { - public static final Function TO_NAME = - new Function() - { - @Override - public String valueOf(Supplier supplier) - { - return supplier.name; - } - }; + public static final Function TO_NAME = Supplier::getName; - public static final Function TO_NUMBER_OF_ITEMS = - new Function() - { - @Override - public Integer valueOf(Supplier supplier) - { - return supplier.itemNames.length; - } - }; + public static final Function TO_NUMBER_OF_ITEMS = supplier -> supplier.getItemNames().length; private final String name; private final String[] itemNames; diff --git a/src/test/java/com/gs/collections/kata/CompanyDomainForKata.java b/src/test/java/com/gs/collections/kata/CompanyDomainForKata.java index 6c9bde4..ac20b5e 100644 --- a/src/test/java/com/gs/collections/kata/CompanyDomainForKata.java +++ b/src/test/java/com/gs/collections/kata/CompanyDomainForKata.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,14 +47,14 @@ private void setUpCustomersAndOrders() fredOrder.addLineItem(new LineItem("shed", 50.0)); /** - * TODO 8: Refactor Order and its API so this repetition is not necessary. + * TODO 7: Refactor Order and its API so this repetition is not necessary. */ - // TODO 8: Add 3 cups at 1.5 each to the order + // TODO 7: Add 3 cups at 1.5 each to the order fredOrder.addLineItem(new LineItem("cup", 1.5)); fredOrder.addLineItem(new LineItem("cup", 1.5)); fredOrder.addLineItem(new LineItem("cup", 1.5)); - // TODO 8: Add 3 saucers at 1.0 each to the order + // TODO 7: Add 3 saucers at 1.0 each to the order fredOrder.addLineItem(new LineItem("saucer", 1.0)); fredOrder.addLineItem(new LineItem("saucer", 1.0)); fredOrder.addLineItem(new LineItem("saucer", 1.0)); @@ -70,13 +70,13 @@ private void setUpCustomersAndOrders() maryOrder.addLineItem(new LineItem("cat", 150.0)); maryOrder.addLineItem(new LineItem("big shed", 500.0)); - // TODO 8: Add 4 cups at 1.50 each to the order + // TODO 7: Add 4 cups at 1.50 each to the order maryOrder.addLineItem(new LineItem("cup", 1.5)); maryOrder.addLineItem(new LineItem("cup", 1.5)); maryOrder.addLineItem(new LineItem("cup", 1.5)); maryOrder.addLineItem(new LineItem("cup", 1.5)); - // TODO 8: Add 4 saucers at 1.50 each to the order + // TODO 7: Add 4 saucers at 1.50 each to the order maryOrder.addLineItem(new LineItem("saucer", 1.5)); maryOrder.addLineItem(new LineItem("saucer", 1.5)); maryOrder.addLineItem(new LineItem("saucer", 1.5)); @@ -92,7 +92,7 @@ private void setUpCustomersAndOrders() Order billOrder1 = new Order(); billOrder1.addLineItem(new LineItem("shed", 50.0)); - // TODO 8: Add 43 gnomes at 7.50 each to the order + // TODO 7: Add 43 gnomes at 7.50 each to the order for (int i = 0; i < 43; i++) { billOrder1.addLineItem(new LineItem("gnome", 7.50)); diff --git a/src/test/java/com/gs/collections/kata/Exercise1Test.java b/src/test/java/com/gs/collections/kata/Exercise1Test.java index a111581..6bcdc14 100644 --- a/src/test/java/com/gs/collections/kata/Exercise1Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise1Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,14 +28,7 @@ public class Exercise1Test extends CompanyDomainForKata @Test public void getCustomerNames() { - Function nameFunction = new Function() - { - @Override - public String valueOf(Customer customer) - { - return customer.getName(); - } - }; + Function nameFunction = Customer::getName; /** * Get the name of each of the company's customers. @@ -51,9 +44,7 @@ public String valueOf(Customer customer) public void getCustomerCities() { /** - * Get the city for each of the company's customers. Use an anonymous inner class. Use the IDE to help you as - * much as possible. Ctrl+space will help you implement an anonymous inner class. Implementing an interface is - * ctrl+i in IntelliJ. Eclipse's ctrl+1 is auto-fix and works to implement interfaces. + * Get the city for each of the company's customers. */ MutableList customers = this.company.getCustomers(); MutableList customerCities = null; @@ -66,7 +57,7 @@ public void getCustomerCities() public void getLondonCustomers() { /** - * Which customers come from London? Get a collection of those which do. Use an anonymous inner class. + * Which customers come from London? Get a collection of those which do. */ MutableList customers = this.company.getCustomers(); MutableList customersFromLondon = null; diff --git a/src/test/java/com/gs/collections/kata/Exercise2Test.java b/src/test/java/com/gs/collections/kata/Exercise2Test.java index ce7946a..26942de 100644 --- a/src/test/java/com/gs/collections/kata/Exercise2Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise2Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,44 +16,123 @@ package com.gs.collections.kata; +import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.api.list.MutableList; -import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; public class Exercise2Test extends CompanyDomainForKata { + /** + * Set up a {@link Predicate} that tests to see if a {@link Customer}'s city is "London" + */ + private static final Predicate CUSTOMER_FROM_LONDON = null; + @Test - public void getCustomerNames() + public void customerFromLondonPredicate() { - /** - * Get the name of each of the company's customers. This time move the {@link Function} to a - * constant on {@link Customer}. - */ - MutableList customerNames = this.company.getCustomers().collect(Customer.TO_NAME); + String predicateClass = CUSTOMER_FROM_LONDON.getClass().getSimpleName(); + Assert.assertTrue( + "Solution should use Predicates.attributeEquals() or a lambda but used " + predicateClass, + "AttributePredicate".equals(predicateClass) || predicateClass.startsWith("Exercise2Test$$Lambda")); + + Customer customerFromLondon = new Customer("test customer", "London"); - MutableList expectedNames = FastList.newListWith("Fred", "Mary", "Bill"); - Assert.assertEquals(expectedNames, customerNames); + Assert.assertEquals( + "Implement Customer.TO_CITY", + "London", + Customer.TO_CITY.valueOf(customerFromLondon)); + + Assert.assertTrue( + "CUSTOMER_FROM_LONDON should accept Customers where city is London", + CUSTOMER_FROM_LONDON.accept(customerFromLondon)); + } + + /** + * Do any customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + */ + @Test + public void doAnyCustomersLiveInLondon() + { + boolean anyCustomersFromLondon = false; + Assert.assertTrue(anyCustomersFromLondon); } + /** + * Do all customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + */ @Test - public void getCustomerCities() + public void doAllCustomersLiveInLondon() { - /** - * Get the city for each of the company's customers. This time move the {@link Function} to a - * constant on {@link Customer}. - */ - MutableList customerCities = null; + boolean allCustomersFromLondon = true; + Assert.assertFalse(allCustomersFromLondon); + } - MutableList expectedCities = FastList.newListWith("London", "Liphook", "London"); - Assert.assertEquals(expectedCities, customerCities); + /** + * How many customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + */ + @Test + public void howManyCustomersLiveInLondon() + { + int numberOfCustomerFromLondon = 0; + Assert.assertEquals("Should be 2 London customers", 2, numberOfCustomerFromLondon); } + /** + * Which customers come from London? Get a collection of those which do. Use the Predicate {@link + * #CUSTOMER_FROM_LONDON}. + */ @Test public void getLondonCustomers() { MutableList customersFromLondon = null; Verify.assertSize("Should be 2 London customers", 2, customersFromLondon); } + + /** + * Which customers do not come from London? Get a collection of those which don't. Use the Predicate {@link + * #CUSTOMER_FROM_LONDON}. + */ + @Test + public void getCustomersWhoDontLiveInLondon() + { + MutableList customersNotFromLondon = null; + Verify.assertSize("customers not from London", 1, customersNotFromLondon); + } + + /** + * Which customers come from London? Which customers do not come from London? Get a collection of each in a single pass. + * Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + */ + @Test + public void getCustomersWhoDoAndDoNotLiveInLondon() + { + MutableList customersFromLondon = null; + MutableList customersNotFromLondon = null; + Verify.assertSize("Should be 2 London customers", 2, customersFromLondon); + Verify.assertSize("customers not from London", 1, customersNotFromLondon); + } + + /** + * Implement {@link Company#getCustomerNamed(String)} and get this test to pass. + */ + @Test + public void findMary() + { + Customer mary = this.company.getCustomerNamed("Mary"); + Assert.assertEquals("customer's name should be Mary", "Mary", mary.getName()); + } + + /** + * Implement {@link Company#getCustomerNamed(String)} and get this test to pass. + */ + @Test + public void findPete() + { + Customer pete = this.company.getCustomerNamed("Pete"); + Assert.assertNull( + "Should be null as there is no customer called Pete", + pete); + } } diff --git a/src/test/java/com/gs/collections/kata/Exercise3Test.java b/src/test/java/com/gs/collections/kata/Exercise3Test.java index 05547cf..9648e38 100644 --- a/src/test/java/com/gs/collections/kata/Exercise3Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise3Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,10 @@ package com.gs.collections.kata; -import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.api.list.MutableList; +import com.gs.collections.api.set.MutableSet; +import com.gs.collections.impl.list.mutable.FastList; +import com.gs.collections.impl.set.mutable.UnifiedSet; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; @@ -25,81 +27,40 @@ public class Exercise3Test extends CompanyDomainForKata { /** - * Set up a {@link Predicate} that tests to see if a {@link Customer}'s city is "London" - */ - private static final Predicate CUSTOMER_FROM_LONDON = null; - - /** - * Do any customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + * Improve {@link Company#getOrders()} without breaking this test. */ @Test - public void doAnyCustomersLiveInLondon() + public void improveGetOrders() { - boolean anyCustomersFromLondon = false; - Assert.assertTrue(anyCustomersFromLondon); + // Delete this line - it's a reminder + Assert.fail("Improve getOrders() without breaking this test"); + Verify.assertSize(5, this.company.getOrders()); } /** - * Do all customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. + * Get all items that have been ordered by anybody. */ @Test - public void doAllCustomersLiveInLondon() + public void findItemNames() { - boolean allCustomersFromLondon = true; - Assert.assertFalse(allCustomersFromLondon); - } + MutableList allOrderedLineItems = null; + MutableSet actualItemNames = null; - /** - * How many customers come from London? Use the Predicate {@link #CUSTOMER_FROM_LONDON}. - */ - @Test - public void howManyCustomersLiveInLondon() - { - int numberOfCustomerFromLondon = 0; - Assert.assertEquals("Should be 2 London customers", 2, numberOfCustomerFromLondon); - } - - /** - * Which customers come from London? Get a collection of those which do. Use the Predicate {@link - * #CUSTOMER_FROM_LONDON}. - */ - @Test - public void getLondonCustomers() - { - MutableList customersFromLondon = null; - Verify.assertSize("Should be 2 London customers", 2, customersFromLondon); - } + Verify.assertInstanceOf(MutableSet.class, actualItemNames); + Verify.assertInstanceOf(String.class, actualItemNames.getFirst()); - /** - * Which customers do not come from London? Get a collection of those which don't. Use the Predicate {@link - * #CUSTOMER_FROM_LONDON}. - */ - @Test - public void getCustomersWhoDontLiveInLondon() - { - MutableList customersNotFromLondon = null; - Verify.assertSize("customers not from London", 1, customersNotFromLondon); + MutableSet expectedItemNames = UnifiedSet.newSetWith( + "shed", "big shed", "bowl", "cat", "cup", "chair", "dog", + "goldfish", "gnome", "saucer", "sofa", "table"); + Assert.assertEquals(expectedItemNames, actualItemNames); } - /** - * Implement {@link Company#getCustomerNamed(String)} and get this test to pass. - */ @Test - public void findMary() + public void findCustomerNames() { - Customer mary = this.company.getCustomerNamed("Mary"); - Assert.assertEquals("customer's name should be Mary", "Mary", mary.getName()); - } + MutableList names = null; - /** - * Implement {@link Company#getCustomerNamed(String)} and get this test to pass. - */ - @Test - public void findPete() - { - Customer pete = this.company.getCustomerNamed("Pete"); - Assert.assertNull( - "Should be null as there is no customer called Pete", - pete); + MutableList expectedNames = FastList.newListWith("Fred", "Mary", "Bill"); + Assert.assertEquals(expectedNames, names); } } diff --git a/src/test/java/com/gs/collections/kata/Exercise4Test.java b/src/test/java/com/gs/collections/kata/Exercise4Test.java index 4c1d9d0..14af2d8 100644 --- a/src/test/java/com/gs/collections/kata/Exercise4Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise4Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,51 +16,85 @@ package com.gs.collections.kata; +import java.util.List; + +import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.api.list.MutableList; -import com.gs.collections.api.set.MutableSet; import com.gs.collections.impl.list.mutable.FastList; -import com.gs.collections.impl.set.mutable.UnifiedSet; -import com.gs.collections.impl.test.Verify; +import com.gs.collections.impl.utility.ArrayIterate; +import com.gs.collections.impl.utility.Iterate; import org.junit.Assert; import org.junit.Test; public class Exercise4Test extends CompanyDomainForKata { /** - * Improve {@link Company#getOrders()} without breaking this test. + * Solve this without changing the return type of {@link Company#getSuppliers()}. Find the appropriate method on + * {@link ArrayIterate}. */ @Test - public void improveGetOrders() + public void findSupplierNames() { - // Delete this line - it's a reminder - Assert.fail("Improve getOrders() without breaking this test"); - Verify.assertSize(5, this.company.getOrders()); + MutableList supplierNames = null; + + MutableList expectedSupplierNames = FastList.newListWith( + "Shedtastic", + "Splendid Crocks", + "Annoying Pets", + "Gnomes 'R' Us", + "Furniture Hamlet", + "SFD", + "Doxins"); + Assert.assertEquals(expectedSupplierNames, supplierNames); } /** - * Get all items that have been ordered by anybody. + * Create a {@link Predicate} for Suppliers that supply more than 2 items. Find the number of suppliers that + * satisfy that Predicate. */ @Test - public void findItemNames() + public void countSuppliersWithMoreThanTwoItems() { - MutableList allOrderedLineItems = null; - MutableSet actualItemNames = null; + Predicate moreThanTwoItems = null; + int suppliersWithMoreThanTwoItems = 0; + Assert.assertEquals("suppliers with more than 2 items", 5, suppliersWithMoreThanTwoItems); + } - Verify.assertInstanceOf(MutableSet.class, actualItemNames); - Verify.assertInstanceOf(String.class, actualItemNames.getFirst()); + /** + * Try to solve this without changing the return type of {@link Supplier#getItemNames()}. + */ + @Test + public void whoSuppliesSandwichToaster() + { + // Create a Predicate that will check to see if a Supplier supplies a "sandwich toaster". + Predicate suppliesToaster = null; - MutableSet expectedItemNames = UnifiedSet.newSetWith( - "shed", "big shed", "bowl", "cat", "cup", "chair", "dog", - "goldfish", "gnome", "saucer", "sofa", "table"); - Assert.assertEquals(expectedItemNames, actualItemNames); + // Find one supplier that supplies toasters. + Supplier toasterSupplier = null; + Assert.assertNotNull("toaster supplier", toasterSupplier); + Assert.assertEquals("Doxins", toasterSupplier.getName()); } @Test - public void findCustomerNames() + public void filterOrderValues() { - MutableList names = null; + List orders = this.company.getMostRecentCustomer().getOrders(); + /** + * Get the order values that are greater than 1.5. + */ + MutableList orderValues = null; + MutableList filtered = null; + Assert.assertEquals(FastList.newListWith(372.5, 1.75), filtered); + } - MutableList expectedNames = FastList.newListWith("Fred", "Mary", "Bill"); - Assert.assertEquals(expectedNames, names); + @Test + public void filterOrders() + { + List orders = this.company.getMostRecentCustomer().getOrders(); + /** + * Get the actual orders (not their double values) where those orders have a value greater than 2.0. + */ + MutableList filtered = null; + Assert.assertEquals(FastList.newListWith(Iterate.getFirst(this.company.getMostRecentCustomer().getOrders())), filtered); } } diff --git a/src/test/java/com/gs/collections/kata/Exercise5Test.java b/src/test/java/com/gs/collections/kata/Exercise5Test.java index 51b9122..0a0212b 100644 --- a/src/test/java/com/gs/collections/kata/Exercise5Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise5Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,73 +18,31 @@ import java.util.List; -import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.api.list.MutableList; +import com.gs.collections.impl.block.factory.Predicates; import com.gs.collections.impl.list.mutable.FastList; -import com.gs.collections.impl.utility.ArrayIterate; +import com.gs.collections.impl.test.Verify; import com.gs.collections.impl.utility.Iterate; import org.junit.Assert; import org.junit.Test; public class Exercise5Test extends CompanyDomainForKata { - /** - * Solve this without changing the return type of {@link Company#getSuppliers()}. Find the appropriate method on - * {@link ArrayIterate}. - */ - @Test - public void findSupplierNames() - { - MutableList supplierNames = null; - - MutableList expectedSupplierNames = FastList.newListWith( - "Shedtastic", - "Splendid Crocks", - "Annoying Pets", - "Gnomes 'R' Us", - "Furniture Hamlet", - "SFD", - "Doxins"); - Assert.assertEquals(expectedSupplierNames, supplierNames); - } - - /** - * Create a {@link Predicate} for Suppliers that supply more than 2 items. Find the number of suppliers that - * satisfy that Predicate. - */ - @Test - public void countSuppliersWithMoreThanTwoItems() - { - Predicate moreThanTwoItems = null; - int suppliersWithMoreThanTwoItems = 0; - Assert.assertEquals("suppliers with more than 2 items", 5, suppliersWithMoreThanTwoItems); - } - - /** - * Try to solve this without changing the return type of {@link Supplier#getItemNames()}. - */ - @Test - public void whoSuppliesSandwichToaster() - { - // Create a Predicate that will check to see if a Supplier supplies a "sandwich toaster". - Predicate suppliesToaster = null; - - // Find one supplier that supplies toasters. - Supplier toasterSupplier = null; - Assert.assertNotNull("toaster supplier", toasterSupplier); - Assert.assertEquals("Doxins", toasterSupplier.getName()); - } - @Test public void filterOrderValues() { List orders = this.company.getMostRecentCustomer().getOrders(); /** + * Same exercise but don't use static utility - refactor the type of orders and {@link Customer#getOrders()} + * instead. * Get the order values that are greater than 1.5. */ MutableList orderValues = null; - MutableList filtered = null; + MutableList filtered = orderValues.select(Predicates.greaterThan(1.5)); Assert.assertEquals(FastList.newListWith(372.5, 1.75), filtered); + Verify.assertInstanceOf(MutableList.class, this.company.getMostRecentCustomer().getOrders()); + this.company.getMostRecentCustomer().getOrders().add(null); + Verify.assertContains("Don't return a copy from Customer.getOrders(). The field should be a MutableList.", null, this.company.getMostRecentCustomer().getOrders()); } @Test @@ -92,9 +50,14 @@ public void filterOrders() { List orders = this.company.getMostRecentCustomer().getOrders(); /** + * Same exercise but don't use static utility - refactor the type of orders and {@link Customer#getOrders()} + * instead. * Get the actual orders (not their double values) where those orders have a value greater than 2.0. */ MutableList filtered = null; Assert.assertEquals(FastList.newListWith(Iterate.getFirst(this.company.getMostRecentCustomer().getOrders())), filtered); + Verify.assertInstanceOf(MutableList.class, this.company.getMostRecentCustomer().getOrders()); + this.company.getMostRecentCustomer().getOrders().add(null); + Verify.assertContains("Don't return a copy from Customer.getOrders(). The field should be a MutableList.", null, this.company.getMostRecentCustomer().getOrders()); } } diff --git a/src/test/java/com/gs/collections/kata/Exercise6Test.java b/src/test/java/com/gs/collections/kata/Exercise6Test.java index 1e60e3c..b482e71 100644 --- a/src/test/java/com/gs/collections/kata/Exercise6Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise6Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,48 +16,75 @@ package com.gs.collections.kata; -import java.util.List; - +import com.gs.collections.api.RichIterable; +import com.gs.collections.api.block.procedure.Procedure; import com.gs.collections.api.list.MutableList; -import com.gs.collections.impl.block.factory.Predicates; -import com.gs.collections.impl.list.mutable.FastList; +import com.gs.collections.impl.block.factory.Procedures; import com.gs.collections.impl.test.Verify; -import com.gs.collections.impl.utility.Iterate; import org.junit.Assert; import org.junit.Test; public class Exercise6Test extends CompanyDomainForKata { + /** + * Get a list of the customers' total order values, sorted. Check out the implementation of {@link + * Customer#getTotalOrderValue()} and {@link Order#getValue()} . + */ + @Test + public void sortedTotalOrderValue() + { + MutableList sortedTotalValues = null; + + // Don't forget the handy utility methods getFirst() and getLast()... + Assert.assertEquals("Highest total order value", Double.valueOf(857.0), sortedTotalValues.getLast()); + Assert.assertEquals("Lowest total order value", Double.valueOf(71.0), sortedTotalValues.getFirst()); + } + + /** + * Find the max total order value across all customers. + */ + @Test + public void maximumTotalOrderValue() + { + Double maximumTotalOrderValue = null; + Assert.assertEquals("max value", Double.valueOf(857.0), maximumTotalOrderValue); + } + + /** + * Find the customer with the highest total order value. + */ + @Test + public void customerWithMaxTotalOrderValue() + { + Customer customerWithMaxTotalOrderValue = null; + Assert.assertEquals(this.company.getCustomerNamed("Mary"), customerWithMaxTotalOrderValue); + } + + /** + * Create some code to get the company's supplier names as a tilde delimited string. + */ @Test - public void filterOrderValues() + public void supplierNamesAsTildeDelimitedString() { - List orders = this.company.getMostRecentCustomer().getOrders(); - /** - * Same exercise but don't use static utility - refactor the type of orders and {@link Customer#getOrders()} - * instead. - * Get the order values that are greater than 1.5. - */ - MutableList orderValues = null; - MutableList filtered = orderValues.select(Predicates.greaterThan(1.5)); - Assert.assertEquals(FastList.newListWith(372.5, 1.75), filtered); - Verify.assertInstanceOf(MutableList.class, this.company.getMostRecentCustomer().getOrders()); - this.company.getMostRecentCustomer().getOrders().add(null); - Verify.assertContains("Don't return a copy from Customer.getOrders(). The field should be a MutableList.", null, this.company.getMostRecentCustomer().getOrders()); + String tildeSeparatedNames = null; + Assert.assertEquals( + "tilde separated names", + "Shedtastic~Splendid Crocks~Annoying Pets~Gnomes 'R' Us~Furniture Hamlet~SFD~Doxins", + tildeSeparatedNames); } + /** + * Deliver all orders going to customers from London. + *

+ * Hint: Use {@link RichIterable#forEach(Procedure)}. To solve the ambiguity error, use {@link Procedures#cast(Procedure)}}. + * + * @see Order#deliver() + */ @Test - public void filterOrders() + public void deliverOrdersToLondon() { - List orders = this.company.getMostRecentCustomer().getOrders(); - /** - * Same exercise but don't use static utility - refactor the type of orders and {@link Customer#getOrders()} - * instead. - * Get the actual orders (not their double values) where those orders have a value greater than 2.0. - */ - MutableList filtered = null; - Assert.assertEquals(FastList.newListWith(Iterate.getFirst(this.company.getMostRecentCustomer().getOrders())), filtered); - Verify.assertInstanceOf(MutableList.class, this.company.getMostRecentCustomer().getOrders()); - this.company.getMostRecentCustomer().getOrders().add(null); - Verify.assertContains("Don't return a copy from Customer.getOrders(). The field should be a MutableList.", null, this.company.getMostRecentCustomer().getOrders()); + Verify.assertAllSatisfy(this.company.getCustomerNamed("Fred").getOrders(), Order::isDelivered); + Verify.assertNoneSatisfy(this.company.getCustomerNamed("Mary").getOrders(), Order::isDelivered); + Verify.assertAllSatisfy(this.company.getCustomerNamed("Bill").getOrders(), Order::isDelivered); } } diff --git a/src/test/java/com/gs/collections/kata/Exercise7Test.java b/src/test/java/com/gs/collections/kata/Exercise7Test.java index 8c9d8a0..92ca2d5 100644 --- a/src/test/java/com/gs/collections/kata/Exercise7Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise7Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,12 @@ package com.gs.collections.kata; -import com.gs.collections.api.list.MutableList; -import com.gs.collections.impl.block.factory.Predicates; +import java.util.List; + +import com.gs.collections.api.map.MutableMap; +import com.gs.collections.api.multimap.list.MutableListMultimap; +import com.gs.collections.impl.list.mutable.FastList; +import com.gs.collections.impl.map.mutable.UnifiedMap; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; @@ -25,62 +29,56 @@ public class Exercise7Test extends CompanyDomainForKata { /** - * Get a list of the customers' total order values, sorted. Check out the implementation of {@link - * Customer#getTotalOrderValue()} and {@link Order#getValue()} . + * Create a multimap where the keys are the names of cities and the values are the customers from those cities. */ @Test - public void sortedTotalOrderValue() + public void customersByCity() { - MutableList sortedTotalValues = null; + // Notice that the second generic type is Customer, not List + MutableListMultimap multimap = null; - // Don't forget the handy utility methods getFirst() and getLast()... - Assert.assertEquals("Highest total order value", Double.valueOf(857.0), sortedTotalValues.getLast()); - Assert.assertEquals("Lowest total order value", Double.valueOf(71.0), sortedTotalValues.getFirst()); + Assert.assertEquals(FastList.newListWith(this.company.getCustomerNamed("Mary")), multimap.get("Liphook")); + Assert.assertEquals( + FastList.newListWith( + this.company.getCustomerNamed("Fred"), + this.company.getCustomerNamed("Bill")), + multimap.get("London")); } - /** - * Find the max total order value across all customers. - */ @Test - public void maximumTotalOrderValue() + public void mapOfItemsToSuppliers() { - Double maximumTotalOrderValue = null; - Assert.assertEquals("max value", Double.valueOf(857.0), maximumTotalOrderValue); - } + Assert.fail("Refactor this as part of Exercise 7"); + /** + * Change itemsToSuppliers to a MutableMultimap + */ + final MutableMap> itemsToSuppliers = UnifiedMap.newMap(); - /** - * Find the customer with the highest total order value. - */ - @Test - public void customerWithMaxTotalOrderValue() - { - Customer customerWithMaxTotalOrderValue = null; - Assert.assertEquals(this.company.getCustomerNamed("Mary"), customerWithMaxTotalOrderValue); - } + for (Supplier supplier : this.company.getSuppliers()) + { + for (String itemName : supplier.getItemNames()) + { + List suppliersForItem; + if (itemsToSuppliers.containsKey(itemName)) + { + suppliersForItem = itemsToSuppliers.get(itemName); + } + else + { + suppliersForItem = FastList.newList(); + itemsToSuppliers.put(itemName, suppliersForItem); + } - /** - * Create some code to get the company's supplier names as a tilde delimited string. - */ - @Test - public void supplierNamesAsTildeDelimitedString() - { - String tildeSeparatedNames = null; - Assert.assertEquals( - "tilde separated names", - "Shedtastic~Splendid Crocks~Annoying Pets~Gnomes 'R' Us~Furniture Hamlet~SFD~Doxins", - tildeSeparatedNames); + suppliersForItem.add(supplier); + } + } + Verify.assertIterableSize("should be 2 suppliers for sofa", 2, itemsToSuppliers.get("sofa")); } - /** - * Deliver all orders going to customers from London. - * - * @see Order#deliver() - */ @Test - public void deliverOrdersToLondon() + public void reminder() { - Verify.assertAllSatisfy(this.company.getCustomerNamed("Fred").getOrders(), Order.IS_DELIVERED); - Verify.assertAllSatisfy(this.company.getCustomerNamed("Mary").getOrders(), Predicates.not(Order.IS_DELIVERED)); - Verify.assertAllSatisfy(this.company.getCustomerNamed("Bill").getOrders(), Order.IS_DELIVERED); + Assert.fail("Refactor setUpCustomersAndOrders() in the super class to not have so much repetition."); + // Delete this whole method when you're done. It's just a reminder. } } diff --git a/src/test/java/com/gs/collections/kata/Exercise8Test.java b/src/test/java/com/gs/collections/kata/Exercise8Test.java index fe5325a..f82dfbb 100644 --- a/src/test/java/com/gs/collections/kata/Exercise8Test.java +++ b/src/test/java/com/gs/collections/kata/Exercise8Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Goldman Sachs. + * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,80 +16,110 @@ package com.gs.collections.kata; -import java.util.List; +import java.util.Collections; -import com.gs.collections.api.block.procedure.Procedure; +import com.gs.collections.api.RichIterable; +import com.gs.collections.api.bag.sorted.MutableSortedBag; +import com.gs.collections.api.block.function.Function; +import com.gs.collections.api.block.function.Function0; +import com.gs.collections.api.block.function.Function2; +import com.gs.collections.api.list.MutableList; import com.gs.collections.api.map.MutableMap; import com.gs.collections.api.multimap.list.MutableListMultimap; +import com.gs.collections.impl.bag.sorted.mutable.TreeBag; import com.gs.collections.impl.list.mutable.FastList; -import com.gs.collections.impl.map.mutable.UnifiedMap; import com.gs.collections.impl.test.Verify; -import com.gs.collections.impl.utility.ArrayIterate; import org.junit.Assert; import org.junit.Test; public class Exercise8Test extends CompanyDomainForKata { /** - * Create a multimap where the keys are the names of cities and the values are the customers from those cities. + * Extra credit. Aggregate the total order values by city. + * + * @see RichIterable#aggregateBy(Function, Function0, Function2) */ @Test - public void customersByCity() + public void totalOrderValuesByCity() { - // Notice that the second generic type is Customer, not List - MutableListMultimap multimap = null; + Function0 zeroValueFactory = () -> 0.0; + Function2 aggregator = (result, customer) -> result + customer.getTotalOrderValue(); - Assert.assertEquals(FastList.newListWith(this.company.getCustomerNamed("Mary")), multimap.get("Liphook")); - Assert.assertEquals( - FastList.newListWith( - this.company.getCustomerNamed("Fred"), - this.company.getCustomerNamed("Bill")), - multimap.get("London")); + MutableMap map = null; + Assert.assertEquals(2, map.size()); + Assert.assertEquals(446.25, map.get("London"), 0.0); + Assert.assertEquals(857.0, map.get("Liphook"), 0.0); } + /** + * Extra credit. Aggregate the total order values by item. + * Hint: Look at {@link RichIterable#aggregateBy(Function, Function0, Function2)} and remember + * how to use {@link RichIterable#flatCollect(Function)} to get an iterable of all items. + */ @Test - public void mapOfItemsToSuppliers() + public void totalOrderValuesByItem() { - /** - * Change itemsToSuppliers to a MutableMultimap - */ - final MutableMap> itemsToSuppliers = UnifiedMap.newMap(); + Function0 zeroValueFactory = () -> 0.0; + Function2 aggregator = (result, lineItem) -> result + lineItem.getValue(); + + MutableMap map = null; + Verify.assertSize(12, map); + Assert.assertEquals(100.0, map.get("shed"), 0.0); + Assert.assertEquals(10.5, map.get("cup"), 0.0); + } + + /** + * Extra credit. Find all customers' line item values greater than 7.5 and sort them by highest to lowest price. + */ + @Test + public void sortedOrders() + { + MutableSortedBag orderedPrices = null; + + MutableSortedBag expectedPrices = TreeBag.newBagWith( + Collections.reverseOrder(), 500.0, 150.0, 120.0, 75.0, 50.0, 50.0, 12.5); + Verify.assertSortedBagsEqual(expectedPrices, orderedPrices); + } - ArrayIterate.forEach(this.company.getSuppliers(), new Procedure() - { - @Override - public void value(final Supplier supplier) - { - ArrayIterate.forEach(supplier.getItemNames(), new Procedure() - { - @Override - public void value(String itemName) - { - Assert.fail("Refactor this as part of Exercise 6"); + /** + * Extra credit. Figure out which customers ordered saucers (in any of their orders). + */ + @Test + public void whoOrderedSaucers() + { + MutableList customersWithSaucers = null; + Verify.assertSize("customers with saucers", 2, customersWithSaucers); + } - List suppliersForItem; - if (itemsToSuppliers.containsKey(itemName)) - { - suppliersForItem = itemsToSuppliers.get(itemName); - } - else - { - suppliersForItem = FastList.newList(); - itemsToSuppliers.put(itemName, suppliersForItem); - } + /** + * Extra credit. Look into the {@link MutableList#toMap(Function, Function)} method. + */ + @Test + public void ordersByCustomerUsingAsMap() + { + MutableMap> customerNameToOrders = + this.company.getCustomers().toMap(null, null); - suppliersForItem.add(supplier); - } - }); - } - }); - Verify.assertIterableSize("should be 2 suppliers for sofa", 2, itemsToSuppliers.get("sofa")); + Assert.assertNotNull("customer name to orders", customerNameToOrders); + Verify.assertSize("customer names", 3, customerNameToOrders); + MutableList ordersForBill = customerNameToOrders.get("Bill"); + Verify.assertSize("Bill orders", 3, ordersForBill); } + /** + * Extra credit. Create a multimap where the values are customers and the key is the price of + * the most expensive item that the customer ordered. + */ @Test - public void reminder() + public void mostExpensiveItem() { - Assert.fail("Refactor setUpCustomersAndOrders() in the super class to not have so much repetition."); - // Delete this whole method when you're done. It's just a reminder. + MutableListMultimap multimap = null; + Assert.assertEquals(3, multimap.size()); + Assert.assertEquals(2, multimap.keysView().size()); + Assert.assertEquals( + FastList.newListWith( + this.company.getCustomerNamed("Fred"), + this.company.getCustomerNamed("Bill")), + multimap.get(50.0)); } } diff --git a/src/test/java/com/gs/collections/kata/Exercise9Test.java b/src/test/java/com/gs/collections/kata/Exercise9Test.java deleted file mode 100644 index ee7c77d..0000000 --- a/src/test/java/com/gs/collections/kata/Exercise9Test.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2011 Goldman Sachs. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.gs.collections.kata; - -import com.gs.collections.api.bag.sorted.MutableSortedBag; -import com.gs.collections.api.block.function.Function; -import com.gs.collections.api.block.function.Function0; -import com.gs.collections.api.block.function.Function2; -import com.gs.collections.api.list.MutableList; -import com.gs.collections.api.map.MutableMap; -import com.gs.collections.api.multimap.list.MutableListMultimap; -import com.gs.collections.impl.bag.sorted.mutable.TreeBag; -import com.gs.collections.impl.list.mutable.FastList; -import com.gs.collections.impl.test.Verify; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Collections; - -public class Exercise9Test extends CompanyDomainForKata -{ - /** - * Extra credit. Aggregate the total order values by city. Hint: Look at RichIterable.aggregateBy. - */ - @Test - public void totalOrderValuesByCity() - { - Function0 zeroValueFactory = new Function0() - { - public Double value() - { - return Double.valueOf(0.0); - } - }; - - Function2 aggregator = new Function2() - { - public Double value(Double result, Customer customer) - { - return result + customer.getTotalOrderValue(); - } - }; - MutableMap map = null; - Assert.assertEquals(2, map.size()); - Assert.assertEquals(446.25, map.get("London"), 0.0); - Assert.assertEquals(857.0, map.get("Liphook"), 0.0); - } - - /** - * Extra credit. Aggregate the total order values by item. Hint: Look at RichIterable.aggregateBy and remember - * how to use flatCollect to get an iterable of all items. - */ - @Test - public void totalOrderValuesByItem() - { - Function0 zeroValueFactory = new Function0() - { - public Double value() - { - return Double.valueOf(0.0); - } - }; - - Function2 aggregator = new Function2() - { - public Double value(Double result, LineItem lineItem) - { - return result + lineItem.getValue(); - } - }; - MutableMap map = null; - Verify.assertSize(12, map); - Assert.assertEquals(100.0, map.get("shed"), 0.0); - Assert.assertEquals(10.5, map.get("cup"), 0.0); - } - - /** - * Extra credit. Find all customers' line item values greater than 7.5 and sort them by highest to lowest price. - */ - @Test - public void sortedOrders() - { - MutableSortedBag orderedPrices = null; - - MutableSortedBag expectedPrices = TreeBag.newBagWith( - Collections.reverseOrder(), 500.0, 150.0, 120.0, 75.0, 50.0, 50.0, 12.5); - Verify.assertSortedBagsEqual(expectedPrices, orderedPrices); - } - - /** - * Extra credit. Figure out which customers ordered saucers (in any of their orders). - */ - @Test - public void whoOrderedSaucers() - { - MutableList customersWithSaucers = null; - Verify.assertSize("customers with saucers", 2, customersWithSaucers); - } - - /** - * Extra credit. Look into the {@link MutableList#toMap(Function, Function)} method. - */ - @Test - public void ordersByCustomerUsingAsMap() - { - MutableMap> customerNameToOrders = - this.company.getCustomers().toMap(null, null); - - Assert.assertNotNull("customer name to orders", customerNameToOrders); - Verify.assertSize("customer names", 3, customerNameToOrders); - MutableList ordersForBill = customerNameToOrders.get("Bill"); - Verify.assertSize("Bill orders", 3, ordersForBill); - } - - /** - * Extra credit. Create a multimap where the values are customers and the key is the price of - * the most expensive item that the customer ordered. - */ - @Test - public void mostExpensiveItem() - { - MutableListMultimap multimap = null; - Assert.assertEquals(3, multimap.size()); - Assert.assertEquals(2, multimap.keysView().size()); - Assert.assertEquals( - FastList.newListWith( - this.company.getCustomerNamed("Fred"), - this.company.getCustomerNamed("Bill")), - multimap.get(50.0)); - } -}