/*
 * 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.block.predicate.Predicate;
import com.gs.collections.api.block.procedure.ObjectIntProcedure;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.utility.ArrayIterate;
import org.junit.Assert;

/**
 * A company has a {@link MutableList} of {@link Customer}s.  It has an array of {@link Supplier}s, and a name.
 */
public class Company
{
    private final String name;
    private final MutableList<Customer> customers = FastList.newList();

    // suppliers are array based.
    private Supplier[] suppliers = new Supplier[0];

    public Company(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void addCustomer(Customer aCustomer)
    {
        this.customers.add(aCustomer);
    }

    public MutableList<Customer> getCustomers()
    {
        return this.customers;
    }

    public MutableList<Order> getOrders()
    {
        Assert.fail("Refactor this code to use GS Collections as part of Exercise 4");
        MutableList<Order> orders = FastList.newList();
        for (Customer customer : this.customers)
        {
            orders.addAll(customer.getOrders());
        }
        return orders;
    }

    public Customer getMostRecentCustomer()
    {
        return this.customers.getLast();
    }

    public void addSupplier(Supplier supplier)
    {
        // need to replace the current array of suppliers with another, larger array
        // Of course, normally one would not use an array.
        final Supplier[] currentSuppliers = this.suppliers;
        this.suppliers = new Supplier[currentSuppliers.length + 1];
        ArrayIterate.forEachWithIndex(
                currentSuppliers,
                new ObjectIntProcedure<Supplier>()
                {
                    @Override
                    public void value(Supplier eachSupplier, int index)
                    {
                        Company.this.suppliers[index] = currentSuppliers[index];
                    }
                }
        );
        this.suppliers[this.suppliers.length - 1] = supplier;
    }

    public Supplier[] getSuppliers()
    {
        return this.suppliers;
    }

    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");
        return null;
    }
}

@@ -687,74 +687,8 @@
 te;%0A
-import com.gs.collections.api.block.procedure.ObjectIntProcedure;%0A
 impo
@@ -789,61 +789,8 @@
 st;%0A
-import com.gs.collections.impl.utility.ArrayIterate;%0A
 impo
@@ -2109,16 +2109,17 @@
  array.%0A
+%0A
         
@@ -2250,298 +2250,46 @@
     
-ArrayIterate.forEachWithIndex(%0A                currentSuppliers,%0A                new ObjectIntProcedure%3CSupplier%3E()%0A                %7B%0A                    @Override%0A                    public void value(Supplier eachSupplier, int index)%0A                    %7B%0A                        Company.
+System.arraycopy(currentSuppliers, 0, 
 this
@@ -2302,17 +2302,12 @@
 iers
-%5Bindex%5D =
+, 0,
  cur
@@ -2323,65 +2323,15 @@
 iers
-%5Bindex%5D;%0A                    %7D%0A                %7D%0A        
+.length
 );%0A 

Use array copying in Company.addSupplier().