functions.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-2.0.2).
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 /**
029  * This class contains some short general purpose functions.
030  *
031  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
032  @since 1.0
033  @version 2.0 &mdash; <em>$Date: 2014-03-31 $</em>
034  */
035 public final class functions extends StaticObject {
036     private functions() {}
037 
038     /**
039      * Convert an object to a string by calling the objects {@link Object#toString()}
040      * method.
041      */
042     public static final Function<Object, String>
043     ObjectToString = new Function<Object, String>() {
044         @Override public String apply(final Object value) {
045             return Objects.toString(value);
046         }
047     };
048 
049     /**
050      * Convert a string value to its length.
051      */
052     public static final Function<String, Integer>
053     StringLength = new Function<String, Integer>() {
054         @Override public Integer apply(final String value) {
055             return value.length();
056         }
057     };
058 
059     /**
060      * Convert a string to an integer. If the string can't be converted, an
061      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
062      * method.
063      */
064     public static final Function<String, Integer>
065     StringToInteger = new Function<String, Integer>() {
066         @Override public Integer apply(final String value) {
067             return Integer.parseInt(value);
068         }
069     };
070 
071     /**
072      * Convert a string to a long. If the string can't be converted, an
073      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
074      * method.
075      */
076     public static final Function<String, Long>
077     StringToLong = new Function<String, Long>() {
078         @Override public Long apply(final String value) {
079             return Long.parseLong(value);
080         }
081     };
082 
083     /**
084      * Convert a string to a float. If the string can't be converted, an
085      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
086      * method.
087      */
088     public static final Function<String, Float>
089     StringToFloat = new Function<String, Float>() {
090         @Override public Float apply(final String value) {
091             return Float.parseFloat(value);
092         }
093     };
094 
095     /**
096      * Convert a string to a double. If the string can't be converted, an
097      {@link NumberFormatException} is throws by the {@link Function#apply(Object)}
098      * method.
099      */
100     public static final Function<String, Double>
101     StringToDouble = new Function<String, Double>() {
102         @Override public Double apply(final String value) {
103             return Double.parseDouble(value);
104         }
105     };
106 
107 
108     /**
109      * A predicate which return {@code true} if an given value is {@code null}.
110      */
111     public static final Function<Object, Boolean>
112     Null = new Function<Object, Boolean>() {
113         @Override public Boolean apply(final Object object) {
114             return object == null ? Boolean.TRUE : Boolean.FALSE;
115         }
116         @Override public String toString() {
117             return format("%s", getClass().getSimpleName());
118         }
119     };
120 
121     /**
122      * Return a predicate which negates the return value of the given predicate.
123      *
124      @param <T> the value type to check.
125      @param a the predicate to negate.
126      @return a predicate which negates the return value of the given predicate.
127      @throws NullPointerException if the given predicate is {@code null}.
128      */
129     public static <T> Function<T, Boolean> not(final Function<? super T, Boolean> a) {
130         requireNonNull(a);
131         return new Function<T, Boolean>() {
132             @Override public Boolean apply(final T object) {
133                 return a.apply(object? Boolean.FALSE : Boolean.TRUE;
134             }
135             @Override public String toString() {
136                 return format("%s[%s]", getClass().getSimpleName(), a);
137             }
138         };
139     }
140 
141     /**
142      * Return a {@code and} combination of the given predicates.
143      *
144      @param <T> the value type to check.
145      @param a the first predicate
146      @param b the second predicate
147      @return a {@code and} combination of the given predicates.
148      @throws NullPointerException if one of the given predicates is
149      *         {@code null}.
150      */
151     public static <T> Function<T, Boolean> and(
152         final Function<? super T, Boolean> a,
153         final Function<? super T, Boolean> b
154     ) {
155         requireNonNull(a);
156         requireNonNull(b);
157         return new Function<T, Boolean>() {
158             @Override public Boolean apply(final T object) {
159                 return a.apply(object&& b.apply(object);
160             }
161             @Override public String toString() {
162                 return format("%s[%s, %s]", getClass().getSimpleName(), a, b);
163             }
164         };
165     }
166 
167     /**
168      * Return a {@code or} combination of the given predicates.
169      *
170      @param <T> the value type to check.
171      @param a the first predicate
172      @param b the second predicate
173      @return a {@code and} combination of the given predicates.
174      @throws NullPointerException if one of the given predicates is
175      *          {@code null}.
176      */
177     public static <T> Function<T, Boolean> or(
178         final Function<? super T, Boolean> a,
179         final Function<? super T, Boolean> b
180     ) {
181         requireNonNull(a);
182         requireNonNull(b);
183         return new Function<T, Boolean>() {
184             @Override public Boolean apply(final T object) {
185                 return a.apply(object|| b.apply(object);
186             }
187             @Override public String toString() {
188                 return format(
189                     "%s[%s, %s]",
190                     getClass().getSimpleName(), a, b
191                 );
192             }
193         };
194     }
195 
196 
197     private static final class Identity
198         implements Function<Object, Object>, Serializable
199     {
200         private static final long serialVersionUID = 1L;
201 
202         @Override
203         public Object apply(final Object value) {
204             return value;
205         }
206     }
207 
208     private static Function<Object, Object> IDENTITY = new Identity();
209 
210     /**
211      * Return the identity function for the given type.
212      *
213      @param <T> the parameter type for the identity function
214      @return the identity function for the given type.
215      */
216     @SuppressWarnings("unchecked")
217     public static <T> Function<T, T> Identity() {
218         return (Function<T, T>)IDENTITY;
219     }
220 
221 
222     public static <A, B, C> Function<A, C> compose(
223         final Function<A, B> f1,
224         final Function<B, C> f2
225     ) {
226         requireNonNull(f1, "Function 1");
227         requireNonNull(f2, "Function 2");
228 
229         return new Function<A, C>() {
230             @Override public C apply(A value) {
231                 return f2.apply(f1.apply(value));
232             }
233         };
234     }
235 
236     public static <A, B, C, D> Function<A, D> compose(
237         final Function<A, B> f1,
238         final Function<B, C> f2,
239         final Function<C, D> f3
240     ) {
241         return compose(compose(f1, f2), f3);
242     }
243 
244     public static <A, B, C, D, E> Function<A, E> compose(
245         final Function<A, B> f1,
246         final Function<B, C> f2,
247         final Function<C, D> f3,
248         final Function<D, E> f4
249     ) {
250         return compose(compose(compose(f1, f2), f3), f4);
251     }
252 
253     public static <A, B, C, D, E, F> Function<A, F> compose(
254         final Function<A, B> f1,
255         final Function<B, C> f2,
256         final Function<C, D> f3,
257         final Function<D, E> f4,
258         final Function<E, F> f5
259     ) {
260         return compose(compose(compose(compose(f1, f2), f3), f4), f5);
261     }
262 
263 }