From cb576f117b923c4b6d1159de500c1066f2ae1bc1 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Wed, 16 Feb 2022 09:23:49 -0600 Subject: [PATCH 1/3] docs: custom city class --- .../firestore/snippets/model/City.java | 66 +++++++------------ 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java index 9c96f5668..3e777ea19 100644 --- a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java @@ -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 { @@ -29,13 +31,10 @@ public class City { private Long population; private List 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, @@ -43,20 +42,19 @@ public City( Boolean capital, Long population, List 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; } @@ -104,62 +102,43 @@ public List getRegions() { public void setRegions(List 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) @@ -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] From b1dd657ee0c49dc7df637734716590398e05e84a Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Thu, 17 Feb 2022 10:23:09 -0600 Subject: [PATCH 2/3] docs: document id --- .../com/example/firestore/snippets/model/City.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java index 3e777ea19..5b5addf76 100644 --- a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java @@ -18,12 +18,15 @@ import java.util.List; import java.util.Objects; +import com.google.firebase.firestore.DocumentId; // [START fs_class_definition] // [START firestore_data_custom_type_definition] /** Represents a city : name, weather, population, country, capital, geo coordinates. */ public class City { + @DocumentId + private String id; private String name; private String state; private String country; @@ -55,6 +58,14 @@ public City( // public getters and setters for all fields // [START_EXCLUDE] + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public String getName() { return name; } From ee5253cf920a533bb08f8cdba6f0ee055821049c Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 13 Mar 2022 15:53:51 -0500 Subject: [PATCH 3/3] docs: adding {} to fix style violation --- .../java/com/example/firestore/snippets/model/City.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java index 5b5addf76..00eef1450 100644 --- a/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/model/City.java @@ -146,9 +146,9 @@ public String toString() { @Override public boolean equals(Object obj) { - if (obj == this) return true; - if (obj == null) return false; - if (obj.getClass() != this.getClass()) 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)