bobik
Class BobikHelper

java.lang.Object
  extended by bobik.BobikHelper

public class BobikHelper
extends java.lang.Object

A collection of recurring and useful utility functions

Author:
Eugene Mirkin

Constructor Summary
BobikHelper()
           
 
Method Summary
static java.util.List<JSONObject> transpose(JSONObject x)
          Takes a hash of parallel arrays and turns it into a list of hashes, each having all keys from the original hash.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BobikHelper

public BobikHelper()
Method Detail

transpose

public static java.util.List<JSONObject> transpose(JSONObject x)
                                            throws java.lang.ArrayIndexOutOfBoundsException,
                                                   JSONException
Takes a hash of parallel arrays and turns it into a list of hashes, each having all keys from the original hash.

Example:
 {'title':['A', 'B', 'C'], 'price':[4,2,5]}
      becomes
        [{'price':4, 'title':'A'} , {'price':2, 'title':'B'}, {'price':5, 'title':'C'}]
 
 
If the incoming arrays have different length, then a smart selector logic applies. Empty arrays are ignored. Non-empty arrays must all have the same length. If they don't, then an exception is thrown.

Parameters:
x - a hash of parallel arrays
Returns:
a transposed array
Throws:
java.lang.ArrayIndexOutOfBoundsException - if arrays are not of equal length
JSONException - if the incoming object is not of the expected format