samedi 23 avril 2016

Loading compile time constants from properties file

I am working on a java application, where in we receive xml, the xml contains some data from a lot of regions across the globe. So xml looks something like this

<Shipment>
         <Region>
                <name>China</name>
                <calculatedValue>0.001</calculatedValue>
                <deviation>-1<deviation>  
         </Region>
         <Region>
                <name>Russia</name>
                <calculatedValue>0.091</calculatedValue>
                <deviation>-1<deviation>  
         </Region>
         <Region>
                <name>UK</name>
                <calculatedValue>2.001</calculatedValue>
                <deviation>-12<deviation>  
         </Region>
         // lots of other regions //
</Shipment>

We have one pojo called Dock.

public class Dock {

  private Map<SupportedRegion, Region> regionalShipmentDetails = //
  //some field and getter setter.
  getChinaShipmentDetails () { this.regionalShipmentDetails.get(SupportedRegions.CHINA)}
  setChinaShipmentDetails (Region china) {
      this.regionalShipmentDetails.get(SupportedRegions.CHINA).add(china)
  }

  getRussiaShipmentDetails () {//same for russia}
  setRussiaShipmentDetails (Region russia) {same for russia}
}

We extract the region out of this xml create a region object and add to Dock object. Now we also maintain a enum, which defines all the supported by our application.

enum SupportedRegions{
     CHINA (201, "APAC"),
     RUSSIA (202, "EUR")  
}

Now we are getting requirement to support other regions as well. Now we want to refactor our code so that it becomes easy and configurable to support regions by support team.

We want to move it out of code and maintain in a property file with minimal changes. We have some calling code like.

dock.getChinaShipmentDetails()
dock.getRussiaShipmentDetails ().

Now if i remove the enum and move all the regions in properties. I will have to modify my Dock class to something like this

private Map<String, Region> regionalShipmentDetails = //
  //some field and getter setter.
  getShipmentDetailsForRegion (String region) { this.regionalShipmentDetails.get(region)
  }
}

Now my caller will becomes

 Region china = dock.getShipmentDetailsForRegion ("region") 

Now i dont want to introduce the hardcoded string values in code,because this change affects my caller class and now its become the responsibility of caller class to pass the correct reion, previously Dock was exposing method fore regions supported only.

Is there any way at compile time i dont have to use hardcoded string.

Aucun commentaire:

Enregistrer un commentaire