Java Wrapper classes

I have a bunch of classes (a few 10s), that got generated from the .proto files I have. These classes are configuration items. So, let's say I have:

class config_item_1 {
    string Id;
    ...
    ...
}

class config_item_2 {
    string Id;
    ...
    ...
}

I have instances of these classes which represent my system configuration. I'm building logic in my code to keep them in-memory, performing some transformations, and at the end, I need to figure out which objects changed. That is, some objects might stay untouched, some might go away, some might change.

So, for all these classes, I need to add a "status" without changing the actual class definitions. Something like a wrapper.

What is the best way to achieve this?

EDIT:

Another "hack" I'm considering is, since my config item classes are generated classes from proto file, like I mentioned, I'm thinking of creating a wrapper proto, like this:

message ConfigCacheItem {
    enum ItemState {
        UNTOUCHED = 0;
        CREATED = 1;
        UPDATED = 2;
        DELETED = 3;
    }
    ItemState item_state = 1;
    String id = 2; /* ID stashed from one of the items below for lookup */
    oneof config_item {
        ConfigItem1 config_item_1 = 3;
        ConfigItem2 config_item_2 = 4;
        ....
    }
}

Straight forward way is to create wrapper class and extend/ override setter and use them for your status tracking

Wrapper Classes in Java, Wrapper Classes in Java. 20-12-2016. A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper � Java Wrapper Classes. Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.The table below shows the primitive type and the equivalent wrapper class:


I believe that you need a Decorator Pattern. Something like the example bellow.

  public class Decorator_config_item_1 {
    private config_item_1 item_1;
    private Boolean status;

    public Decorator_config_item_1(config_item_1 item_1) {
        this.config_item_1 = item_1;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

}

Wrapper Classes in Java, As the name suggests, wrapper classes are objects encapsulating primitive Java types. Each Java primitive has a corresponding wrapper:. Wrapper Classes in Java A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types.


The initial problem is that generated code is almost always the wrong way to go--yet some people insist so let's make the best out of it.

You can extend the class and add functionality. This is probably a bad idea in your case:

class ConfigItem1Wrapper extends config_item_1 {
    public Boolean getStatus()
    {…}
    ...
}

The main problem here is that you will have ~10 of these classes so a lot of duplicated code or forwarding. It's also going to give you problems if whatever system you are using to create these classes also wants to instantiate them... if it does you will get instances of "config_item_1" instead of the more awesomely named "ConfigItem1Wrapper"

You can encapsulate the class in another class as a delegate. This isn't as bad as it seems. Some editors can automatically create classes like this. Each method is re-created and the call is forwarded to the copy you store internally.

class ConfigItem1Wrapper {
    config_item_1 delegate;

    String some_method1(var) {
        return delegate.some_method1(var);
    }
}

If you can use delegation like this (Hopefully using some IDEs code generation) it might be the best way, it gives you complete control.

Were you to use Groovy (A superset of Java) you'd find it has an @Delegate annotation that does this all for you. Just this:

class ConfigItem1Wrapper {
    @Delegate config_item_1 delegate;

    public Boolean getStatus() {
        return status;
    }
}

would generate all the methods from config_item_1 onto ConfigItem1Wrapper with your additional code. I'm guessing that won't work for you. Pity.

Honestly nearly any other solution I could give you is some type of delegation. I recommend you just find some way to make delegation work (without having to forward each method manually)

Wrapper class in Java, The eight primitive data types byte, short, int, long, float, double, char and boolean are not objects, Wrapper classes are used for converting primitive data types� We use wrapper classes to use these data types in the form of objects. Wrapper class in Java makes the Java code fully object-oriented. For example, converting an int to Integer. Here int is a data type and Integer is the wrapper class of int. We will discuss the concept of wrapper classes in Java with the examples.


Wrapper class in Java, Wrapper classes in Java. The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. Since J2SE 5.0� The difference between wrapper classes and primitive types. Primitive wrapper classes are not the same thing as primitive types. Whereas variables, for example, can be declared in Java as data types double, short, int, etc., the primitive wrapper classes create instantiated objects and methods that inherit but hide the primitive data types, not like variables that are assigned the data type


Why do we need a wrapper class in Java?, A Wrapper class is a class which contains the primitive data types (int, char, short, byte, etc). In other words, wrapper classes provide a way to� The wrapper classes are part of the java.lang package, which is imported by default into all Java programs. The wrapper classes in java servers two primary purposes. To provide a mechanism to ‘wrap’ primitive values in an object so that primitives can do activities reserved for the objects like being added to ArrayList, Hashset, HashMap etc


What are wrapper classes in Java?, Wrapper classes are those whose objects wraps a primitive data type within them .In the java.lang package java provides a separate class for� In the OOPs concepts guide, we learned that object oriented programming is all about objects. The eight primitive data types byte, short, int, long, float, double, char and boolean are not objects, Wrapper classes are used for converting primitive data types into objects, like int to Integer etc. Lets take a simple example to understand why we need wrapper class in java.