Skip to content

Commit

Permalink
docs: custom city class
Browse files Browse the repository at this point in the history
  • Loading branch information
paulsmithkc committed Feb 16, 2022
1 parent e21734c commit cb576f1
Showing 1 changed file with 24 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;
import java.util.Objects;

// [START fs_class_definition]
// [START firestore_data_custom_type_definition]
/** Represents a city : name, weather, population, country, capital, geo coordinates. */
public class City {

Expand All @@ -29,34 +31,30 @@ public class City {
private Long population;
private List<String> regions;

// [START fs_class_definition]
// [START firestore_data_custom_type_definition]
public City() {
// Must have a public no-argument constructor
}

// Initialize all fields of a city
public City(
String name,
String state,
String country,
Boolean capital,
Long population,
List<String> regions) {
// Initialize all fields of a city
// [START_EXCLUDE]
this.name = name;
this.state = state;
this.country = country;
this.capital = capital;
this.population = population;
this.regions = regions;
}
// [END firestore_data_custom_type_definition]
// [END fs_class_definition]

public City(String name) {
this.name = name;
// [END_EXCLUDE]
}

// public getters and setters for all fields
// [START_EXCLUDE]
public String getName() {
return name;
}
Expand Down Expand Up @@ -104,62 +102,43 @@ public List<String> getRegions() {
public void setRegions(List<String> regions) {
this.regions = regions;
}
// [END_EXCLUDE]

private String getDefinedValue(String s) {
if (s != null) {
return s;
} else {
return "";
}
}

// [START_EXCLUDE]
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (name != null) {
sb.append(name);
sb.append(" name : ").append(name).append(",");
}
if (state != null) {
sb.append(" state : ");
sb.append(state);
sb.append(",");
sb.append(" state : ").append(state).append(",");
}
if (country != null) {
sb.append(", ");
sb.append(country);
sb.append(" country : ").append(country).append(",");
}
sb.append(" : [");
if (population != null) {
sb.append(" population : ");
sb.append(population);
sb.append(",");
sb.append(" population : ").append(population).append(",");
}
if (capital != null) {
sb.append(" capital : ");
sb.append(capital);
sb.append(",");
sb.append(" capital : ").append(capital).append(",");
}
if (regions != null) {
sb.append(" regions : [");
for (String r : regions) {
sb.append(r);
sb.append(", ");
}
sb.append("],");
sb.append(" regions : ").append(regions).append(",");
}
// remove trailing comma
if (sb.lastIndexOf(",") >= sb.length() - 1) {
sb.deleteCharAt(sb.length() - 1);
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
sb.append(" ]");
return sb.toString();
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof City)) {
return false;
}
if (obj == this) return true;
if (obj == null) return false;
if (obj.getClass() != this.getClass()) return false;

City city = (City) obj;
return Objects.equals(name, city.name)
&& Objects.equals(state, city.state)
Expand All @@ -173,4 +152,7 @@ public boolean equals(Object obj) {
public int hashCode() {
return Objects.hash(name, state, country, capital, population, regions);
}
// [END_EXCLUDE]
}
// [END firestore_data_custom_type_definition]
// [END fs_class_definition]

0 comments on commit cb576f1

Please sign in to comment.