Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best approach for marshaling unmarshaling immutable objects with builder pattern

I have a simple restful service that I'm developing in java. I have been looking at a few of the options for marshalling/unmarshalling json. The possible approaches available, jaxb jackson etc, are quite new to me and I'm trying to find my feet with them. I was wondering if I could get some advice on what would be the best approach and technology to use especially given that many of the objects I'm interested in I have implemented as being immutable and I have used the builder pattern. So there are no setters and the constructor is private.

I have looked at this previous question: Jackson + Builder Pattern? posted on stackoverflow. I am considering something like this approach although it would be great to get some pointers to more resources about using @JsonDeserialize

Here is a very simple example of the type of object I'm considering

public class Reading {

private final double xCoord;
private final double yCoord;
private final double diameter;
private final double reliability;
private final String qualityCode;


private Reading(Builder builder){
    xCoord = builder.xCoord;
    yCoord = builder.yCoord;
    diameter = builder.diameter;
    reliability = builder.reliability;
    qualityCode = builder.qualityCode;
}


public static class Builder {
    //required parameters
    private final double diameter;
    //optional parameters
    private double xCoord = 0.0;
    private double yCoord = 0.0;
    private double reliability = 1.0;
    private String qualityCode;


    public Builder (double diameter){
        this.diameter = diameter;
    }

    public Builder reliability(double val){
        reliability = val;
        return this;
    }

    public Builder qualityCode(String qualityCode){
        this.qualityCode = qualityCode;
        return this;
    }

    public Builder coordinates(double xCoord, double yCoord){
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        return this;
    }

    public Reading build(){
        return new Reading(this);
    }

}

public double getXCoord() {return xCoord;}

public double getYCoord() {return yCoord;}

public String getQualityCode() {return qualityCode;}

public double getDiameter() { return diameter;}

public double getReliability() {return reliability; }

}

There are no problems marshalling this object but unmarshalling doesn't seem to be straight forward. Also is there support for leaving out entries for object values that are null?

like image 979
Conor Avatar asked Jun 14 '11 10:06

Conor


People also ask

Is Builder pattern immutable?

The builder pattern hides the complexities of creating a class. In the case of an immutable class, it can be used to avoid constructors with too many parameters. Since the builder is not immutable, the values can be set through multiple calls.

Is Builder pattern thread safe?

Last, but not least, builders are not thread-safe. While immutable objects can be freely shared between threads, builders cannot because of the mutable state.

What is meant by marshalling and Unmarshalling in Java?

Marshalling is converting the data present in an object into a an xml format and viewing it in an xml format and unmarshalling is reverse of it converting an xml file into an object.


1 Answers

you can do this: (implement only getters and use XmlAccessType.FIELD)

@XmlAccessorType(XmlAccessType.FIELD)
public class CreditCardVO implements Serializable {

  private Long ccNumber;
  private String ccName;


  public CreditCardVO(Long ccNumber, String ccName) {
   this.ccNumber = ccNumber;
   this.ccName = ccName;
  }


  private CreditCardVO() {
     // for JAXB's Magic
  }

  public Long getCcNumber() {
    return ccNumber;
  }

  public String getCcName() {
   return ccName;
  }    
}

taken from http://aniketshaligram.blogspot.com/2010/05/jaxb-immutable-objects.html

like image 108
ekeren Avatar answered Sep 29 '22 09:09

ekeren