I am new to this.... here is my problem...
I am trying to create a JSON response, in which I have to add different URLs...based on there extension(like .flv or .mp4)...
currently, I have hardcoded my method to accept only flv and mp4 files, but I want to generalize the method...

here is the hardcoded way..

Java Code:
BitrateUrls burls = new BitrateUrls();
Encoding enocflv = new Encoding();
Encoding enocmp4 = new Encoding();


 else if (nName.startsWith("original_") && nName.endsWith(".flv")) {
					BitrateUrl flvurl = populateBitRateUrls(n.getPath(), nName);
					enocflv.setType(MediaCmsConstants.MEDIA_ENOC_FLV);
					enocflv.getBitrateUrl().add(flvurl);
				}
				else if(nName.startsWith("original_") && nName.endsWith(".mp4")){
					
					BitrateUrl mp4url = populateBitRateUrls(n.getPath(), nName);
					enocmp4.setType(MediaCmsConstants.MEDIA_ENOC_MP4);
					enocmp4.getBitrateUrl().add(mp4url);
					}

if(!enoc.getBitrateUrl().isEmpty()){
				burls.getEncoding().add(enoc);
				}
				if(!enocmp4.getBitrateUrl().isEmpty()){
				burls.getEncoding().add(enocmp4);
				}


here is the way I am trying to generalize it....

Java Code:
Encoding enoc = new Encoding();

else if (nName.startsWith("original_")){
					BitrateUrl url = populateBitRateUrls(n.getPath(), nName);
					String extension = nName.substring(nName.lastIndexOf(".")+1, nName.length()).toLowerCase();
					
					enoc.setType(extension);
					enoc.getBitrateUrl().add(url);
					burls.getEncoding().add(enoc);
					
				}
the problem now it lists all the mp4 renditions under 1 file extension.... and if I initilaize the enoc variable inside elseif block, then it changes the JSON response.... by specifying file extension seperately...in JSON response..... instead of grouping them...like it shows in hardcoded way.....

So, How can I create objects at runtime and use the appropriate one for particular file extension..dynamically....
Please let me know if you need more explanation...