Skip to content

Commit adf8bf2

Browse files
authored
Feature/recursive mapping support (#1)
* Adding support for recursive mapping * Adding nested elements discovery and topological sorting
1 parent 469a403 commit adf8bf2

File tree

18 files changed

+684
-133
lines changed

18 files changed

+684
-133
lines changed

.idea/misc.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/de/foodora/android/automapper/MainActivity.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,8 @@ public void onClick(View view) {
5050
try {
5151
int age = TextUtils.isEmpty(mAgeEditText.getText()) ? 0 : Integer.parseInt(mAgeEditText.getText().toString());
5252
Date date = TextUtils.isEmpty(mBdayEditText.getText()) ? new Date(System.currentTimeMillis()) : df.parse(mBdayEditText.getText().toString());
53-
Address address = Address.create(
54-
mStreetEditText.getText().toString(),
55-
mPostcodeEditText.getText().toString(),
56-
mCityEditText.getText().toString(),
57-
/* Country */ null);
53+
Address address = new Address();
5854
Restaurant restaurant = new Restaurant();
59-
6055
Intent activityIntent = RestaurantActivity.createIntent(MainActivity.this, restaurant);
6156

6257
if (activityIntent != null) {

app/src/main/java/de/foodora/android/automapper/RestaurantActivity.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.os.Parcelable;
76
import android.support.annotation.NonNull;
87
import android.support.annotation.Nullable;
98
import android.support.v7.app.AppCompatActivity;
109
import android.text.TextUtils;
1110
import android.widget.TextView;
1211

1312
import de.foodora.android.automapper.model.Restaurant;
14-
import de.foodora.android.automapper.model.RestaurantAutoMapper;
1513

1614
public class RestaurantActivity extends AppCompatActivity {
1715

1816
private static final String EXTRA_PERSON = "EXTRA_PERSON";
1917

2018
@Nullable
21-
public static Intent createIntent(@NonNull Context context, RestaurantAutoMapper person) {
19+
public static Intent createIntent(@NonNull Context context, Restaurant restaurant) {
2220
//noinspection ConstantConditions
2321
if (context == null) {
2422
return null;
2523
}
2624
Intent intent = new Intent(context, RestaurantActivity.class);
2725
// we need to cast it to Parcelable because Person does not itself implement parcelable
28-
intent.putExtra(EXTRA_PERSON, (Parcelable) person);
26+
// intent.putExtra(EXTRA_PERSON, (Parcelable) person);
2927

3028
return intent;
3129
}

app/src/main/java/de/foodora/android/automapper/model/Address.java renamed to app/src/main/java/de/foodora/android/automapper/model/ApiAddress.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
import android.os.Parcelable;
2019
import android.support.annotation.NonNull;
2120

22-
import de.foodora.automapper.AutoMapper;
21+
import de.foodora.android.automapper.model.location.ApiArea;
2322

24-
@AutoMapper(parcelable = true)
25-
public abstract class Address implements Parcelable {
23+
public class ApiAddress {
2624
@NonNull
2725
public String street;
2826

@@ -32,7 +30,5 @@ public abstract class Address implements Parcelable {
3230

3331
public String country;
3432

35-
public static Address create(@NonNull String street, String postCode, String city, String country) {
36-
return new FDAddress(street, postCode, city, country);
37-
}
33+
public ApiArea area;
3834
}

app/src/main/java/de/foodora/android/automapper/model/ApiRestaurant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public class ApiRestaurant {
1919

2020
public int branches;
2121

22-
public Address address;
22+
public ApiAddress address;
2323
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package de.foodora.android.automapper.model;
2+
3+
import java.util.Collection;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
public class ApiRestaurantSet {
8+
public List<ApiRestaurant> restaurants;
9+
public Set<ApiRestaurant> restaurantsSet;
10+
public Collection<ApiRestaurant> restaurantCollection;
11+
public ApiRestaurant[] restaurantArray;
12+
public List<String> tags;
13+
public Set<String> keywords;
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.foodora.android.automapper.model;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
class ChildModel implements Parcelable {
7+
public String name;
8+
public String description;
9+
public Integer id;
10+
public GrandChild children;
11+
12+
@Override
13+
public int describeContents() {
14+
return 0;
15+
}
16+
17+
@Override
18+
public void writeToParcel(Parcel dest, int flags) {
19+
dest.writeString(this.name);
20+
dest.writeString(this.description);
21+
dest.writeValue(this.id);
22+
dest.writeParcelable(this.children, flags);
23+
}
24+
25+
public ChildModel() {
26+
}
27+
28+
protected ChildModel(Parcel in) {
29+
this.name = in.readString();
30+
this.description = in.readString();
31+
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
32+
this.children = in.readParcelable(GrandChild.class.getClassLoader());
33+
}
34+
35+
public static final Parcelable.Creator<ChildModel> CREATOR = new Parcelable.Creator<ChildModel>() {
36+
@Override
37+
public ChildModel createFromParcel(Parcel source) {
38+
return new ChildModel(source);
39+
}
40+
41+
@Override
42+
public ChildModel[] newArray(int size) {
43+
return new ChildModel[size];
44+
}
45+
};
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.foodora.android.automapper.model;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
class GrandChild implements Parcelable {
7+
public String name;
8+
public String details;
9+
public Double length;
10+
public int id;
11+
12+
@Override
13+
public int describeContents() {
14+
return 0;
15+
}
16+
17+
@Override
18+
public void writeToParcel(Parcel dest, int flags) {
19+
dest.writeString(this.name);
20+
dest.writeString(this.details);
21+
dest.writeValue(this.length);
22+
dest.writeInt(this.id);
23+
}
24+
25+
public GrandChild() {
26+
}
27+
28+
protected GrandChild(Parcel in) {
29+
this.name = in.readString();
30+
this.details = in.readString();
31+
this.length = (Double) in.readValue(Double.class.getClassLoader());
32+
this.id = in.readInt();
33+
}
34+
35+
public static final Parcelable.Creator<GrandChild> CREATOR = new Parcelable.Creator<GrandChild>() {
36+
@Override
37+
public GrandChild createFromParcel(Parcel source) {
38+
return new GrandChild(source);
39+
}
40+
41+
@Override
42+
public GrandChild[] newArray(int size) {
43+
return new GrandChild[size];
44+
}
45+
};
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.foodora.android.automapper.model;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class ParentModel implements Parcelable {
10+
public String name;
11+
public String description;
12+
public List<ChildModel> children;
13+
14+
@Override
15+
public int describeContents() {
16+
return 0;
17+
}
18+
19+
@Override
20+
public void writeToParcel(Parcel dest, int flags) {
21+
dest.writeString(this.name);
22+
dest.writeString(this.description);
23+
dest.writeList(this.children);
24+
}
25+
26+
public ParentModel() {
27+
}
28+
29+
protected ParentModel(Parcel in) {
30+
this.name = in.readString();
31+
this.description = in.readString();
32+
this.children = new ArrayList<ChildModel>();
33+
in.readList(this.children, ChildModel.class.getClassLoader());
34+
}
35+
36+
public static final Parcelable.Creator<ParentModel> CREATOR = new Parcelable.Creator<ParentModel>() {
37+
@Override
38+
public ParentModel createFromParcel(Parcel source) {
39+
return new ParentModel(source);
40+
}
41+
42+
@Override
43+
public ParentModel[] newArray(int size) {
44+
return new ParentModel[size];
45+
}
46+
};
47+
}

app/src/main/java/de/foodora/android/automapper/model/RestaurantAutoMapper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@
1616
* limitations under the License.
1717
*/
1818

19+
import de.foodora.android.automapper.model.location.gps.ApiGps;
1920
import de.foodora.automapper.AutoMapper;
2021

21-
@AutoMapper(map = ApiRestaurant.class, targetName = "Restaurant", parcelable = true)
22-
public abstract class RestaurantAutoMapper {
22+
public class RestaurantAutoMapper {
2323

24-
public String frontSign;
24+
@AutoMapper(mapFrom = ApiRestaurant.class, mapTo = "Restaurant")
25+
public static abstract class RestaurantAutoMapperClass {
26+
public String frontSign;
2527

26-
void map(Restaurant restaurant) {
27-
restaurant.frontSign = restaurant.name + "| " + restaurant.slogan;
28+
void map(Restaurant restaurant) {
29+
restaurant.frontSign = restaurant.name + "| " + restaurant.slogan;
30+
}
2831
}
32+
33+
@AutoMapper(mapFrom = ApiAddress.class, mapTo = "Address", extendMapper = false)
34+
public static abstract class AddressAutoMapper { }
35+
36+
@AutoMapper(mapFrom = ApiGps.class, mapTo = "Gps", extendMapper = false)
37+
public static abstract class GpsAutoMapper { }
38+
39+
@AutoMapper(mapFrom = ApiRestaurantSet.class, mapTo = "RestaurantSet", extendMapper = false)
40+
public static abstract class RestaurantSetAutoMapper { }
41+
2942
}

0 commit comments

Comments
 (0)