001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.stat.descriptive.rank;
018
019import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
020import org.apache.commons.math4.legacy.exception.NullArgumentException;
021import org.apache.commons.math4.legacy.stat.ranking.NaNStrategy;
022
023
024/**
025 * Returns the median of the available values.  This is the same as the 50th percentile.
026 * See {@link Percentile} for a description of the algorithm used.
027 * <p>
028 * <strong>Note that this implementation is not synchronized.</strong> If
029 * multiple threads access an instance of this class concurrently, and at least
030 * one of the threads invokes the <code>increment()</code> or
031 * <code>clear()</code> method, it must be synchronized externally.</p>
032 */
033public class Median extends Percentile {
034    /** Fixed quantile. */
035    private static final double FIXED_QUANTILE_50 = 50.0;
036
037    /**
038     * Default constructor.
039     */
040    public Median() {
041        // No try-catch or advertised exception - arg is valid
042        super(FIXED_QUANTILE_50);
043    }
044
045    /**
046     * Copy constructor, creates a new {@code Median} identical.
047     * to the {@code original}
048     *
049     * @param original the {@code Median} instance to copy
050     * @throws NullArgumentException if original is null
051     */
052    public Median(Median original) throws NullArgumentException {
053        super(original);
054    }
055
056    /**
057     * Constructs a Median with the specific {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
058     *
059     * @param estimationType one of the percentile {@link EstimationType  estimation types}
060     * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
061     * @param kthSelector {@link KthSelector} to use for pivoting during search
062     * @throws MathIllegalArgumentException if p is not within (0,100]
063     * @throws NullArgumentException if type or NaNStrategy passed is null
064     */
065    private Median(final EstimationType estimationType, final NaNStrategy nanStrategy,
066                   final KthSelector kthSelector)
067        throws MathIllegalArgumentException {
068        super(FIXED_QUANTILE_50, estimationType, nanStrategy, kthSelector);
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public Median withEstimationType(final EstimationType newEstimationType) {
074        return new Median(newEstimationType, getNaNStrategy(), getKthSelector());
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public Median withNaNStrategy(final NaNStrategy newNaNStrategy) {
080        return new Median(getEstimationType(), newNaNStrategy, getKthSelector());
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public Median withKthSelector(final KthSelector newKthSelector) {
086        return new Median(getEstimationType(), getNaNStrategy(), newKthSelector);
087    }
088}