package org.msf.records.utils;

import android.text.format.DateFormat;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Gil on 03/10/2014.
 */
public class Utils {

    /**
     *  Convenience method to convert a byte array to a hex string.
     *
     * @param  data  the byte[] to convert
     * @return String the converted byte[]
     */

    public static String bytesToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            buf.append(byteToHex(data[i]).toUpperCase());
            buf.append(" ");
        }
        return (buf.toString());
    }

    /**
     *  method to convert a byte to a hex string.
     *
     * @param  data  the byte to convert
     * @return String the converted byte
     */
    public static String byteToHex(byte data) {
        StringBuffer buf = new StringBuffer();
        buf.append(toHexChar((data >>> 4) & 0x0F));
        buf.append(toHexChar(data & 0x0F));
        return buf.toString();
    }

    /**
     *  Convenience method to convert an int to a hex char.
     *
     * @param  i  the int to convert
     * @return char the converted char
     */
    public static char toHexChar(int i) {
        if ((0 <= i) && (i <= 9)) {
            return (char) ('0' + i);
        } else {
            return (char) ('a' + (i - 10));
        }
    }

    /**
     *  Convenience method to convert timestamp to date
     *
     * @param  timestamp  the long to convert
     * @return String the converted date
     */
    public static String timestampToDate(Long timestamp) {
        if (timestamp == null) {
            timestamp = 0L;
        }
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(timestamp * 1000);
        return DateFormat.format("dd-MM-yyyy", cal).toString();
    }

    /**
     *  Convenience method to calculate time difference between given timestamp and current timestamp
     *
     * @param  timestamp  the long to compare with current date
     * @return Period between the 2 dates
     */
    public static Period timeDifference(Long timestamp) {
        if (timestamp == null) {
            return Period.millis(0);
        }
        DateTime start = new DateTime(timestamp * 1000);
        DateTime currentDate = new DateTime();
        return new Period(start, currentDate, PeriodType.days());
    }

    /**
     * Encode a URL parameter, catching the useless exception that never happens.
     */
    public static String urlEncode(String s) {
        try {
            // Oh Java, how you make the simplest operation a waste of millions of programmer-hours.
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError("UTF-8 should be supported in every JVM");
        }
    }
}

@@ -296,36 +296,23 @@
 %0A * 
-Created by Gil on 03/10/2014
+Utility methods
 .%0A *
@@ -353,39 +353,17 @@
    *
- 
  Conve
-nience method to conve
 rt
+s
  a b
@@ -407,86 +407,67 @@
   * 
-@param  data  the byte%5B%5D to convert%0A     * @return String the converted
+%3Cp%3EThe resulting strings will have two characters per
  byte
-%5B%5D
+.
 %0A   
@@ -471,17 +471,16 @@
      */%0A
-%0A
     publ
@@ -671,16 +671,25 @@
 perCase(
+Locale.US
 ));%0A    
@@ -778,34 +778,24 @@
 %0A     * 
- method to c
+C
 onvert
+s
  a byte 
@@ -803,108 +803,33 @@
 o a 
-hex string.%0A     *%0A     * @param  data  the byte to convert%0A     * @return String the converted byte
+two-character hex string.
 %0A   
@@ -1080,144 +1080,60 @@
    *
- 
  Conve
-nience method to conve
 rt
+s
  an int
- to a hex char.%0A     *%0A     * @param  i  the int to convert%0A     * @return char the converted
+eger between 0 and 15 to a hex
  char
+.
 %0A   
@@ -1194,30 +1194,26 @@
  if 
-(
 (0 %3C= i
-)
  && 
-(
 i %3C= 9)
-)
  %7B%0A 
@@ -1344,151 +1344,47 @@
    *
- 
  Conve
-nience method to convert timestamp to date%0A     *%0A     * @param  timestamp  the long to convert%0A     * @return String the converted date
+rts a timestamp to a date string.
 %0A   
@@ -1718,38 +1718,16 @@
 *  C
-onvenience method to c
 alculate
  tim
@@ -1722,16 +1722,21 @@
 alculate
+s the
  time di
@@ -1789,16 +1789,17 @@
 imestamp
+.
 %0A     *%0A
@@ -2248,16 +2248,17 @@
 * Encode
+s
  a URL p
@@ -2684,11 +2684,71 @@
  %7D%0A    %7D
+%0A%0A    private Utils() %7B%0A    %09// Prevent instantiation.%0A    %7D
 %0A%7D%0A

Improve javadoc for Utils.java.