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.stat.descriptive.rank;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.exception.MathIllegalArgumentException;
022import org.apache.commons.math4.exception.NullArgumentException;
023import org.apache.commons.math4.stat.ranking.NaNStrategy;
024import org.apache.commons.math4.util.KthSelector;
025
026
027/**
028 * Returns the median of the available values.  This is the same as the 50th percentile.
029 * See {@link Percentile} for a description of the algorithm used.
030 * <p>
031 * <strong>Note that this implementation is not synchronized.</strong> If
032 * multiple threads access an instance of this class concurrently, and at least
033 * one of the threads invokes the <code>increment()</code> or
034 * <code>clear()</code> method, it must be synchronized externally.</p>
035 */
036public class Median extends Percentile implements Serializable {
037
038    /** Serializable version identifier */
039    private static final long serialVersionUID = 20150412L;
040
041    /** Fixed quantile. */
042    private static final double FIXED_QUANTILE_50 = 50.0;
043
044    /**
045     * Default constructor.
046     */
047    public Median() {
048        // No try-catch or advertised exception - arg is valid
049        super(FIXED_QUANTILE_50);
050    }
051
052    /**
053     * Copy constructor, creates a new {@code Median} identical
054     * to the {@code original}
055     *
056     * @param original the {@code Median} instance to copy
057     * @throws NullArgumentException if original is null
058     */
059    public Median(Median original) throws NullArgumentException {
060        super(original);
061    }
062
063    /**
064     * Constructs a Median with the specific {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
065     *
066     * @param estimationType one of the percentile {@link EstimationType  estimation types}
067     * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
068     * @param kthSelector {@link KthSelector} to use for pivoting during search
069     * @throws MathIllegalArgumentException if p is not within (0,100]
070     * @throws NullArgumentException if type or NaNStrategy passed is null
071     */
072    private Median(final EstimationType estimationType, final NaNStrategy nanStrategy,
073                   final KthSelector kthSelector)
074        throws MathIllegalArgumentException {
075        super(FIXED_QUANTILE_50, estimationType, nanStrategy, kthSelector);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public Median withEstimationType(final EstimationType newEstimationType) {
081        return new Median(newEstimationType, getNaNStrategy(), getKthSelector());
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public Median withNaNStrategy(final NaNStrategy newNaNStrategy) {
087        return new Median(getEstimationType(), newNaNStrategy, getKthSelector());
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public Median withKthSelector(final KthSelector newKthSelector) {
093        return new Median(getEstimationType(), getNaNStrategy(), newKthSelector);
094    }
095
096}