/* 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.parquet.column.statistics;

import java.util.Hashtable;

import org.apache.parquet.bytes.BytesUtils;

public class IntStatistics extends Statistics<Integer> {

  private int max;
  private int min;

  private DistributionStatistics<Integer> distStat;
  
  
  public DistributionStatistics<Integer> getDistStat() {
	  return distStat;
  }
  @Override
  public void updateStats(int value) {
    if (!this.hasNonNullValue()) {
      initializeStats(value, value);
      if (Statistics.statVersion == StatisticVersion.MODIFIED_STAT) {
    	  distStat = new DistributionStatistics<Integer>();
          distStat.initializeStats(value);
      }
    } else {
      updateStats(value, value);
      if(Statistics.statVersion == StatisticVersion.MODIFIED_STAT) {
    	  //Create by Lang Yu, 11:20 PM, Jun 6, 2016
    	  distStat.updateStat(value);
      }
    }
  }

  @Override
  public void mergeStatisticsMinMax(Statistics stats) {
	  IntStatistics intStats = (IntStatistics)stats;
	  if (!this.hasNonNullValue()) {
		  initializeStats(intStats.getMin(), intStats.getMax());
	  } else {
		  updateStats(intStats.getMin(), intStats.getMax());
	  }
	  distStat.mergeStats(intStats.getDistStat());
  }

  @Override
  public void setMinMaxFromBytes(byte[] minBytes, byte[] maxBytes) {
    max = BytesUtils.bytesToInt(maxBytes);
    min = BytesUtils.bytesToInt(minBytes);
    this.markAsNotEmpty();
  }

  @Override
  public byte[] getMaxBytes() {
    return BytesUtils.intToBytes(max);
  }

  @Override
  public byte[] getMinBytes() {
    return BytesUtils.intToBytes(min);
  }

  @Override
  public String toString() {
    if (this.hasNonNullValue())
      return String.format("min: %d, max: %d, num_nulls: %d", min, max, this.getNumNulls());
    else if (!this.isEmpty())
      return String.format("num_nulls: %d, min/max is not defined", this.getNumNulls());
    else
      return "no stats for this column";
  }

  public void updateStats(int min_value, int max_value) {
    if (min_value < min) { min = min_value; }
    if (max_value > max) { max = max_value; }
  }

  public void initializeStats(int min_value, int max_value) {
      min = min_value;
      max = max_value;
      this.markAsNotEmpty();
  }

  @Override
  public Integer genericGetMin() {
    return min;
  }

  @Override
  public Integer genericGetMax() {
    return max;
  }

  public int getMax() {
    return max;
  }

  public int getMin() {
    return min;
  }

  public void setMinMax(int min, int max) {
    this.max = max;
    this.min = min;
    this.markAsNotEmpty();
  }
  
  
  
}

@@ -1799,17 +1799,18 @@
 )stats;%0A
-%09
+  
   if (!t
@@ -1834,18 +1834,20 @@
 ue()) %7B%0A
-%09%09
+    
   initia
@@ -1895,17 +1895,154 @@
 Max());%0A
-%09
+      if (Statistics.statVersion == StatisticVersion.MODIFIED_STAT) %7B%0A        distStat = new DistributionStatistics%3CInteger%3E();%0A      %7D%0A  
   %7D else
@@ -2048,10 +2048,12 @@
 e %7B%0A
-%09%09
+    
   up
@@ -2105,14 +2105,86 @@
 ));%0A
-%09
+  
   %7D%0A
-%09
+    if (Statistics.statVersion == StatisticVersion.MODIFIED_STAT) %7B%0A    
   di
@@ -2226,16 +2226,22 @@
 tat());%0A
+    %7D%0A
   %7D%0A%0A  @

fix bug in intDistStat