functions.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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: 2013-12-02 $</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     public static final Function<String, Integer64>
092     StringToInteger64 = new Function<String, Integer64>() {
093         @Override public Integer64 apply(final String value) {
094             return Integer64.valueOf(value);
095         }
096     };
097 
098     /**
099      * Convert a string to a float. If the string can't be converted, an
100      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
101      * method.
102      */
103     public static final Function<String, Float>
104     StringToFloat = new Function<String, Float>() {
105         @Override public Float apply(final String value) {
106             return Float.parseFloat(value);
107         }
108     };
109 
110     /**
111      * Convert a string to a double. If the string can't be converted, an
112      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
113      * method.
114      */
115     public static final Function<String, Double>
116     StringToDouble = new Function<String, Double>() {
117         @Override public Double apply(final String value) {
118             return Double.parseDouble(value);
119         }
120     };
121 
122     /**
123      * Convert a string to a Float64. If the string can't be converted, an
124      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
125      * method.
126      */
127     public static final Function<String, Float64>
128     StringToFloat64 = new Function<String, Float64>() {
129         @Override public Float64 apply(final String value) {
130             return Float64.valueOf(value);
131         }
132     };
133 
134     /**
135      * Convert a {@link Float64} value to a {@link Double} value.
136      */
137     public static final Function<Float64, Double>
138     Float64ToDouble = new Function<Float64, Double>() {
139         @Override public Double apply(final Float64 value) {
140             return value.doubleValue();
141         }
142     };
143 
144     /**
145      * Convert a {@link Double} value to a {@link Float64} value.
146      */
147     public static final Function<Double, Float64>
148     DoubleToFloat64 = new Function<Double, Float64>() {
149         @Override public Float64 apply(final Double value) {
150             return Float64.valueOf(value);
151         }
152     };
153 
154     /**
155      * Convert a {@link Integer64} value to a {@link Long} value.
156      */
157     public static final Function<Integer64, Long>
158     Integer64ToLong = new Function<Integer64, Long>() {
159         @Override public Long apply(final Integer64 value) {
160             return value.longValue();
161         }
162     };
163 
164     /**
165      * Convert a {link Long} value to a {@link Integer64} value.
166      */
167     public static final Function<Long, Integer64>
168     LongToInteger64 = new Function<Long, Integer64>() {
169         @Override public Integer64 apply(final Long value) {
170             return Integer64.valueOf(value);
171         }
172     };
173 
174     /**
175      * A predicate which return {@code true} if an given value is {@code null}.
176      */
177     public static final Function<Object, Boolean>
178     Null = new Function<Object, Boolean>() {
179         @Override public Boolean apply(final Object object) {
180             return object == null ? Boolean.TRUE : Boolean.FALSE;
181         }
182         @Override public String toString() {
183             return format("%s", getClass().getSimpleName());
184         }
185     };
186 
187     /**
188      * Return a predicate which negates the return value of the given predicate.
189      *
190      @param <T> the value type to check.
191      @param a the predicate to negate.
192      @return a predicate which negates the return value of the given predicate.
193      @throws NullPointerException if the given predicate is {@code null}.
194      */
195     public static <T> Function<T, Boolean> not(final Function<? super T, Boolean> a) {
196         requireNonNull(a);
197         return new Function<T, Boolean>() {
198             @Override public Boolean apply(final T object) {
199                 return a.apply(object? Boolean.FALSE : Boolean.TRUE;
200             }
201             @Override public String toString() {
202                 return format("%s[%s]", getClass().getSimpleName(), a);
203             }
204         };
205     }
206 
207     /**
208      * Return a {@code and} combination of the given predicates.
209      *
210      @param <T> the value type to check.
211      @param a the first predicate
212      @param b the second predicate
213      @return a {@code and} combination of the given predicates.
214      @throws NullPointerException if one of the given predicates is
215      *         {@code null}.
216      */
217     public static <T> Function<T, Boolean> and(
218         final Function<? super T, Boolean> a,
219         final Function<? super T, Boolean> b
220     ) {
221         requireNonNull(a);
222         requireNonNull(b);
223         return new Function<T, Boolean>() {
224             @Override public Boolean apply(final T object) {
225                 return a.apply(object&& b.apply(object);
226             }
227             @Override public String toString() {
228                 return format("%s[%s, %s]", getClass().getSimpleName(), a, b);
229             }
230         };
231     }
232 
233     /**
234      * Return a {@code or} combination of the given predicates.
235      *
236      @param <T> the value type to check.
237      @param a the first predicate
238      @param b the second predicate
239      @return a {@code and} combination of the given predicates.
240      @throws NullPointerException if one of the given predicates is
241      *          {@code null}.
242      */
243     public static <T> Function<T, Boolean> or(
244         final Function<? super T, Boolean> a,
245         final Function<? super T, Boolean> b
246     ) {
247         requireNonNull(a);
248         requireNonNull(b);
249         return new Function<T, Boolean>() {
250             @Override public Boolean apply(final T object) {
251                 return a.apply(object|| b.apply(object);
252             }
253             @Override public String toString() {
254                 return format(
255                     "%s[%s, %s]",
256                     getClass().getSimpleName(), a, b
257                 );
258             }
259         };
260     }
261 
262 
263     private static final class Identity
264         implements Function<Object, Object>, Serializable
265     {
266         private static final long serialVersionUID = 1L;
267 
268         @Override
269         public Object apply(final Object value) {
270             return value;
271         }
272     }
273 
274     private static Function<Object, Object> IDENTITY = new Identity();
275 
276     /**
277      * Return the identity function for the given type.
278      *
279      @return the identity function for the given type.
280      */
281     @SuppressWarnings("unchecked")
282     public static <T> Function<T, T> Identity() {
283         return (Function<T, T>)IDENTITY;
284     }
285 
286 
287     public static <A, B, C> Function<A, C> compose(
288         final Function<A, B> f1,
289         final Function<B, C> f2
290     ) {
291         requireNonNull(f1, "Function 1");
292         requireNonNull(f2, "Function 2");
293 
294         return new Function<A, C>() {
295             @Override public C apply(A value) {
296                 return f2.apply(f1.apply(value));
297             }
298         };
299     }
300 
301     public static <A, B, C, D> Function<A, D> compose(
302         final Function<A, B> f1,
303         final Function<B, C> f2,
304         final Function<C, D> f3
305     ) {
306         return compose(compose(f1, f2), f3);
307     }
308 
309     public static <A, B, C, D, E> Function<A, E> compose(
310         final Function<A, B> f1,
311         final Function<B, C> f2,
312         final Function<C, D> f3,
313         final Function<D, E> f4
314     ) {
315         return compose(compose(compose(f1, f2), f3), f4);
316     }
317 
318     public static <A, B, C, D, E, F> Function<A, F> compose(
319         final Function<A, B> f1,
320         final Function<B, C> f2,
321         final Function<C, D> f3,
322         final Function<D, E> f4,
323         final Function<E, F> f5
324     ) {
325         return compose(compose(compose(compose(f1, f2), f3), f4), f5);
326     }
327 
328 }
329 
330 
331 
332 
333