View Javadoc
1   /*
2    * Copyright (C) 2014 Sven von Pluto - javanarior (a) gmail dot com
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.javanarior.utils;
17  
18  import de.javanarior.utils.compare.SpeakingComparable;
19  import de.javanarior.utils.compare.SpeakingComparableAdapter;
20  
21  /**
22   * Factory to create speaking API instances.
23   */
24  public final class Speaking {
25  
26      private Speaking() {
27          /* Factory class */
28      }
29  
30      /**
31       * Create a {@linkplain SpeakingComparable}.
32       *
33       * @param comparable
34       *            - to compare
35       * @param <T>
36       *            - type which is compared
37       * @return speaking comparable
38       */
39      public static <T> SpeakingComparable<T> comparable(Comparable<T> comparable) {
40          if (null == comparable) {
41              throw new IllegalArgumentException("Argument 'comparable' must not be null");
42          }
43          return new SpeakingComparableWrapper<T>(comparable);
44      }
45  
46      private static final class SpeakingComparableWrapper<T> extends SpeakingComparableAdapter<T> {
47  
48          private final Comparable<T> comparable;
49  
50          SpeakingComparableWrapper(Comparable<T> comparable) {
51              this.comparable = comparable;
52          }
53  
54          @Override
55          public int compareTo(T other) {
56              return comparable.compareTo(other);
57          }
58      }
59  
60  }