Skip to content

Instantly share code, notes, and snippets.

@mlesikov
Created April 21, 2011 13:40
Show Gist options
  • Save mlesikov/934491 to your computer and use it in GitHub Desktop.
Save mlesikov/934491 to your computer and use it in GitHub Desktop.
Contract field updates example 1
package com.evo.adm;
import com.opengrapes.common.widgets.util.Lists;
import org.junit.Test;
import java.util.List;
import static junit.framework.Assert.assertEquals;
/**
* @author Mihail Lesikov ([email protected])
*/
public class TestContractUpdate {
class Customer {
private String name;
public Customer(String name) {
this.name = name;
}
public void updateFrom(Customer customer2) {
for (Field field : customer2.getFields()) {
field.applyUpdate(this);
}
}
private Iterable<? extends Field> getFields() {
return Lists.newArrayList(new CustomerNameField(this));
}
}
class Contract {
private String code;
public int paymentDay;
private Customer customer;
public void updateFrom(Contract contract2) {
for (Field field : contract2.getFields()) {
field.applyUpdate(this);
}
}
public List<Field<Contract>> getFields() {
return Lists.newArrayList(new PaymentDayField(this), new CustomerField(this));
}
}
private interface Field<T> {
void applyUpdate(T t);
}
class PaymentDayField implements Field<Contract> {
private Contract owner;
PaymentDayField(Contract owner) {
this.owner = owner;
}
public void applyUpdate(Contract contract) {
contract.paymentDay = owner.paymentDay;
}
}
class CustomerField implements Field<Contract> {
private Contract owner;
CustomerField(Contract owner) {
this.owner = owner;
}
public void applyUpdate(Contract contract) {
contract.customer.updateFrom(owner.customer);
}
}
class CustomerNameField implements Field<Customer> {
private Customer owner;
CustomerNameField(Customer owner) {
this.owner = owner;
}
public void applyUpdate(Customer customer) {
customer.name = owner.name;
}
}
@Test
public void test() throws Exception {
Contract contract = new Contract();
contract.code = "1111";
contract.paymentDay = 17;
contract.customer = new Customer("lazo");
Contract contract2 = new Contract();
contract2.code = "2222";
contract2.paymentDay = 7;
contract2.customer = new Customer("mi6o");
contract.updateFrom(contract2);
assertEquals(7, contract.paymentDay);
assertEquals("mi6o", contract.customer.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment