What's Wrong With You People?

The opinion of the author, presented as a factually objective and correct observation of humanity.

Monday, October 15, 2012

Java EnumSet jump table for high performance!

Sometimes you have code that you only want to run part of - maybe it's JAXB that you only want some XML filled out for. Maybe it's a return set you only want part of the result set for. Using EnumSets you can ask for criteria that have only what you want. If you have a fine grained web service, great, pass the tags, if you don't, fine just de-reference the tags you want. The great thing here is you can reuse this same code in multiple data layers in your application, saving time and processing at each layer!

for (IDiscount responseTO : discountsResponseTo.getAllOffers()) {
 Discount discount = obj.createDiscount();
 for(Tag x:tags) {//COOL!
  switch(x) {//COOL!
   case aaaDollars: discount.setAaaDollars(responseTO.getAAADollars()); break;
   case bestAvailRate: discount.setBestAvailRate(responseTO.getBestRate()); break;
   case caaDollars: discount.setCaaDollars(responseTO.getCAADollars()); break;
   case bogoFree: discount.setBogoFree(responseTO.getBOGOFree()); break;
   case couponBook: discount.setCouponBook(responseTO.getCouponBook()); beak;
   default: LOG.warn("Unhandled Tag: " + x); break;
  }
 }

package com.aaa.module.discounts.clubdeal.model;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Enum that enables high performance creation of return objects from the data
 * layer and also the construction of XML for the restful web service that only
 * contains the requested elements.
 * @author greg bishop
 */
public enum Tag {
 aaaDollars,
 bestAvailRate,
 bogoFree,
 bogoHalf,
 caaDollars, 
 categoryName,
 clubCode,
 couponBook, 
 cuisineType,
 dealDescription,
 dealRedemptionInfo, 
 dealRestriction,
 dealRestrictionUrl,
 detailsAvailable, 
 discountPercentage,
 discountPrice,
 discountsAvailable, 
 discountSavings,
 discountType, 
 discountUrl,
 discountValue,
 discPercent, 
 displayDiscountPercentage,
 displayDiscountSavings,
 displayPrice,
 displayTimeRemaining, 
 displayValue,
 dollarsSaved, 
 endDate,
 keywordRelevenceScore, 
 locations,
 loyaltyPoints, 
 merchantName,
 other, 
 phones,
 photoUrl,
 rebate, 
 shortDescription,
 smartUrl, 
 specialOffers,
 startDate,
 subCategoryName;
 
 private static final EnumSet NONE_INTERNAL;//Used for cloning
 private static final EnumSet ALL_INTERNAL;//Used for cloning
 public static final Set ALL;
 public static final Set ACTION_CENTER_WEEKLY_EXTRACT;
 
 private static final java.util.regex.Pattern commaPattern = Pattern.compile(",");
 
 static{
  NONE_INTERNAL = EnumSet.noneOf(Tag.class);
  ALL_INTERNAL = EnumSet.allOf(Tag.class);
  ALL = Collections.unmodifiableSet(ALL_INTERNAL);
  ACTION_CENTER_WEEKLY_EXTRACT = Collections.unmodifiableSet(
   getSet( categoryName, subCategoryName, merchantName, shortDescription, startDate, endDate, specialOffers, locations) );
 }

 /**
  * Creates a set of Tag objects from the users requested fields (case matters & spaces not allowed).
  * If a string is not found in a tag, that string is silently ignored. 
  * @param commaSeperatedListOfDesierdEnums
  * @return
  */
 public static Set getTags(final String commaSeperatedListOfDesierdEnums) {
  Set retval = NONE_INTERNAL.clone();//Faster
  //String[] data = commaSeperatedListOfDesierdEnums.split(",");
  String[] data = commaPattern.split(commaSeperatedListOfDesierdEnums,0);
  for (Tag value : ALL) {
   for (String str : data) {
    if (str.equalsIgnoreCase(value.name())) {
     retval.add(value);
     break;
    }
   }
  }
  return retval;
 }
 
 /**
  * Creates a tag set from a given list of tags.
  * @param values
  * @return
  */
 public static Set getSet(final Tag ... values){
  Set retval = NONE_INTERNAL.clone();
  for (Tag value : values) {
   retval.add(value);
  }
  return retval;
 }

} 

Labels: , , , , , , , , , , , , , ,

0 Comments:

Post a Comment

<< Home