/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search 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.elasticsearch.search.facet.histogram;

import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.search.facet.Facet;

import java.util.Comparator;
import java.util.List;

/**
 * Numeric histogram facet.
 *
 * @author kimchy (shay.banon)
 */
public interface HistogramFacet extends Facet, Iterable<HistogramFacet.Entry> {

    /**
     * The key field name used with this facet.
     */
    String keyFieldName();

    /**
     * The key field name used with this facet.
     */
    String getKeyFieldName();

    /**
     * The value field name used with this facet.
     */
    String valueFieldName();

    /**
     * The value field name used with this facet.
     */
    String getValueFieldName();

    /**
     * An ordered list of histogram facet entries.
     */
    List<Entry> entries();

    /**
     * An ordered list of histogram facet entries.
     */
    List<Entry> getEntries();

    public static enum ComparatorType {
        KEY((byte) 0, "key", new Comparator<Entry>() {

            @Override public int compare(Entry o1, Entry o2) {
                // really, there should not be two entries with the same value
                int i = (int) (o1.key() - o2.key());
                if (i == 0) {
                    i = System.identityHashCode(o1) - System.identityHashCode(o2);
                }
                return i;
            }
        }),
        COUNT((byte) 1, "count", new Comparator<Entry>() {

            @Override public int compare(Entry o1, Entry o2) {
                int i = (int) (o2.count() - o1.count());
                if (i == 0) {
                    i = (int) (o2.total() - o1.total());
                    if (i == 0) {
                        i = System.identityHashCode(o2) - System.identityHashCode(o1);
                    }
                }
                return i;
            }
        }),
        TOTAL((byte) 2, "total", new Comparator<Entry>() {

            @Override public int compare(Entry o1, Entry o2) {
                int i = (int) (o2.total() - o1.total());
                if (i == 0) {
                    i = (int) (o2.count() - o1.count());
                    if (i == 0) {
                        i = System.identityHashCode(o2) - System.identityHashCode(o1);
                    }
                }
                return i;
            }
        });

        private final byte id;

        private final String description;

        private final Comparator<Entry> comparator;

        ComparatorType(byte id, String description, Comparator<Entry> comparator) {
            this.id = id;
            this.description = description;
            this.comparator = comparator;
        }

        public byte id() {
            return this.id;
        }

        public String description() {
            return this.description;
        }

        public Comparator<Entry> comparator() {
            return comparator;
        }

        public static ComparatorType fromId(byte id) {
            if (id == 0) {
                return KEY;
            } else if (id == 1) {
                return COUNT;
            } else if (id == 2) {
                return TOTAL;
            }
            throw new ElasticSearchIllegalArgumentException("No type argument match for histogram comparator [" + id + "]");
        }

        public static ComparatorType fromString(String type) {
            if ("key".equals(type)) {
                return KEY;
            } else if ("count".equals(type)) {
                return COUNT;
            } else if ("total".equals(type)) {
                return TOTAL;
            }
            throw new ElasticSearchIllegalArgumentException("No type argument match for histogram comparator [" + type + "]");
        }
    }


    /**
     * A histogram entry representing a single entry within the result of a histogram facet.
     */
    public class Entry {
        private final long key;
        private final long count;
        private final double total;

        public Entry(long key, long count, double total) {
            this.key = key;
            this.count = count;
            this.total = total;
        }

        /**
         * The key value of the histogram.
         */
        public long key() {
            return key;
        }

        /**
         * The key value of the histogram.
         */
        public long getKey() {
            return key();
        }

        /**
         * The number of hits that fall within that key "range" or "interval".
         */
        public long count() {
            return count;
        }

        /**
         * The number of hits that fall within that key "range" or "interval".
         */
        public long getCount() {
            return count();
        }

        /**
         * The sum / total of the value field that fall within this key "interval".
         */
        public double total() {
            return total;
        }

        /**
         * The sum / total of the value field that fall within this key "interval".
         */
        public double getTotal() {
            return total();
        }

        /**
         * The mean of this facet interval.
         */
        public double mean() {
            return total / count;
        }

        /**
         * The mean of this facet interval.
         */
        public double getMean() {
            return mean();
        }
    }
}
@@ -1921,100 +1921,14 @@
     
-// really, there should not be two entries with the same value%0A                int i = (int)
+return
  (o1
@@ -1934,17 +1934,17 @@
 1.key() 
--
+%3C
  o2.key(
@@ -1948,166 +1948,47 @@
 ey()
-);%0A                if (i == 0) %7B%0A                    i = System.identityHashCode(o1) - System.identityHashCode(o2);%0A                %7D%0A                return i
+ ? -1 : (o1.key() == o2.key() ? 0 : 1))
 ;%0A  
@@ -2150,33 +2150,26 @@
         
-int i = (int) (o2
+return (o1
 .count()
@@ -2165,36 +2165,36 @@
 (o1.count() 
-- o1
+%3C o2
 .count()
 );%0A         
@@ -2185,283 +2185,51 @@
 nt()
-);%0A                if (i == 0) %7B%0A                    i = (int) (o2.total() - o1.total());%0A                    if (i == 0) %7B%0A                        i = System.identityHashCode(o2) - System.identityHashCode(o1);%0A                    %7D%0A                %7D%0A                return i
+ ? -1 : (o1.count() == o2.count() ? 0 : 1))
 ;%0A  
@@ -2395,25 +2395,18 @@
     
-int i = (int) (o2
+return (o1
 .tot
@@ -2414,20 +2414,20 @@
 l() 
-- o1
+%3C o2
 .total()
 );%0A 
@@ -2426,283 +2426,51 @@
 al()
-);%0A                if (i == 0) %7B%0A                    i = (int) (o2.count() - o1.count());%0A                    if (i == 0) %7B%0A                        i = System.identityHashCode(o2) - System.identityHashCode(o1);%0A                    %7D%0A                %7D%0A                return i
+ ? -1 : (o1.total() == o2.total() ? 0 : 1))
 ;%0A  

fix comparator implementation in histogram facet