<commit_before>package weatherhandler.processor.query;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.lang.AutoCloseable;

import weatherhandler.data.Measurement;
import weatherhandler.Logger;

/**
 * @author <NAME>
 * @author <NAME>
 *
 * Query class for computing the lowest value by some grouping. Output is sent
 * to an OutputStream in TSV format.
 */
public class MinQuery<T> extends NumericGroupedQuery<T> implements AutoCloseable {
    /**
     * Used by NumericGroupedQuery for logging (potentially).
     */
    protected Logger logger = new Logger("MinQuery");

    /**
     * Create a new MinQuery, directing output to the given output stream.
     *
     * @param out Output stream.
     * @param grouper Function that returns a group key for a given measurement.
     * @param mapper Function that returns the number to use for the min()
     *               computation.
     */
    public MinQuery(
        OutputStream out,
        Function<Measurement, T> grouper,
        ToDoubleFunction<Measurement> mapper
    ) {
        super(out, grouper, mapper);
    }

    /**
     * Create a new MinQuery, directing output to standard output.
     *
     * @param out Output stream.
     * @param grouper Function that returns a group key for a given measurement.
     * @param mapper Function that returns the number to use for the min()
     *               computation.
     */
    public MinQuery(
        Function<Measurement, T> grouper,
        ToDoubleFunction<Measurement> mapper
    ) {
        super(grouper, mapper);
    }

    /**
     * @return Query that computes the lowest temperature ever measured.
     */
    public static MinQuery<Integer> temperature() {
        return new MinQuery<Integer>(
            m -> 1,
            Measurement::getTemperature
        );
    }

    /**
     * Create a new query group.
     *
     * @return Query group that returns the lowest value in the given numbers.
     */
    protected Minimum newGroup() {
        return new Minimum();
    }

    /**
     * Print the result set to a file or stream in TSV format.
     */
    public void close() {
        this.output.println("group_key\tminimum");
        for (T group : groups.keySet()) {
            this.output.println(
                String.join("\t", new String[] {
                    "" + group,
                    "" + groups.get(group).get()
                })
            );
        }
    }

    /**
     * Group class for computing minima.
     */
    protected class Minimum implements NumericGroupedQuery.Group {
        /**
         * Lowest number seen so far.
         */
        private double min = 0;

        /**
         * Check a new contestant for the title of "Lowest Number Known To This
         * Group".
         *
         * @param n The daring new contestant.
         */
        public void add(double n) {
            if (n < min) {
                min = n;
            }
        }
<commit_msg>:zero: fix MinQuery formatting & min computation if all values are >0<commit_after>package weatherhandler.processor.query;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.lang.AutoCloseable;

import weatherhandler.data.Measurement;
import weatherhandler.Logger;

/**
 * @author <NAME>
 * @author <NAME>
 *
 * Query class for computing the lowest value by some grouping. Output is sent
 * to an OutputStream in TSV format.
 */
public class MinQuery<T> extends NumericGroupedQuery<T> implements AutoCloseable {
    /**
     * Used by NumericGroupedQuery for logging (potentially).
     */
    protected Logger logger = new Logger("MinQuery");

    /**
     * Create a new MinQuery, directing output to the given output stream.
     *
     * @param out Output stream.
     * @param grouper Function that returns a group key for a given measurement.
     * @param mapper Function that returns the number to use for the min()
     *               computation.
     */
    public MinQuery(
        OutputStream out,
        Function<Measurement, T> grouper,
        ToDoubleFunction<Measurement> mapper
    ) {
        super(out, grouper, mapper);
    }

    /**
     * Create a new MinQuery, directing output to standard output.
     *
     * @param out Output stream.
     * @param grouper Function that returns a group key for a given measurement.
     * @param mapper Function that returns the number to use for the min()
     *               computation.
     */
    public MinQuery(
        Function<Measurement, T> grouper,
        ToDoubleFunction<Measurement> mapper
    ) {
        super(grouper, mapper);
    }

    /**
     * @return Query that computes the lowest temperature ever measured.
     */
    public static MinQuery<Integer> temperature() {
        return new MinQuery<Integer>(
            m -> 1,
            Measurement::getTemperature
        );
    }

    /**
     * Create a new query group.
     *
     * @return Query group that returns the lowest value in the given numbers.
     */
    protected Minimum newGroup() {
        return new Minimum();
    }

    /**
     * Print the result set to a file or stream in TSV format.
     */
    public void close() {
        this.output.println("group_key\tminimum");
        for (T group : groups.keySet()) {
            this.output.println(
                String.join("\t", new String[] {
                    "" + group,
                    String.format("%.1f", groups.get(group).get())
                })
            );
        }
    }

    /**
     * Group class for computing minima.
     */
    protected class Minimum implements NumericGroupedQuery.Group {
        /**
         * Lowest number seen so far.
         */
        private double min = Double.MAX_VALUE;

        /**
         * Check a new contestant for the title of "Lowest Number Known To This
         * Group".
         *
         * @param n The daring new contestant.
         */
        public void add(double n) {
            if (n < min) {
                min = n;
            }
        }

{'fraction_non_alphanumeric': 0.09225385716557977, 'fraction_numerical': 0.000795291872117067, 'mean_word_length': 1.9576669802445907, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '522541', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1654, 'n_words': 563}