functions.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * 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  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.util;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 
025 import java.io.Serializable;
026 import java.util.Objects;
027 
028 import org.jscience.mathematics.number.Float64;
029 import org.jscience.mathematics.number.Integer64;
030 
031 /**
032  * This class contains some short general purpose functions.
033  *
034  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
035  @since 1.0
036  @version 1.0 &mdash; <em>$Date: 2014-02-27 $</em>
037  */
038 public final class functions extends StaticObject {
039     private functions() {}
040 
041     /**
042      * Convert an object to a string by calling the objects {@link Object#toString()}
043      * method.
044      */
045     public static final Function<Object, String>
046     ObjectToString = new Function<Object, String>() {
047         @Override public String apply(final Object value) {
048             return Objects.toString(value);
049         }
050     };
051 
052     /**
053      * Convert a string value to its length.
054      */
055     public static final Function<String, Integer>
056     StringLength = new Function<String, Integer>() {
057         @Override public Integer apply(final String value) {
058             return value.length();
059         }
060     };
061 
062     /**
063      * Convert a string to an integer. If the string can't be converted, an
064      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
065      * method.
066      */
067     public static final Function<String, Integer>
068     StringToInteger = new Function<String, Integer>() {
069         @Override public Integer apply(final String value) {
070             return Integer.parseInt(value);
071         }
072     };
073 
074     /**
075      * Convert a string to a long. If the string can't be converted, an
076      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
077      * method.
078      */
079     public static final Function<String, Long>
080     StringToLong = new Function<String, Long>() {
081         @Override public Long apply(final String value) {
082             return Long.parseLong(value);
083         }
084     };
085 
086     /**
087      * Convert a string to an Integer64. If the string can't be converted, an
088      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
089      * method.
090      *
091      @deprecated Will be removed.
092      */
093     @Deprecated
094     public static final Function<String, Integer64>
095     StringToInteger64 = new Function<String, Integer64>() {
096         @Override public Integer64 apply(final String value) {
097             return Integer64.valueOf(value);
098         }
099     };
100 
101     /**
102      * Convert a string to a float. If the string can't be converted, an
103      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
104      * method.
105      */
106     public static final Function<String, Float>
107     StringToFloat = new Function<String, Float>() {
108         @Override public Float apply(final String value) {
109             return Float.parseFloat(value);
110         }
111     };
112 
113     /**
114      * Convert a string to a double. If the string can't be converted, an
115      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
116      * method.
117      */
118     public static final Function<String, Double>
119     StringToDouble = new Function<String, Double>() {
120         @Override public Double apply(final String value) {
121             return Double.parseDouble(value);
122         }
123     };
124 
125     /**
126      * Convert a string to a Float64. If the string can't be converted, an
127      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
128      * method.
129      *
130      @deprecated Will be removed.
131      */
132     @Deprecated
133     public static final Function<String, Float64>
134     StringToFloat64 = new Function<String, Float64>() {
135         @Override public Float64 apply(final String value) {
136             return Float64.valueOf(value);
137         }
138     };
139 
140     /**
141      * Convert a {@link Float64} value to a {@link Double} value.
142      *
143      @deprecated Will be removed.
144      */
145     @Deprecated
146     public static final Function<Float64, Double>
147     Float64ToDouble = new Function<Float64, Double>() {
148         @Override public Double apply(final Float64 value) {
149             return value.doubleValue();
150         }
151     };
152 
153     /**
154      * Convert a {@link Double} value to a {@link Float64} value.
155      *
156      @deprecated Will be removed.
157      */
158     @Deprecated
159     public static final Function<Double, Float64>
160     DoubleToFloat64 = new Function<Double, Float64>() {
161         @Override public Float64 apply(final Double value) {
162             return Float64.valueOf(value);
163         }
164     };
165 
166     /**
167      * Convert a {@link Integer64} value to a {@link Long} value.
168      *
169      @deprecated Will be removed.
170      */
171     @Deprecated
172     public static final Function<Integer64, Long>
173     Integer64ToLong = new Function<Integer64, Long>() {
174         @Override public Long apply(final Integer64 value) {
175             return value.longValue();
176         }
177     };
178 
179     /**
180      * Convert a {link Long} value to a {@link Integer64} value.
181      *
182      @deprecated Will be removed.
183      */
184     @Deprecated
185     public static final Function<Long, Integer64>
186     LongToInteger64 = new Function<Long, Integer64>() {
187         @Override public Integer64 apply(final Long value) {
188             return Integer64.valueOf(value);
189         }
190     };
191 
192     /**
193      * A predicate which return {@code true} if an given value is {@code null}.
194      */
195     public static final Function<Object, Boolean>
196     Null = new Function<Object, Boolean>() {
197         @Override public Boolean apply(final Object object) {
198             return object == null ? Boolean.TRUE : Boolean.FALSE;
199         }
200         @Override public String toString() {
201             return format("%s", getClass().getSimpleName());
202         }
203     };
204 
205     /**
206      * Return a predicate which negates the return value of the given predicate.
207      *
208      @param <T> the value type to check.
209      @param a the predicate to negate.
210      @return a predicate which negates the return value of the given predicate.
211      @throws NullPointerException if the given predicate is {@code null}.
212      */
213     public static <T> Function<T, Boolean> not(final Function<? super T, Boolean> a) {
214         requireNonNull(a);
215         return new Function<T, Boolean>() {
216             @Override public Boolean apply(final T object) {
217                 return a.apply(object? Boolean.FALSE : Boolean.TRUE;
218             }
219             @Override public String toString() {
220                 return format("%s[%s]", getClass().getSimpleName(), a);
221             }
222         };
223     }
224 
225     /**
226      * Return a {@code and} combination of the given predicates.
227      *
228      @param <T> the value type to check.
229      @param a the first predicate
230      @param b the second predicate
231      @return a {@code and} combination of the given predicates.
232      @throws NullPointerException if one of the given predicates is
233      *         {@code null}.
234      */
235     public static <T> Function<T, Boolean> and(
236         final Function<? super T, Boolean> a,
237         final Function<? super T, Boolean> b
238     ) {
239         requireNonNull(a);
240         requireNonNull(b);
241         return new Function<T, Boolean>() {
242             @Override public Boolean apply(final T object) {
243                 return a.apply(object&& b.apply(object);
244             }
245             @Override public String toString() {
246                 return format("%s[%s, %s]", getClass().getSimpleName(), a, b);
247             }
248         };
249     }
250 
251     /**
252      * Return a {@code or} combination of the given predicates.
253      *
254      @param <T> the value type to check.
255      @param a the first predicate
256      @param b the second predicate
257      @return a {@code and} combination of the given predicates.
258      @throws NullPointerException if one of the given predicates is
259      *          {@code null}.
260      */
261     public static <T> Function<T, Boolean> or(
262         final Function<? super T, Boolean> a,
263         final Function<? super T, Boolean> b
264     ) {
265         requireNonNull(a);
266         requireNonNull(b);
267         return new Function<T, Boolean>() {
268             @Override public Boolean apply(final T object) {
269                 return a.apply(object|| b.apply(object);
270             }
271             @Override public String toString() {
272                 return format(
273                     "%s[%s, %s]",
274                     getClass().getSimpleName(), a, b
275                 );
276             }
277         };
278     }
279 
280 
281     private static final class Identity
282         implements Function<Object, Object>, Serializable
283     {
284         private static final long serialVersionUID = 1L;
285 
286         @Override
287         public Object apply(final Object value) {
288             return value;
289         }
290     }
291 
292     private static Function<Object, Object> IDENTITY = new Identity();
293 
294     /**
295      * Return the identity function for the given type.
296      *
297      @return the identity function for the given type.
298      */
299     @SuppressWarnings("unchecked")
300     public static <T> Function<T, T> Identity() {
301         return (Function<T, T>)IDENTITY;
302     }
303 
304 
305     public static <A, B, C> Function<A, C> compose(
306         final Function<A, B> f1,
307         final Function<B, C> f2
308     ) {
309         requireNonNull(f1, "Function 1");
310         requireNonNull(f2, "Function 2");
311 
312         return new Function<A, C>() {
313             @Override public C apply(A value) {
314                 return f2.apply(f1.apply(value));
315             }
316         };
317     }
318 
319     public static <A, B, C, D> Function<A, D> compose(
320         final Function<A, B> f1,
321         final Function<B, C> f2,
322         final Function<C, D> f3
323     ) {
324         return compose(compose(f1, f2), f3);
325     }
326 
327     public static <A, B, C, D, E> Function<A, E> compose(
328         final Function<A, B> f1,
329         final Function<B, C> f2,
330         final Function<C, D> f3,
331         final Function<D, E> f4
332     ) {
333         return compose(compose(compose(f1, f2), f3), f4);
334     }
335 
336     public static <A, B, C, D, E, F> Function<A, F> compose(
337         final Function<A, B> f1,
338         final Function<B, C> f2,
339         final Function<C, D> f3,
340         final Function<D, E> f4,
341         final Function<E, F> f5
342     ) {
343         return compose(compose(compose(compose(f1, f2), f3), f4), f5);
344     }
345 
346 }