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 *      https://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.lang3;
018
019import java.util.Arrays;
020import java.util.Comparator;
021
022/**
023 * Sorts and returns arrays in the fluent style.
024 *
025 * TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
026 *
027 * @since 3.12.0
028 */
029public class ArraySorter {
030
031    /**
032     * Sorts the given array into ascending order and returns it.
033     *
034     * @param array the array to sort (may be null).
035     * @return the given array.
036     * @see Arrays#sort(byte[])
037     */
038    public static byte[] sort(final byte[] array) {
039        if (array != null) {
040            Arrays.sort(array);
041        }
042        return array;
043    }
044
045    /**
046     * Sorts the given array into ascending order and returns it.
047     *
048     * @param array the array to sort (may be null).
049     * @return the given array.
050     * @see Arrays#sort(char[])
051     */
052    public static char[] sort(final char[] array) {
053        if (array != null) {
054            Arrays.sort(array);
055        }
056        return array;
057    }
058
059    /**
060     * Sorts the given array into ascending order and returns it.
061     *
062     * @param array the array to sort (may be null).
063     * @return the given array.
064     * @see Arrays#sort(double[])
065     */
066    public static double[] sort(final double[] array) {
067        if (array != null) {
068            Arrays.sort(array);
069        }
070        return array;
071    }
072
073    /**
074     * Sorts the given array into ascending order and returns it.
075     *
076     * @param array the array to sort (may be null).
077     * @return the given array.
078     * @see Arrays#sort(float[])
079     */
080    public static float[] sort(final float[] array) {
081        if (array != null) {
082            Arrays.sort(array);
083        }
084        return array;
085    }
086
087    /**
088     * Sorts the given array into ascending order and returns it.
089     *
090     * @param array the array to sort (may be null).
091     * @return the given array.
092     * @see Arrays#sort(int[])
093     */
094    public static int[] sort(final int[] array) {
095        if (array != null) {
096            Arrays.sort(array);
097        }
098        return array;
099    }
100
101    /**
102     * Sorts the given array into ascending order and returns it.
103     *
104     * @param array the array to sort (may be null).
105     * @return the given array.
106     * @see Arrays#sort(long[])
107     */
108    public static long[] sort(final long[] array) {
109        if (array != null) {
110            Arrays.sort(array);
111        }
112        return array;
113    }
114
115    /**
116     * Sorts the given array into ascending order and returns it.
117     *
118     * @param array the array to sort (may be null).
119     * @return the given array.
120     * @see Arrays#sort(short[])
121     */
122    public static short[] sort(final short[] array) {
123        if (array != null) {
124            Arrays.sort(array);
125        }
126        return array;
127    }
128
129    /**
130     * Sorts the given array into ascending order and returns it.
131     *
132     * @param <T> the array type.
133     * @param array the array to sort (may be null).
134     * @return the given array.
135     * @see Arrays#sort(Object[])
136     */
137    public static <T> T[] sort(final T[] array) {
138        if (array != null) {
139            Arrays.sort(array);
140        }
141        return array;
142    }
143
144    /**
145     * Sorts the given array into ascending order and returns it.
146     *
147     * @param <T> the array type.
148     * @param array the array to sort (may be null).
149     * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
150     *        {@link Comparable natural ordering}.
151     * @return the given array.
152     * @see Arrays#sort(Object[])
153     */
154    public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
155        if (array != null) {
156            Arrays.sort(array, comparator);
157        }
158        return array;
159    }
160
161    /**
162     * Constructs a new instance.
163     *
164     * @deprecated Will be removed in 4.0.0.
165     */
166    @Deprecated
167    public ArraySorter() {
168        // empty
169    }
170
171}