View Javadoc
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  
19  import java.util.Arrays;
20  import java.util.Comparator;
21  
22  /**
23   * Sorts and returns arrays in the fluent style.
24   *
25   * TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
26   * @since 3.12.0
27   */
28  public class ArraySorter {
29  
30      /**
31       * Sorts and returns the given array.
32       *
33       * @param array the array to sort.
34       * @return the given array.
35       * @see Arrays#sort(byte[])
36       */
37      public static byte[] sort(final byte[] array) {
38          if (array != null) {
39              Arrays.sort(array);
40          }
41          return array;
42      }
43  
44      /**
45       * Sorts and returns the given array.
46       *
47       * @param array the array to sort.
48       * @return the given array.
49       * @see Arrays#sort(char[])
50       */
51      public static char[] sort(final char[] array) {
52          if (array != null) {
53              Arrays.sort(array);
54          }
55          return array;
56      }
57  
58      /**
59       * Sorts and returns the given array.
60       *
61       * @param array the array to sort.
62       * @return the given array.
63       * @see Arrays#sort(double[])
64       */
65      public static double[] sort(final double[] array) {
66          if (array != null) {
67              Arrays.sort(array);
68          }
69          return array;
70      }
71  
72      /**
73       * Sorts and returns the given array.
74       *
75       * @param array the array to sort.
76       * @return the given array.
77       * @see Arrays#sort(float[])
78       */
79      public static float[] sort(final float[] array) {
80          if (array != null) {
81              Arrays.sort(array);
82          }
83          return array;
84      }
85  
86      /**
87       * Sorts and returns the given array.
88       *
89       * @param array the array to sort.
90       * @return the given array.
91       * @see Arrays#sort(int[])
92       */
93      public static int[] sort(final int[] array) {
94          if (array != null) {
95              Arrays.sort(array);
96          }
97          return array;
98      }
99  
100     /**
101      * Sorts and returns the given array.
102      *
103      * @param array the array to sort.
104      * @return the given array.
105      * @see Arrays#sort(long[])
106      */
107     public static long[] sort(final long[] array) {
108         if (array != null) {
109             Arrays.sort(array);
110         }
111         return array;
112     }
113 
114     /**
115      * Sorts and returns the given array.
116      *
117      * @param array the array to sort.
118      * @return the given array.
119      * @see Arrays#sort(short[])
120      */
121     public static short[] sort(final short[] array) {
122         if (array != null) {
123             Arrays.sort(array);
124         }
125         return array;
126     }
127 
128     /**
129      * Sorts and returns the given array.
130      *
131      * @param <T> the array type.
132      * @param array the array to sort.
133      * @return the given array.
134      * @see Arrays#sort(Object[])
135      */
136     public static <T> T[] sort(final T[] array) {
137         if (array != null) {
138             Arrays.sort(array);
139         }
140         return array;
141     }
142 
143     /**
144      * Sorts and returns the given array.
145      *
146      * @param <T> the array type.
147      * @param array the array to sort.
148      * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
149      *        {@link Comparable natural ordering}.
150      * @return the given array.
151      * @see Arrays#sort(Object[])
152      */
153     public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
154         if (array != null) {
155             Arrays.sort(array, comparator);
156         }
157         return array;
158     }
159 
160     /**
161      * Constructs a new instance.
162      *
163      * @deprecated Will be removed in 4.0.0.
164      */
165     @Deprecated
166     public ArraySorter() {
167         // empty
168     }
169 
170 }