org.linkedin.util-core/src/main/java/org/linkedin/util/lang/MemorySize.java
public String getAsString(EnumSet<SizeUnit> sizeUnits)
  {
    if(sizeUnits == null || sizeUnits.size() == 0)
      return toString();
    
    StringBuilder sb = new StringBuilder();
    MemorySize ms = this;
    MemorySize prevMs;

    // IMPORTANT: 
    // according to javadoc for EnumSet:
    // "The iterator returned by the iteratormethod traverses the elements in their natural order 
    // (the order in which the enum constants are declared)" and the toArray() method of AbstractCollection
    // guarantees such an order
    Object[] unitsAsArr = sizeUnits.toArray();

    int len = unitsAsArr.length;
    for (int i = len - 1; i > -1; i--)
    {
      prevMs = ms;
      ms = ms.truncate((SizeUnit)unitsAsArr[i]);
      if (ms.getSize() > 0)
      {
        sb.append(ms.getSize()).append(ms.getSizeUnit().getDisplayChar());
      }
      ms = prevMs.subtract(ms);
    }

    if(sb.length() == 0)
    {
      SizeUnit smallestUnit = (SizeUnit)unitsAsArr[0];
      sb.append("0").append(smallestUnit.getDisplayChar());
    }
      
    return sb.toString();
  }
public String getAsString(EnumSet<SizeUnit> sizeUnits)
  {
    if(sizeUnits == null || sizeUnits.size() == 0)
      return toString();
    
    StringBuilder sb = new StringBuilder();
    MemorySize ms = this;
    MemorySize prevMs;

    // IMPORTANT: 
    // according to javadoc for EnumSet:
    // "The iterator returned by the iteratormethod traverses the elements in their natural order 
    // (the order in which the enum constants are declared)" and the toArray() method of AbstractCollection
    // guarantees such an order
    Object[] unitsAsArr = sizeUnits.toArray();

    int len = unitsAsArr.length;
    for (int i = len - 1; i > -1; i--)
    {
      prevMs = ms;
      ms = ms.truncate((SizeUnit)unitsAsArr[i]);
      if (ms.getSize() > 0)
      {
        sb.append(ms.getSize()).append(ms.getSizeUnit().getDisplayChar());
      }
      ms = prevMs.subtract(ms);
    }

    if(sb.length() == 0)
    {
      SizeUnit smallestUnit = (SizeUnit)unitsAsArr[0];
      sb.append("0").append(smallestUnit.getDisplayChar());
    }
      
    return sb.toString();
  }
Return a string representation of this instance using the given units
E.g. 2g3m4k given units g/m -> "2g3m"
2g3m4k given units g/k -> "2g3076k"
3m4k given units t/g -> "0g"
2g3m given units k/b -> "2100224k"

@param sizeUnits the sizeUnits you want in the decomposition
@return a string representation of this instance using the given units.