Skip to content

Instantly share code, notes, and snippets.

@mmpataki
Last active July 28, 2021 07:57
Show Gist options
  • Select an option

  • Save mmpataki/39c259b6fea72f94310e1810c23c112e to your computer and use it in GitHub Desktop.

Select an option

Save mmpataki/39c259b6fea72f94310e1810c23c112e to your computer and use it in GitHub Desktop.

Revisions

  1. mmpataki revised this gist Jul 28, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    new object
    Dish{name='idli', originCountry='india', recipe='ask your mom!', updates={}}
    update an object
    Dish{name='idli', originCountry='india', recipe='mom is not home, get it from a hotel', updates={recipe=mom is not home, get it from a hotel}}
  2. mmpataki created this gist Jul 28, 2021.
    54 changes: 54 additions & 0 deletions Dish.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /*
    * Model to be stored
    */
    public class Dish extends Updateable {

    String name;
    String originCountry;
    String recipe;

    public Dish(String name, String originCountry, String recipe) {
    this.name = name;
    this.originCountry = originCountry;
    this.recipe = recipe;
    }

    /* updates */
    public void setName(String name) {
    doUpdate("name", this.name, name);
    this.name = name;
    }

    public void setOriginCountry(String originCountry) {
    doUpdate("originCountry", this.originCountry, originCountry);
    this.originCountry = originCountry;
    }

    public void setRecipe(String recipe) {
    doUpdate("recipe", this.recipe, recipe);
    this.recipe = recipe;
    }

    /* getters */
    public String getName() {
    return name;
    }

    public String getOriginCountry() {
    return originCountry;
    }

    public String getRecipe() {
    return recipe;
    }

    @Override
    public String toString() {
    return "Dish{" +
    "name='" + name + '\'' +
    ", originCountry='" + originCountry + '\'' +
    ", recipe='" + recipe + '\'' +
    ", updates=" + updates +
    '}';
    }
    }
    16 changes: 16 additions & 0 deletions Main.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    public class Main {
    public static void main(String[] args) {

    UpdateableMongoStore ums = new UpdateableMongoStore();

    Dish idli = new Dish("idli", "india", "ask your mom!");

    /* save it first time */
    ums.save(idli);

    /* update it */
    idli.setRecipe("mom is not home, get it from a hotel");

    ums.save(idli);
    }
    }
    23 changes: 23 additions & 0 deletions Updateable.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import java.util.HashMap;

    public abstract class Updateable {

    public HashMap<String, Object> updates = new HashMap<>();

    public void put(String field, Object value) {
    updates.put(field, value);
    }

    public boolean isUpdated() {
    return !updates.isEmpty();
    }

    public HashMap<String, Object> getUpdates() {
    return updates;
    }

    protected void doUpdate(String key, String curVal, String newVal) {
    if(!curVal.equals(newVal))
    updates.put(key, newVal);
    }
    }
    22 changes: 22 additions & 0 deletions UpdateableMongoStore.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    public class UpdateableMongoStore {

    public void save(Updateable updateable) {

    if(!updateable.isUpdated()) {

    System.out.println("new object");
    System.out.println(updateable);

    } else {

    System.out.println("update an object");
    System.out.println(updateable);

    /* build some update query using `updateable.getUpdates()` */

    /* save them to store */
    }
    }

    /* other store methods */
    }