Last active
August 29, 2015 14:07
-
-
Save thomasdarimont/782ed24e9eda8cff5328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bob.asm; | |
/** | |
* @author Thomas Darimont | |
*/ | |
public interface BeanAccessor { | |
Object getProperty(String name); | |
void setProperty(String name, Object value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bob.model; | |
/** | |
* @author Thomas Darimont | |
*/ | |
public class Person { | |
String firstname; | |
String lastname; | |
int age; | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Person person = (Person) o; | |
if (age != person.age) return false; | |
if (firstname != null ? !firstname.equals(person.firstname) : person.firstname != null) return false; | |
if (lastname != null ? !lastname.equals(person.lastname) : person.lastname != null) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = firstname != null ? firstname.hashCode() : 0; | |
result = 31 * result + (lastname != null ? lastname.hashCode() : 0); | |
result = 31 * result + age; | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "Person{" + | |
"firstname='" + firstname + '\'' + | |
", lastname='" + lastname + '\'' + | |
", age=" + age + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bob.asm.exp; | |
import java.util.function.Consumer; | |
import java.util.function.Supplier; | |
import bob.asm.BeanAccessor; | |
import bob.model.Person; | |
/** | |
* @author Thomas Darimont | |
*/ | |
public class PersonIndyWrapper implements BeanAccessor { | |
private final Consumer<String> firstname_set; | |
private final Consumer<String> lastname_set; | |
private final Consumer<Integer> age_set; | |
private final Supplier<String> firstname_get; | |
private final Supplier<String> lastname_get; | |
private final Supplier<Integer> age_get; | |
public PersonIndyWrapper(Person instance) { | |
this.firstname_set = instance::setFirstname; | |
this.lastname_set = instance::setLastname; | |
this.age_set = instance::setAge; | |
this.firstname_get = instance::getFirstname; | |
this.lastname_get = instance::getLastname; | |
this.age_get = instance::getAge; | |
} | |
public Object getProperty(String name) { | |
switch (name) { | |
case "firstname": | |
return firstname_get.get(); | |
case "lastname": | |
return lastname_get.get(); | |
case "age": | |
return age_get.get(); | |
default: | |
return null; | |
} | |
} | |
public void setProperty(String name, Object value) { | |
switch (name) { | |
case "firstname": | |
firstname_set.accept((String) value); | |
return; | |
case "lastname": | |
lastname_set.accept((String) value); | |
return; | |
case "age": | |
age_set.accept((Integer) value); | |
return; | |
default: | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment