1

I recently came across the concept of using AutoValues (GSON) extension to speed up parsing our json data. We actuall found it parsed our json twice as fast.

But I am missing a point, the data set while using AutoValues is immutable, but often our data sets/models are similar to the json and there are no mappers required.

So if I want to set individual data elements in my model classes this does not look feasible(since we have only abstract getters and no setters. Is there a way around this problem or am I missing a point. I will explain using a sample code of what we do and the similar syntax using AutoValue.

So we usually write the code something like

public class ModelClass{
    private String aValue;
    private String anotherValue;
    public String getAValue(){
        return this.aValue;
    }
    public void setAValue(String aValue){
        this.aValue = aValue;
    }
    public String getAnotherValue(){
        return this.anotherValue;
    }
    public String setAnotherValue(anotherValue){
        this.anotherValue = anotherValue
    }
}

public class ViewClass{
     public void foo(){
          String json = {"aValue":"This is a sample string", "anotherValue": "this is another string"};
          ModelClass model = gson.fromJson(json, ModelClass.class);
          model.getAValue();
          model.setAValue("this is a new value"); // and we can set individual element ModelClass.aValue


     }
}

But while using Auto Value the structure of the Model Class changes to

  @AutoValue public abstract class AutoValueModel{
       public abstract String bValue();
       public abstract String otherValue();

       public static AutoValueModel(String bValue, String otherValue){
           return new AutoValue_AutoValueModel(bValue, otherValue);
       }
  }

// Now as you can see AutoValueModel structure does not contain any setters what if I may want to change just the bValue (based on say a user action, even though I know that the basic premise behind AutoValues is them being immutable) and continue using in other parts of code. And then serialize a json using the very same AutoValueModel. Or should I deserialize using AutoValue technique and then use a mapped model on which I can perform changes to the dataset? (Will I lose the benefit of speed attained by using AutoValue if I use this technique?)

Also reference from where I learnt about AutoValues :

An Introduction to AutoValue

FASTER JSON DESERIALIZATION WITH AUTOVALUE GSON EXTENSION

ichthyocentaurs
  • 2,173
  • 21
  • 35

1 Answers1

1

what if I may want to change just the bValue ... and continue using in other parts of code?

If you want to update the certain value of @AutoValued class, you need to make use of @AutoValue.Builder to create a new class WITH updated value, as Vincent Dubedout replied to your comment on his blog.

You would have a class with a static builder class with @AutoValue.Builder annotation like this one, and when you need to have a class with updated value, you would create a new class using this builder to have a class only the certain value updated.

https://github.com/google/auto/blob/master/value/userguide/builders.md#autovalue-with-builders

I hope this helps.

shoheikawano
  • 1,092
  • 3
  • 14
  • 31
  • 2
    I agree with this approach but would I not end up creating a new object using the builder approach each time I need to change a value of 1 variable. Imagine a class which has a 100 variables, now say I need to update all variables at different time intervals based on user action, now will this not mitigate any performance improvements I attained by parsing the json faster? – ichthyocentaurs Jul 26 '16 at 14:06
  • If you "need to change a value of 1 variable" so many times, then I would recommend you rethink how you treat your data models inside your app perhaps. AutoValue simply helps developers create a value type; [an immutable objects whose equality is based on property values](http://ryanharter.com/blog/2016/03/22/autovalue/) with ease and I would not surprise what AutoValue is heading is not always what everyone is willing to do. – shoheikawano Jul 26 '16 at 14:25
  • I am not saying you need to change one variable multiple times(although that is entirely possible for always in sync systems like ours), my question is more regarding the performance offset which was attained y faster parsing. Although after looking at it deeper I think yours is the only valid approach to perform a setter action on data elements. So accepting the answer – ichthyocentaurs Jul 27 '16 at 06:07