
/*
 * @(#) Util.java 2.0 2000/07/24 Larry Rodrigues
 *
 */

package vis.util;

import java.lang.Math;

/** The Util class has a number of utility methods that are used in different beans.
  * @version 2.0  July 24 2000
  * @author Larry Rodrigues
  **/

 public class Util{
 /**
   * Computes  histogram.
   * @param data the input data array whose elements can be of any Java primitive types.
   * @param maxbars the maximum number of bars in the histogram.
   **/
static public HistoData computeHistogram(Object dataObj, int maxbars){

    if((dataObj == null) || (maxbars <=0)) {
       System.out.println("data = null");
       return null;
    }
    int yMinmax[] = new int[2];
    int binCount[] = new int[maxbars];
    int bin;
    Object binMinmax = getMinMax(dataObj);
    if(binMinmax == null) {
       System.out.println("bin minmax= null");
       return null;
    }
    int max =0, min= Integer.MAX_VALUE;
    HistoData histoData = new HistoData();
    if(dataObj instanceof int[]){
       int[] xMinmax1 = new int[2];
       xMinmax1[0] = ((int[])binMinmax)[0];
       xMinmax1[1] = ((int[])binMinmax)[1];
       int fullRange = xMinmax1[1] - xMinmax1[0]+1;
       int[] data = (int[])dataObj;
       for(int i=0;i<data.length;i++){
            bin =(int)(((double)(data[i] -xMinmax1[0])/fullRange)*maxbars);
	          binCount[bin]++;
       }
       min = data.length;
       histoData.setBinMinmax(xMinmax1);
    }
    if(dataObj instanceof double[]){
       double[] xMinmax2 = new double[2];
       xMinmax2[0] = ((double[])binMinmax)[0];
       xMinmax2[1] = ((double[])binMinmax)[1];
       double fullRange = xMinmax2[1] - xMinmax2[0]+1;
       double[] data = (double[])dataObj;
       for(int i=0;i<data.length;i++){
           bin =(int)(((double)(data[i] -xMinmax2[0])/fullRange)*maxbars);
           binCount[bin]++;
       }
       min = data.length;
       histoData.setBinMinmax(xMinmax2);
    }
    if(dataObj instanceof short[]){
       short[] xMinmax3 = new short[2];
       xMinmax3[0] = ((short[])binMinmax)[0];
       xMinmax3[1] = ((short[])binMinmax)[1];
       short fullRange = (short)(xMinmax3[1] - xMinmax3[0]+1);
       short[] data = (short[])dataObj;
       for(int i=0;i<data.length;i++){
           bin =(int)(((double)(data[i] -xMinmax3[0])/fullRange)*maxbars);
           binCount[bin]++;
       }
       min = data.length;
       histoData.setBinMinmax(xMinmax3);

    }
    if(dataObj instanceof long[]){
       long[] xMinmax4 = new long[2];
       xMinmax4[0] = ((long[])binMinmax)[0];
       xMinmax4[1] = ((long[])binMinmax)[1];
       long fullRange = xMinmax4[1] - xMinmax4[0]+1;
       long[] data = (long[])dataObj;
       for(int i=0;i<data.length;i++){
           bin =(int)(((double)(data[i] -xMinmax4[0])/fullRange)*maxbars);
           binCount[bin]++;
       }
       min = data.length;
       histoData.setBinMinmax(xMinmax4);
    }

    for(int i=0;i<maxbars;i++){
        if(binCount[i] > max) max = binCount[i];
        if(binCount[i] < min) min = binCount[i];
    }
    yMinmax[0] = min;
    yMinmax[1] = max;

    histoData.setFrequency(binCount);
    histoData.setFreqMinmax(yMinmax);

    return histoData;
}


 /**
   * Computes  histogram.
   * @param data the input data array.
   * @param maxbars the maximum number of bars in the histogram.
   * @param binCount the array of frequency count.
   * @param xMinMax an array of size 2 which holds minimum and maximum of X axis values.
   * @param yMinMax an array of size 2 which holds minimum and maximum of Y values.
   **/
 static  public void computeHistogram(int data[],
                                      int maxbars,
                                      int binCount[],
                                      int xMinmax[],
                                      int yMinmax[] ){


    int fullRange;
    Object binMinmax;
    int bin;

    if(data == null) {
       System.out.println("data = null");
       return;
    }
    binMinmax = getMinMax(data);
    if(binMinmax == null) {
       System.out.println("bin minmax= null");
       return;
    }
    xMinmax[0] = ((int[])binMinmax)[0];
    xMinmax[1] = ((int[])binMinmax)[1];

    fullRange = xMinmax[1] - xMinmax[0]+1;

    for(int i=0;i<data.length;i++){
        bin =(int)(((double)(data[i] -xMinmax[0])/fullRange)*maxbars);
	          binCount[bin]++;
    }
    int max =0, min  = data.length;
    for(int i=0;i<maxbars;i++){
        if(binCount[i] > max) max = binCount[i];
        if(binCount[i] < min) min = binCount[i];
	      }

    yMinmax[0] = min;
    yMinmax[1] = max;
 }

 /**
   * Computes minimun and maximum values of an array.
   * @param inp the input array as an Object.
   * @percenatge the percenatage of range by which the minimum value to be reduced.
   * @return the minmax array as an object. minmax[0] holds the
   * minimum value and minmax[1] holds the maximum value.
   **/
 static public Object getMinMax(Object inp, int percentage){
    if(inp instanceof int[]){
       int[] mm = getMinMax((int[])inp);
       mm[0] -= (int)((mm[1] -mm[0])*((double)percentage/100.0));
       return mm;
    }
    if(inp instanceof double[]){
       double[] mm = getMinMax((double[])inp);
       mm[0] -= (double)((mm[1] -mm[0])*(percentage/100));
       return mm;
    }

    if(inp instanceof short[]){
       short[] mm = getMinMax((short[])inp);
       mm[0] -= (short)((mm[1] -mm[0])*(percentage/100));
       return mm;
    }

    if(inp instanceof long[]){
       long[] mm = getMinMax((long[])inp);
       mm[0] -= (long)((mm[1] -mm[0])*(percentage/100));
       return mm;
    }
    return null;
 }


 /**
   * Computes minimun and maximum values of an array.
   * @param inp the input array as an Object.
   * @return the minmax array as an Object. minmax[0] holds the
   * minimum value and minmax[1] holds the maximum value.
   **/
 static public Object getMinMax(Object inp){
    if(inp instanceof int[]){
       return getMinMax((int[])inp);
    }
    if(inp instanceof double[]){
       return getMinMax((double[])inp);
    }

    if(inp instanceof short[]){
       return getMinMax((short[])inp);
    }

    if(inp instanceof long[]){
       return getMinMax((long[])inp);
    }
    return null;
  }

 static public Object getMinMax(Object inparray[]){
    if(inparray == null) return null;
    int len = inparray.length;
    Object inp = inparray[0];
    if(inp instanceof int[]){
       int minmax[] = new int[2];
       minmax[0] = Integer.MAX_VALUE;
       minmax[1]=  Integer.MIN_VALUE;
       for(int i=0;i<len;i++) {
          int[] mm = getMinMax((int[])inp);
          if(minmax[0] > mm[0]) minmax[0] = mm[0];
          if(minmax[1] < mm[1]) minmax[1] = mm[1];
       }
       return minmax;
    }
    if(inp instanceof double[]){
       double minmax[] = new double[2];
       minmax[0] = Double.MAX_VALUE;
       minmax[1]=  Double.MIN_VALUE;
       for(int i=0;i<len;i++) {
          double[] mm = getMinMax((double[])inp);
          if(minmax[0] > mm[0]) minmax[0] = mm[0];
          if(minmax[1] < mm[1]) minmax[1] = mm[1];
       }
       return minmax;
    }

    if(inp instanceof short[]){
       short minmax[] = new short[2];
       minmax[0] = Short.MAX_VALUE;
       minmax[1]=  Short.MIN_VALUE;
       for(int i=0;i<len;i++) {
          short[] mm = getMinMax((short[])inp);
          if(minmax[0] > mm[0]) minmax[0] = mm[0];
          if(minmax[1] < mm[1]) minmax[1] = mm[1];
       }
       return minmax;
    }

    if(inp instanceof long[]){
       long minmax[] = new long[2];
       minmax[0] = Long.MAX_VALUE;
       minmax[1]=  Long.MIN_VALUE;
       for(int i=0;i<len;i++) {
          long[] mm = getMinMax((long[])inp);
          if(minmax[0] > mm[0]) minmax[0] = mm[0];
          if(minmax[1] < mm[1]) minmax[1] = mm[1];
       }
       return minmax;
    }
    return null;
  }

 /**
   * Computes histogram.
   * @param data the input data array as an Object.
   * @param maxbars the maximum number of bars.
   * @param binCount an array of bin count.
   * @param xMinmax an array of size 2 which holds the
   * minimum and maximum of X axis values.
   * @param yMinmax an array of size 2 which holds the
   * minimum and maximum of Y axis values.
   **/

   static  public void computeHistogram(double data[],
                                      int maxbars,
                                      int binCount[],
                                      double[] xMinmax,
                                      int yMinmax[] ){

      double[] binMinmax= new double[2];
      double fullRange;
      int bin;
      binMinmax = getMinMax(data);
      xMinmax[0] = binMinmax[0];
      xMinmax[1] = binMinmax[1];
      fullRange = xMinmax[1] - xMinmax[0]+1;

      for(int i=0;i<data.length;i++){
          bin =(int)(((double)(data[i] -xMinmax[0])/fullRange)*maxbars);
	          binCount[bin]++;
      }
      int max =0, min  = data.length;
      for(int i=0;i<maxbars;i++){
          if(binCount[i] > max) max = binCount[i];
          if(binCount[i] < min) min = binCount[i];
	      }

      yMinmax[0] = min;
      yMinmax[1] = max;

   }

 /**
   * Computes minimun and maximum of the values in a given array.
   * @param inp the input array.
   * @return the minmax array. minmax[0] holds the
   * minimum value and minmax[1] holds the maximum value.
   **/
   static public int[] getMinMax(int inp[]){
     int minmax[] = new int[2];
      minmax[0] = Integer.MAX_VALUE;
      minmax[1]=  Integer.MIN_VALUE;
      for(int i=0;i<inp.length;i++){
          if(inp[i] > minmax[1]) minmax[1] = inp[i];
          if(inp[i] <minmax[0]) minmax[0] = inp[i];
      }
      return minmax;
    }

   /**
   * Computes minimun and maximum of the values in an array.
   * @param inp the input array.
   * @return the minmax array. minmax[0] holds the
   * minimum value and minmax[1] holds the maximum value.
   **/
   static public double[] getMinMax(double inp[]){
      double minmax[] = new double[2];
      minmax[0] = Double.MAX_VALUE;
      minmax[1]=  Double.MIN_VALUE;
      for(int i=0;i<inp.length;i++){
          if(inp[i] > minmax[1]) minmax[1] = inp[i];
          if(inp[i] <minmax[0]) minmax[0] = inp[i];
      }

      return minmax;

   }

   /**
     * Computes minimum and maximum values of an array.
     * @param inp a one dimensional array.
     * @return the minmax array. minmax[0] holds the
     * minimum value and minmax[1] holds the maximum value.
     */
   static public short[] getMinMax(short inp[]){
        short minmax[] = new short[2];
        minmax[0] = Short.MAX_VALUE;
        minmax[1]=  Short.MIN_VALUE;
        for(int i=0;i<inp.length;i++){
            if(inp[i] > minmax[1]) minmax[1] = inp[i];
            if(inp[i] <minmax[0]) minmax[0] = inp[i];
        }

        return minmax;
    }

   /**
     * Computes minimum and maximum values of an array.
     * @param inp a one dimensional array.
     * @return the minmax array. minmax[0] holds the
     * minimum value and minmax[1] holds the maximum value.
     */
    static public long[] getMinMax(long inp[]){
        long minmax[] = new long[2];
        minmax[0] = Long.MAX_VALUE;
        minmax[1]=  Long.MIN_VALUE;
        for(int i=0;i<inp.length;i++){
            if(inp[i] > minmax[1]) minmax[1] = inp[i];
            if(inp[i] <minmax[0]) minmax[0] = inp[i];
        }

        return minmax;
    }


    /**
      * Converts RGB color values to its corresponding intensity values.
      * @param pixValue an array of color pixel values.
      * @return an array of intensity values.
      */
    static public int[] colorToIntensity(int pixValue[]){

           if(pixValue == null) return null;
           int grayVal[] = new int[pixValue.length];
            for(int i=0;i < pixValue.length;i++){
               grayVal[i] = (int)(((pixValue[i] & 0xff0000) >> 16) * (0.212671) +
                        ((pixValue[i] & 0x00ff00) >> 8) * (0.715160) +
                        (pixValue[i] & 0x0000ff) * (0.071169));

               }

           return grayVal;


   }

   /**
     * Converts an one dimensional array of numbers into an
     * array of corresponding string values.
     * @param obj an array of Java primitive type as an Object.
     * @return an array of strings.
     */
   public static String[] toString(Object obj){
      if(obj == null) return null;
      int len;
      String outStr[];
      if(obj instanceof int[]) {
         len = ((int[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Integer.toString(((int[])obj)[i]);
         }
         return outStr;
      }
      if(obj instanceof double[]) {
         len = ((double[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Double.toString(((double[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof short[]) {
         len = ((short[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Short.toString(((short[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof long[]) {
         len = ((long[])obj).length;
         outStr = new String[len];
         for(int i=0;i<len;i++){
             outStr[i]= Long.toString(((long[])obj)[i]);
         }
         return outStr;
      }

      if(obj instanceof String[]) return (String[])obj;
      return null;
   }

   /**
     * Returns the maximum value as a string from a minmax array.
     * @param obj an array of size 2 representing the min and mx values.
     * @return the maximum value as a string.
     */
   public static String maxString(Object obj){
      if(obj == null) return null;
      if(obj instanceof int[]) return Integer.toString(((int[])obj)[1]);
      if(obj instanceof double[]) return Double.toString(((double[])obj)[1]);
      if(obj instanceof short[]) return Short.toString(((short[])obj)[1]);
      if(obj instanceof long[]) return Long.toString(((long[])obj)[1]);
      return null;
   }

   /**
     * Computes the absolute inbetween value and returns it as a string.
     * @param obj the minmax array i.e.; an array of size 2 in which minmax[0]
     * is the maximum value and minmax[1] as the minimum value.
     * @param val the inbetween value relative to the minimum value.
     * @return the absolute inbetween value as a string.
     */
   public static String getInBetString(Object obj, double val){
      if(obj == null) return null;
      if(obj instanceof int[]) return Integer.toString(((int[])obj)[0]+(int)val);
      if(obj instanceof double[]) {
         double db =  ((double[])obj)[0]+val;
         return Double.toString(trunc(db));
       }
      //if(obj instanceof short[]) return Short.toString(((short[])obj)[0]+
      //                               (new Short(val)).shortValue());
      if(obj instanceof long[]) return Long.toString(((long[])obj)[0]+(long)val);
      return null;
   }

   /**
     * Computes the range (maximum - minimum) from a minmax array which can be generic.
     * @param obj a one dimensional array of size 2 of Java primitive types as an Object.
     * @return the range as a double value.
     */
   public static double getRange(Object obj){
      if(obj == null) return 1.0;
      if(obj instanceof int[]) return ((int[])obj)[1]- ((int[])obj)[0];
      if(obj instanceof double[]) return (((double[])obj)[1] - ((double[])obj)[0]);
      if(obj instanceof short[]) return (((short[])obj)[1] -((short[])obj)[0]);
      if(obj instanceof long[]) return (((long[])obj)[1]- ((long[])obj)[0]);
      return 1.0 ;
   }

   /**
     * Truncates a double value to the 2nd decimal precison.
     * @param db the double value.
     * @return the truncated double value.
     */
   public static double trunc(double db){
      return  ((double)(Math.round(db*100)))/100.0;
   }

   /**
     * Gets the size of a generic array.
     * @param obj an array of Java primitive types.
     * @return the length of the array.
     */
   public static int getLength(Object obj){
      if(obj == null) return 0;
      if(obj instanceof int[]) return ((int[])obj).length;
      if(obj instanceof double[]) return ((double[])obj).length;
      if(obj instanceof float[]) return ((float[])obj).length;

      if(obj instanceof short[]) return ((short[])obj).length;
      if(obj instanceof long[]) return ((long[])obj).length;
      return 0 ;
   }

   /**
     *  Gets the dimesions of a generic 2 dimensional array.
     *  @param obj the input array which can be of any primitive type.
     *  @return an array representing the input array dimensions.
     */
   public static int[] getArrayDimension(Object obj){
      if(obj == null) return null;
      int[] len = new int[2];
      if(obj instanceof int[][]){
         int[][] intar= (int[][])obj;
         len[0] = intar.length;
         len[1] = intar[0].length;
         return len;
      }
      if(obj instanceof double[]){
         double[][] doublear= (double[][])obj;
         len[0] = doublear.length;
         len[1] = doublear[0].length;
         return len;
      }
      if(obj instanceof float[]){
         float[][] floatar= (float[][])obj;
         len[0] = floatar.length;
         len[1] = floatar[0].length;
         return len;
      }

      if(obj instanceof short[]) {
         short[][] shortar= (short[][])obj;
         len[0] = shortar.length;
         len[1] = shortar[0].length;
         return len;
      }
      if(obj instanceof long[]) {
         long[][] longar= (long[][])obj;
         len[0] = longar.length;
         len[1] = longar[0].length;
         return len;
      }
      return null ;
   }

  //---------------------- 2D conversions----------------------------------
   /**
     * Converts a 2 dimensional array of any type to a 2 dimensional array of
     * of double.
     * @param obj a 2 dimensional array as an Object.
     * @return a 2 dimensional array of double.
     */
   public static double[][] convertToDouble(Object obj){
      if(obj == null) return null;
      double[][] dbdata =null;

      if(obj instanceof String[][]){
         String[][] stringar= (String[][])obj;
         int row = stringar.length;
         int col = stringar[0].length;
         for(int i=0; i<row;i++){
             try{
                for(int j=0; j<col;j++){
                    dbdata[i][j] = (Double.valueOf(stringar[i][j])).intValue();
                }
              } catch(Exception e){ }
         }
         return dbdata;
      }

      if(obj instanceof int[][]){
         int[][] intar= (int[][])obj;
         int row = intar.length;
         int col = intar[0].length;
         for(int i=0; i<row;i++){
             for(int j=0; j<col;j++){
                  dbdata[i][j] = (double)intar[i][j];
             }
         }
         return dbdata;
      }
      if(obj instanceof double[][]){
         return   (double[][])obj;
      }
      if(obj instanceof float[][]){
         return   (double[][])obj;
      }

      if(obj instanceof short[][]){
         short[][] shortar= (short[][])obj;
         int row = shortar.length;
         int col = shortar[0].length;

         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbdata[i][j] = (double)shortar[i][j];
             }
         }
         return dbdata;
      }

      if(obj instanceof long[][]){
         long[][] longar= (long[][])obj;
         int row = longar.length;
         int col = longar[0].length;


         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbdata[i][j] = (double)longar[i][j];
             }
         }
         return dbdata;
      }
      return null;
    }

   /**
     * Converts a 2 dimensional array object of any type to a
     * 2 dimensional string array.
     * @param obj a 2 dimensional array as an Object.
     * @return a 2 dimensional array of strings.
     */
   public static String[][] convertToString(Object obj){
      if(obj == null) return null;
      String[][] dbstr=null;
      if(obj instanceof int[][]){
         int[][] intar= (int[][])obj;
         int row = intar.length;
         int col = intar[0].length;
         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbstr[i][j] = Integer.toString(intar[i][j]);
              }
         }
         return dbstr;
      }
      if(obj instanceof double[][]){
         double[][] doublear= (double[][])obj;
         int row = doublear.length;
         int col = doublear[0].length;
         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbstr[i][j] = Double.toString(doublear[i][j]);
              }
         }
         return dbstr;
      }
      if(obj instanceof float[][]){
         float[][] floatar= (float[][])obj;
         int row = floatar.length;
         int col = floatar[0].length;
         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbstr[i][j] = Float.toString(floatar[i][j]);
              }
         }
         return dbstr;
      }

      if(obj instanceof short[][]){
         short[][] shortar= (short[][])obj;
         int row = shortar.length;
         int col = shortar[0].length;

         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbstr[i][j] = Short.toString(shortar[i][j]);
             }
         }
         return dbstr;
      }

      if(obj instanceof long[][]){
         long[][] longar= (long[][])obj;
         int row = longar.length;
         int col = longar[0].length;


         for(int i=0; i<row;i++){
              for(int j=0; j<col;j++){
                  dbstr[i][j] = Long.toString(longar[i][j]);
             }
         }
         return dbstr;
      }
      return null;
    }

   /**
     * Computes the difference between the value at a given index of the
     * input array and the minimum value, i.e.;
     *   diff = (inp[index] - min)*scale.
     * This difference returned is a scaled value.
     * This method is useful in plotting applications in which there is a
     * need to compute the actual distance at every tick from the origin.
     * @param obj a one dimensional array of any primitive data type.
     * @param minmax an array of size 2 that represents the minimum and maximum
     * values.
     * @param index the index in the input array.
     * @param scale the scaling factor.
     * @return the scaled difference from the minimum value.
     */
    public static int getDiff(Object obj,
                             Object minmax,
                             int index,
                             double scale){
      if((obj == null)||(minmax == null)||(index<0) || (scale <= 0.0)) return 0;
      if(obj instanceof int[]) {
         return (int)((((int[])obj)[index]- ((int[])minmax)[0])*scale);
      }
      if(obj instanceof double[]) {
         return (int)((((double[])obj)[index]- ((double[])minmax)[0])*scale);
      }
      if(obj instanceof short[]) {
        return (int)((((short[])obj)[index]- ((short[])minmax)[0])*scale);
      }
      if(obj instanceof long[]){
         return (int)((((long[])obj)[index]- ((long[])minmax)[0])*scale);
      }
      return 0 ;
   }

   /**
     *  Converts an input array into an array of angles. This is used in
     *  for computing pie angles in pie charts.
     *  @param obj a one dimensional array of Java primitive data types.
     *  @return an array of angles.
     */
   public static int[] getDegrees(Object obj){
      if(obj == null) return null;
      int degree[];
      if(obj instanceof int[]) {
         int len = ((int[])obj).length;
         degree = new int[len];
         int sum =0;
         for (int i=0;i<len;i++){
              sum += ((int[])obj)[i];
         }
         for(int i=0;i<len;i++){
            degree[i] =(int)(((double)(((int[])obj)[i])*360)/(double)sum);
         }
         sum =0;
         for (int i=0;i<len;i++){
              sum += degree[i];
         }
         int diff = 360-sum;
         if(diff >0){
            for(int i=0;i<diff;i++){
                degree[i%len]++;
            }

            degree[degree.length-1] +=diff;
         }
         return degree;
      }
      if(obj instanceof double[]) {
         int len = ((double[])obj).length;
         degree = new int[len];
         double sum =0;
         for (int i=0;i<len;i++){
              sum += ((double[])obj)[i];
         }
         for(int i=0;i<len;i++){
            degree[i] =(int)((((double[])obj)[i]*360)/(double)sum);
         }

         int sum1 =0;
         for (int i=0;i<len;i++){
              sum1 += degree[i];
         }
         int diff = 360-sum1;
         if(diff >0){
            for(int i=0;i<diff;i++){
                degree[i%len]++;
            }
            degree[degree.length-1] +=diff;
         }
         return degree;

      }
      if(obj instanceof short[]) {
         int len = ((short[])obj).length;
         degree = new int[len];
         short sum =0;
         for (int i=0;i<len;i++){
              sum += ((short[])obj)[i];
         }
         for(int i=0;i<len;i++){
            degree[i] =(int)((((double[])obj)[i]*360)/(double)sum);
         }

         int sum1 =0;
         for (int i=0;i<len;i++){
              sum1 += degree[i];
         }
         int diff = 360-sum1;
         if(diff >0){
            for(int i=0;i<diff;i++){
                degree[i%len]++;
            }
            degree[degree.length-1] +=diff;
         }
         return degree;

      }
      if(obj instanceof long[]){
         int len = ((short[])obj).length;
         degree = new int[len];
         long sum =0;
         for (int i=0;i<len;i++){
              sum += ((long[])obj)[i];
         }
         for(int i=0;i<len;i++){
            degree[i] =(int)((((double[])obj)[i]*360)/(double)sum);
         }

         int sum1 =0;
         for (int i=0;i<len;i++){
              sum1 += degree[i];
         }
         int diff = 360-sum1;
         if(diff >0){
            for(int i=0;i<diff;i++){
                degree[i%len]++;
            }
            degree[degree.length-1] +=diff;
         }
         return degree;
      }
      return null;
   }

 }
