-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Max Berkelmans edited this page Aug 10, 2019
·
2 revisions
Migrational is a simple and small library which takes advantage of reflection to migrate data in objects.
If you are using Maven add the following to your pom.xml file Add this in your <repositories> block
<repository>
<id>lemmotresto</id>
<url>http://repo.lemmotresto.com/</url>
</repository>And this in your <dependencies> block
<dependency>
<groupId>me.max</groupId>
<artifactId>migrational</artifactId>
<version>VERSION</version>
</dependency>If you are using Gradle add this to your build.gradle file
dependencies {
compile 'me.max:migrational:VERSION'
}
repositories {
maven {
url "http://repo.lemmotresto.com/"
}
}This is a field example it uses the @Migratable annotation on fields to show what fields should be migrated.
public class FieldExampleObject {
@Migratable(key = "name", defaultValue = "Max") private String name = "Lily";
@Migratable private int age;
@Migratable(defaultValue = "Netherlands") private String country;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCountry() {
return country;
}
public static void main(String[] args){
//'Load' the data in this case just creating a map but in reality you load this from a file.
Map<String, Object> data = new HashMap<>();
//name is in there with the key
//age is in there without a key so it will use the field name
//country is not in there so defaultValue will be used
//if no defaultValue present it will use field value
data.put("name", "Stijn");
data.put("age", 20);
//Instantiate the migrator
Migrator mig = new Migrator(FieldExampleObject.class, data);
try {
//Migrate
mig.migrate();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
//Cast the result
FieldExampleObject result = (FieldExampleObject) mig.getLastMigratedObject();
//Print the values
System.out.println(result.getName());
System.out.println(result.getAge());
System.out.println(result.getCountry());
}
}This next example uses the @Migratable annotation on the class. This will migrate every field in this class besides the fields annotated with the @Exempt annotation.
//This will migrate every field unless it has the @Exempt annotation.
@Migratable
public class ClassExampleObject {
//Will migrate
private String name = "Stijn";
private boolean isCool = true;
//Wont migrate
@Exempt private int age = 27;
public String getName() {
return name;
}
public boolean isCool() {
return isCool;
}
public int getAge() {
return age;
}
public static void main(String[] args){
//'Load' the data in this case just creating a map but in reality you load this from a file.
Map<String, Object> data = new HashMap<>();
//Only name is in here so isCool will default to it's initialised value (true).
data.put("name", "Stijn");
//Instantiate the migrator
Migrator mig = new Migrator(ClassExampleObject.class, data);
try {
//Migrate
mig.migrate();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
//Cast the result
ClassExampleObject result = (ClassExampleObject) mig.getLastMigratedObject();
//Print the values
System.out.println(result.getName());
System.out.println(result.isCool());
System.out.println(result.getAge());
}
}Copyright 2019 Max Berkelmans