ArraySorter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3;

  18. import java.util.Arrays;
  19. import java.util.Comparator;

  20. /**
  21.  * Sorts and returns arrays in the fluent style.
  22.  *
  23.  * TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
  24.  * @since 3.12.0
  25.  */
  26. public class ArraySorter {

  27.     /**
  28.      * Sorts and returns the given array.
  29.      *
  30.      * @param array the array to sort.
  31.      * @return the given array.
  32.      * @see Arrays#sort(byte[])
  33.      */
  34.     public static byte[] sort(final byte[] array) {
  35.         if (array != null) {
  36.             Arrays.sort(array);
  37.         }
  38.         return array;
  39.     }

  40.     /**
  41.      * Sorts and returns the given array.
  42.      *
  43.      * @param array the array to sort.
  44.      * @return the given array.
  45.      * @see Arrays#sort(char[])
  46.      */
  47.     public static char[] sort(final char[] array) {
  48.         if (array != null) {
  49.             Arrays.sort(array);
  50.         }
  51.         return array;
  52.     }

  53.     /**
  54.      * Sorts and returns the given array.
  55.      *
  56.      * @param array the array to sort.
  57.      * @return the given array.
  58.      * @see Arrays#sort(double[])
  59.      */
  60.     public static double[] sort(final double[] array) {
  61.         if (array != null) {
  62.             Arrays.sort(array);
  63.         }
  64.         return array;
  65.     }

  66.     /**
  67.      * Sorts and returns the given array.
  68.      *
  69.      * @param array the array to sort.
  70.      * @return the given array.
  71.      * @see Arrays#sort(float[])
  72.      */
  73.     public static float[] sort(final float[] array) {
  74.         if (array != null) {
  75.             Arrays.sort(array);
  76.         }
  77.         return array;
  78.     }

  79.     /**
  80.      * Sorts and returns the given array.
  81.      *
  82.      * @param array the array to sort.
  83.      * @return the given array.
  84.      * @see Arrays#sort(int[])
  85.      */
  86.     public static int[] sort(final int[] array) {
  87.         if (array != null) {
  88.             Arrays.sort(array);
  89.         }
  90.         return array;
  91.     }

  92.     /**
  93.      * Sorts and returns the given array.
  94.      *
  95.      * @param array the array to sort.
  96.      * @return the given array.
  97.      * @see Arrays#sort(long[])
  98.      */
  99.     public static long[] sort(final long[] array) {
  100.         if (array != null) {
  101.             Arrays.sort(array);
  102.         }
  103.         return array;
  104.     }

  105.     /**
  106.      * Sorts and returns the given array.
  107.      *
  108.      * @param array the array to sort.
  109.      * @return the given array.
  110.      * @see Arrays#sort(short[])
  111.      */
  112.     public static short[] sort(final short[] array) {
  113.         if (array != null) {
  114.             Arrays.sort(array);
  115.         }
  116.         return array;
  117.     }

  118.     /**
  119.      * Sorts and returns the given array.
  120.      *
  121.      * @param <T> the array type.
  122.      * @param array the array to sort.
  123.      * @return the given array.
  124.      * @see Arrays#sort(Object[])
  125.      */
  126.     public static <T> T[] sort(final T[] array) {
  127.         if (array != null) {
  128.             Arrays.sort(array);
  129.         }
  130.         return array;
  131.     }

  132.     /**
  133.      * Sorts and returns the given array.
  134.      *
  135.      * @param <T> the array type.
  136.      * @param array the array to sort.
  137.      * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
  138.      *        {@link Comparable natural ordering}.
  139.      * @return the given array.
  140.      * @see Arrays#sort(Object[])
  141.      */
  142.     public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
  143.         if (array != null) {
  144.             Arrays.sort(array, comparator);
  145.         }
  146.         return array;
  147.     }

  148.     /**
  149.      * Constructs a new instance.
  150.      *
  151.      * @deprecated Will be removed in 4.0.0.
  152.      */
  153.     @Deprecated
  154.     public ArraySorter() {
  155.         // empty
  156.     }

  157. }