Skip to content

Commit

Permalink
Create PaymentMethod model
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe committed Feb 22, 2019
1 parent ea40ee8 commit 3df2511
Show file tree
Hide file tree
Showing 9 changed files with 836 additions and 181 deletions.
166 changes: 95 additions & 71 deletions stripe/src/main/java/com/stripe/android/model/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.stripe.android.utils.ObjectUtils;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -54,12 +56,12 @@ public class Address extends StripeJsonModel implements Parcelable {
@Nullable private String mState;

private Address(
String city,
String country,
String line1,
String line2,
String postalCode,
String state) {
@Nullable String city,
@Nullable String country,
@Nullable String line1,
@Nullable String line2,
@Nullable String postalCode,
@Nullable String state) {
mCity = city;
mCountry = country;
mLine1 = line1;
Expand All @@ -69,12 +71,17 @@ private Address(
}

private Address(@NonNull Builder addressBuilder) {
mCity = addressBuilder.mCity;
mCountry = addressBuilder.mCountry;
mLine1 = addressBuilder.mLine1;
mLine2 = addressBuilder.mLine2;
mPostalCode = addressBuilder.mPostalCode;
mState = addressBuilder.mState;
this(addressBuilder.mCity, addressBuilder.mCountry, addressBuilder.mLine1,
addressBuilder.mLine2, addressBuilder.mPostalCode, addressBuilder.mState);
}

protected Address(@NonNull Parcel in) {
mCity = in.readString();
mCountry = in.readString();
mLine1 = in.readString();
mLine2 = in.readString();
mPostalCode = in.readString();
mState = in.readString();
}

@Nullable
Expand Down Expand Up @@ -140,20 +147,20 @@ public void setState(String state) {
@NonNull
@Override
public Map<String, Object> toMap() {
Map<String, Object> hashMap = new HashMap<>();
hashMap.put(FIELD_CITY, mCity);
hashMap.put(FIELD_COUNTRY, mCountry);
hashMap.put(FIELD_LINE_1, mLine1);
hashMap.put(FIELD_LINE_2, mLine2);
hashMap.put(FIELD_POSTAL_CODE, mPostalCode);
hashMap.put(FIELD_STATE, mState);
return hashMap;
final Map<String, Object> map = new HashMap<>();
map.put(FIELD_CITY, mCity);
map.put(FIELD_COUNTRY, mCountry);
map.put(FIELD_LINE_1, mLine1);
map.put(FIELD_LINE_2, mLine2);
map.put(FIELD_POSTAL_CODE, mPostalCode);
map.put(FIELD_STATE, mState);
return map;
}

@NonNull
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
final JSONObject jsonObject = new JSONObject();
putStringIfNotNull(jsonObject, FIELD_CITY, mCity);
putStringIfNotNull(jsonObject, FIELD_COUNTRY, mCountry);
putStringIfNotNull(jsonObject, FIELD_LINE_1, mLine1);
Expand All @@ -177,16 +184,65 @@ public static Address fromJson(@Nullable JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
String city = optString(jsonObject, FIELD_CITY);
String country = optString(jsonObject, FIELD_COUNTRY);
String line1 = optString(jsonObject, FIELD_LINE_1);
String line2 = optString(jsonObject, FIELD_LINE_2);
String postalCode = optString(jsonObject, FIELD_POSTAL_CODE);
String state = optString(jsonObject, FIELD_STATE);

final String city = optString(jsonObject, FIELD_CITY);
final String country = optString(jsonObject, FIELD_COUNTRY);
final String line1 = optString(jsonObject, FIELD_LINE_1);
final String line2 = optString(jsonObject, FIELD_LINE_2);
final String postalCode = optString(jsonObject, FIELD_POSTAL_CODE);
final String state = optString(jsonObject, FIELD_STATE);
return new Address(city, country, line1, line2, postalCode, state);
}

@Override
public boolean equals(Object obj) {
return this == obj || (obj instanceof Address && typedEquals((Address) obj));
}

private boolean typedEquals(@NonNull Address obj) {
return ObjectUtils.equals(mCity, obj.mCity)
&& ObjectUtils.equals(mCountry, obj.mCountry)
&& ObjectUtils.equals(mLine1, obj.mLine1)
&& ObjectUtils.equals(mLine2, obj.mLine2)
&& ObjectUtils.equals(mPostalCode, obj.mPostalCode)
&& ObjectUtils.equals(mState, obj.mState);
}

@Override
public int hashCode() {
return ObjectUtils.hash(mCity, mCountry, mLine1, mLine2, mPostalCode, mState);
}

/************** Parcelable *********************/
public static final Parcelable.Creator<Address> CREATOR
= new Parcelable.Creator<Address>() {

@Override
public Address createFromParcel(Parcel in) {
return new Address(in);
}

@Override
public Address[] newArray(int size) {
return new Address[size];
}
};

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mCity);
out.writeString(mCountry);
out.writeString(mLine1);
out.writeString(mLine2);
out.writeString(mPostalCode);
out.writeString(mState);
}

@Override
public int describeContents() {
return 0;
}

public static class Builder {
private String mCity;
private String mCountry;
Expand All @@ -195,78 +251,46 @@ public static class Builder {
private String mPostalCode;
private String mState;

public Builder setCity(String city) {
@NonNull
public Builder setCity(@Nullable String city) {
mCity = city;
return this;
}

@NonNull
public Builder setCountry(@NonNull String country) {
mCountry = country.toUpperCase(Locale.ROOT);
return this;
}

public Builder setLine1(String line1) {
@NonNull
public Builder setLine1(@Nullable String line1) {
mLine1 = line1;
return this;
}

public Builder setLine2(String line2) {
@NonNull
public Builder setLine2(@Nullable String line2) {
mLine2 = line2;
return this;
}

public Builder setPostalCode(String postalCode) {
@NonNull
public Builder setPostalCode(@Nullable String postalCode) {
mPostalCode = postalCode;
return this;
}

public Builder setState(String state) {
@NonNull
public Builder setState(@Nullable String state) {
mState = state;
return this;
}

@NonNull
public Address build() {
return new Address(this);
}

}

/************** Parcelable *********************/
public static final Parcelable.Creator<Address> CREATOR
= new Parcelable.Creator<Address>() {

@Override
public Address createFromParcel(Parcel in) {
return new Address(in);
}

@Override
public Address[] newArray(int size) {
return new Address[size];
}
};

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mCity);
out.writeString(mCountry);
out.writeString(mLine1);
out.writeString(mLine2);
out.writeString(mPostalCode);
out.writeString(mState);
}

@Override
public int describeContents() {
return 0;
}

protected Address(Parcel in) {
mCity = in.readString();
mCountry = in.readString();
mLine1 = in.readString();
mLine2 = in.readString();
mPostalCode = in.readString();
mState = in.readString();
}
}
Loading

0 comments on commit 3df2511

Please sign in to comment.