MtasDataDoubleBasic.java

  1. package mtas.codec.util.collector;

  2. import java.io.IOException;
  3. import java.util.Collections;
  4. import java.util.SortedSet;

  5. import org.apache.commons.lang.ArrayUtils;
  6. import mtas.codec.util.CodecUtil;

  7. /**
  8.  * The Class MtasDataDoubleBasic.
  9.  */
  10. public class MtasDataDoubleBasic extends MtasDataBasic<Double, Double> {

  11.   /** The Constant serialVersionUID. */
  12.   private static final long serialVersionUID = 1L;

  13.   /**
  14.    * Instantiates a new mtas data double basic.
  15.    *
  16.    * @param collectorType the collector type
  17.    * @param statsItems the stats items
  18.    * @param sortType the sort type
  19.    * @param sortDirection the sort direction
  20.    * @param start the start
  21.    * @param number the number
  22.    * @param subCollectorTypes the sub collector types
  23.    * @param subDataTypes the sub data types
  24.    * @param subStatsTypes the sub stats types
  25.    * @param subStatsItems the sub stats items
  26.    * @param subSortTypes the sub sort types
  27.    * @param subSortDirections the sub sort directions
  28.    * @param subStart the sub start
  29.    * @param subNumber the sub number
  30.    * @param segmentRegistration the segment registration
  31.    * @param boundary the boundary
  32.    * @throws IOException Signals that an I/O exception has occurred.
  33.    */
  34.   public MtasDataDoubleBasic(String collectorType, SortedSet<String> statsItems,
  35.       String sortType, String sortDirection, Integer start, Integer number,
  36.       String[] subCollectorTypes, String[] subDataTypes, String[] subStatsTypes,
  37.       SortedSet<String>[] subStatsItems, String[] subSortTypes,
  38.       String[] subSortDirections, Integer[] subStart, Integer[] subNumber,
  39.       String segmentRegistration, String boundary) throws IOException {
  40.     super(collectorType, CodecUtil.DATA_TYPE_DOUBLE, statsItems, sortType,
  41.         sortDirection, start, number, subCollectorTypes, subDataTypes,
  42.         subStatsTypes, subStatsItems, subSortTypes, subSortDirections, subStart,
  43.         subNumber, new MtasDataDoubleOperations(), segmentRegistration,
  44.         boundary);
  45.   }

  46.   /*
  47.    * (non-Javadoc)
  48.    *
  49.    * @see mtas.codec.util.DataCollector.MtasDataCollector#getItem(int)
  50.    */
  51.   @Override
  52.   protected MtasDataItemDoubleBasic getItem(int i) {
  53.     if (i >= 0 && i < size) {
  54.       return new MtasDataItemDoubleBasic(basicValueSumList[i],
  55.           basicValueNList[i], hasSub() ? subCollectorListNextLevel[i] : null,
  56.           getStatsItems(), sortType, sortDirection, errorNumber[i],
  57.           errorList[i], sourceNumberList[i]);
  58.     } else {
  59.       return null;
  60.     }
  61.   }

  62.   /*
  63.    * (non-Javadoc)
  64.    *
  65.    * @see mtas.codec.util.DataCollector.MtasDataCollector#add(long, long)
  66.    */
  67.   @Override
  68.   public MtasDataCollector<?, ?> add(long valueSum, long valueN)
  69.       throws IOException {
  70.     MtasDataCollector<?, ?> dataCollector = add(false);
  71.     setValue(newCurrentPosition, Double.valueOf(valueSum), valueN,
  72.         newCurrentExisting);
  73.     return dataCollector;
  74.   }

  75.   /*
  76.    * (non-Javadoc)
  77.    *
  78.    * @see mtas.codec.util.DataCollector.MtasDataCollector#add(long[], int)
  79.    */
  80.   @Override
  81.   public MtasDataCollector<?, ?> add(long[] values, int number)
  82.       throws IOException {
  83.     MtasDataCollector<?, ?> dataCollector = add(false);
  84.     Double[] newValues = new Double[number];
  85.     for (int i = 0; i < values.length; i++)
  86.       newValues[i] = Long.valueOf(values[i]).doubleValue();
  87.     setValue(newCurrentPosition, newValues, number, newCurrentExisting);
  88.     return dataCollector;
  89.   }

  90.   /*
  91.    * (non-Javadoc)
  92.    *
  93.    * @see mtas.codec.util.DataCollector.MtasDataCollector#add(double, long)
  94.    */
  95.   @Override
  96.   public MtasDataCollector<?, ?> add(double valueSum, long valueN)
  97.       throws IOException {
  98.     MtasDataCollector<?, ?> dataCollector = add(false);
  99.     setValue(newCurrentPosition, valueSum, valueN, newCurrentExisting);
  100.     return dataCollector;
  101.   }

  102.   /*
  103.    * (non-Javadoc)
  104.    *
  105.    * @see mtas.codec.util.DataCollector.MtasDataCollector#add(double[], int)
  106.    */
  107.   @Override
  108.   public MtasDataCollector<?, ?> add(double[] values, int number)
  109.       throws IOException {
  110.     MtasDataCollector<?, ?> dataCollector = add(false);
  111.     setValue(newCurrentPosition, ArrayUtils.toObject(values), number,
  112.         newCurrentExisting);
  113.     return dataCollector;
  114.   }

  115.   /*
  116.    * (non-Javadoc)
  117.    *
  118.    * @see
  119.    * mtas.codec.util.DataCollector.MtasDataCollector#add(java.lang.String[],
  120.    * long, long)
  121.    */
  122.   @Override
  123.   public MtasDataCollector<?, ?> add(String key, long valueSum, long valueN)
  124.       throws IOException {
  125.     if (key != null) {
  126.       MtasDataCollector<?, ?> subCollector = add(key, false);
  127.       setValue(newCurrentPosition, Double.valueOf(valueSum), valueN,
  128.           newCurrentExisting);
  129.       return subCollector;
  130.     } else {
  131.       return null;
  132.     }
  133.   }

  134.   /*
  135.    * (non-Javadoc)
  136.    *
  137.    * @see
  138.    * mtas.codec.util.DataCollector.MtasDataCollector#add(java.lang.String[],
  139.    * long[], int)
  140.    */
  141.   @Override
  142.   public MtasDataCollector<?, ?> add(String key, long[] values, int number)
  143.       throws IOException {
  144.     if (key != null) {
  145.       Double[] newValues = new Double[number];
  146.       for (int i = 0; i < values.length; i++)
  147.         newValues[i] = Long.valueOf(values[i]).doubleValue();
  148.       MtasDataCollector<?, ?> subCollector = add(key, false);
  149.       setValue(newCurrentPosition, newValues, number, newCurrentExisting);
  150.       return subCollector;
  151.     } else {
  152.       return null;
  153.     }
  154.   }

  155.   /*
  156.    * (non-Javadoc)
  157.    *
  158.    * @see
  159.    * mtas.codec.util.DataCollector.MtasDataCollector#add(java.lang.String[],
  160.    * double, long)
  161.    */
  162.   @Override
  163.   public MtasDataCollector<?, ?> add(String key, double valueSum, long valueN)
  164.       throws IOException {
  165.     if (key != null) {
  166.       MtasDataCollector<?, ?> subCollector = add(key, false);
  167.       setValue(newCurrentPosition, valueSum, valueN, newCurrentExisting);
  168.       return subCollector;
  169.     } else {
  170.       return null;
  171.     }
  172.   }

  173.   /*
  174.    * (non-Javadoc)
  175.    *
  176.    * @see
  177.    * mtas.codec.util.DataCollector.MtasDataCollector#add(java.lang.String[],
  178.    * double[], int)
  179.    */
  180.   @Override
  181.   public MtasDataCollector<?, ?> add(String key, double[] values, int number)
  182.       throws IOException {
  183.     if (key != null) {
  184.       MtasDataCollector<?, ?> subCollector = add(key, false);
  185.       setValue(newCurrentPosition, ArrayUtils.toObject(values), number,
  186.           newCurrentExisting);
  187.       return subCollector;
  188.     } else {
  189.       return null;
  190.     }
  191.   }

  192.   /*
  193.    * (non-Javadoc)
  194.    *
  195.    * @see
  196.    * mtas.codec.util.DataCollector.MtasDataCollector#compareForComputingSegment(
  197.    * java.lang.Number, java.lang.Number)
  198.    */
  199.   @Override
  200.   protected boolean compareWithBoundary(Double value, Double boundary)
  201.       throws IOException {
  202.     if (segmentRegistration.equals(SEGMENT_SORT_ASC)
  203.         || segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)) {
  204.       return value <= boundary;
  205.     } else if (segmentRegistration.equals(SEGMENT_SORT_DESC)
  206.         || segmentRegistration.equals(SEGMENT_BOUNDARY_DESC)) {
  207.       return value >= boundary;
  208.     } else {
  209.       throw new IOException(
  210.           "can't compare for segmentRegistration " + segmentRegistration);
  211.     }
  212.   }

  213.   /*
  214.    * (non-Javadoc)
  215.    *
  216.    * @see
  217.    * mtas.codec.util.DataCollector.MtasDataCollector#minimumForComputingSegment(
  218.    * java.lang.Number, java.lang.Number)
  219.    */
  220.   @Override
  221.   protected Double lastForComputingSegment(Double value, Double boundary)
  222.       throws IOException {
  223.     if (segmentRegistration.equals(SEGMENT_SORT_ASC)) {
  224.       return Math.max(value, boundary);
  225.     } else if (segmentRegistration.equals(SEGMENT_SORT_DESC)) {
  226.       return Math.min(value, boundary);
  227.     } else {
  228.       throw new IOException(
  229.           "can't compute last for segmentRegistration " + segmentRegistration);
  230.     }
  231.   }

  232.   /*
  233.    * (non-Javadoc)
  234.    *
  235.    * @see
  236.    * mtas.codec.util.DataCollector.MtasDataCollector#minimumForComputingSegment(
  237.    * )
  238.    */
  239.   @Override
  240.   protected Double lastForComputingSegment() throws IOException {
  241.     if (segmentRegistration.equals(SEGMENT_SORT_ASC)
  242.         || segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)) {
  243.       return Collections.max(segmentValueTopList);
  244.     } else if (segmentRegistration.equals(SEGMENT_SORT_DESC)
  245.         || segmentRegistration.equals(SEGMENT_BOUNDARY_DESC)) {
  246.       return Collections.min(segmentValueTopList);
  247.     } else {
  248.       throw new IOException(
  249.           "can't compute last for segmentRegistration " + segmentRegistration);
  250.     }
  251.   }

  252.   /*
  253.    * (non-Javadoc)
  254.    *
  255.    * @see
  256.    * mtas.codec.util.DataCollector.MtasDataCollector#boundaryForComputingSegment
  257.    * ()
  258.    */
  259.   @Override
  260.   protected Double boundaryForSegmentComputing(String segmentName)
  261.       throws IOException {
  262.     if (segmentRegistration.equals(SEGMENT_SORT_ASC)
  263.         || segmentRegistration.equals(SEGMENT_SORT_DESC)) {
  264.       Double boundary = boundaryForSegment(segmentName);
  265.       if (boundary == null) {
  266.         return null;
  267.       } else {
  268.         if (segmentRegistration.equals(SEGMENT_SORT_DESC)) {
  269.           long correctionBoundary = 0;
  270.           for (String otherSegmentName : segmentValueTopListLast.keySet()) {
  271.             if (!otherSegmentName.equals(segmentName)) {
  272.               Double otherBoundary = segmentValuesBoundary
  273.                   .get(otherSegmentName);
  274.               if (otherBoundary != null) {
  275.                 correctionBoundary += Math.max(0, otherBoundary - boundary);
  276.               }
  277.             }
  278.           }
  279.           return boundary + correctionBoundary;
  280.         } else {
  281.           return boundary;
  282.         }
  283.       }
  284.     } else {
  285.       throw new IOException("can't compute boundary for segmentRegistration "
  286.           + segmentRegistration);
  287.     }
  288.   }

  289.   /*
  290.    * (non-Javadoc)
  291.    *
  292.    * @see mtas.codec.util.DataCollector.MtasDataCollector#boundaryForSegment()
  293.    */
  294.   @Override
  295.   protected Double boundaryForSegment(String segmentName) throws IOException {
  296.     if (segmentRegistration.equals(SEGMENT_SORT_ASC)
  297.         || segmentRegistration.equals(SEGMENT_SORT_DESC)) {
  298.       Double thisLast = segmentValueTopListLast.get(segmentName);
  299.       if (thisLast == null) {
  300.         return null;
  301.       } else if (segmentRegistration.equals(SEGMENT_SORT_ASC)) {
  302.         return thisLast * segmentNumber;
  303.       } else {
  304.         return thisLast / segmentNumber;
  305.       }
  306.     } else {
  307.       throw new IOException("can't compute boundary for segmentRegistration "
  308.           + segmentRegistration);
  309.     }
  310.   }

  311.   /*
  312.    * (non-Javadoc)
  313.    *
  314.    * @see
  315.    * mtas.codec.util.collector.MtasDataCollector#stringToBoundary(java.lang.
  316.    * String, java.lang.Integer)
  317.    */
  318.   @Override
  319.   protected Double stringToBoundary(String boundary, Integer segmentNumber)
  320.       throws IOException {
  321.     if (segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)
  322.         || segmentRegistration.equals(SEGMENT_BOUNDARY_DESC)) {
  323.       if (segmentNumber == null) {
  324.         return Double.valueOf(boundary);
  325.       } else {
  326.         return Double.valueOf(boundary) / segmentNumber;
  327.       }
  328.     } else {
  329.       throw new IOException(
  330.           "not available for segmentRegistration " + segmentRegistration);
  331.     }
  332.   }

  333.   /*
  334.    * (non-Javadoc)
  335.    *
  336.    * @see
  337.    * mtas.codec.util.collector.MtasDataCollector#validateSegmentBoundary(java.
  338.    * lang.Object)
  339.    */
  340.   @Override
  341.   public boolean validateSegmentBoundary(Object o) throws IOException {
  342.     if (o instanceof Double) {
  343.       return validateWithSegmentBoundary((Double) o);
  344.     } else {
  345.       throw new IOException("incorrect type");
  346.     }
  347.   }

  348. }