001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.misc;
025    
026    import microsoft.exchange.webservices.data.core.EwsUtilities;
027    import org.apache.commons.codec.binary.Base64;
028    
029    import java.util.Date;
030    import java.util.UUID;
031    
032    /**
033     * Class with re-usable function implementations.
034     */
035    
036    public final class IFunctions {
037    
038      private IFunctions() {
039        throw new UnsupportedOperationException();
040      }
041    
042      public static class ToString implements IFunction<Object, String> {
043        public static final ToString INSTANCE = new ToString();
044    
045        public String func(final Object o) {
046          return String.valueOf(o);
047        }
048      }
049    
050      public static class ToBoolean implements IFunction<String, Object> {
051        public static final ToBoolean INSTANCE = new ToBoolean();
052    
053        public Boolean func(final String s) {
054          return Boolean.parseBoolean(s);
055        }
056      }
057    
058      public static class StringToObject implements IFunction<String, Object> {
059        public static final StringToObject INSTANCE = new StringToObject();
060    
061        public Object func(final String o) {
062          return o;
063        }
064      }
065    
066      public static class ToUUID implements IFunction<String, Object> {
067        public static final ToUUID INSTANCE = new ToUUID();
068    
069        public Object func(final String s) {
070          return UUID.fromString(s);
071        }
072      }
073    
074      public static class Base64Decoder implements IFunction<String, Object> {
075        public static final Base64Decoder INSTANCE = new Base64Decoder();
076    
077        public Object func(final String s) {
078          return Base64.decodeBase64(s);
079        }
080      }
081    
082      public static class Base64Encoder implements IFunction<Object, String> {
083        public static final Base64Encoder INSTANCE = new Base64Encoder();
084    
085        public String func(final Object o) {
086          return Base64.encodeBase64String((byte[]) o);
087        }
088      }
089    
090      public static class ToLowerCase implements IFunction<Object, String> {
091        public static final ToLowerCase INSTANCE = new ToLowerCase();
092    
093        public String func(final Object o) {
094          return o == null ? null : o.toString().toLowerCase();
095        }
096      }
097    
098      public static class DateTimeToXSDateTime implements IFunction<Object, String> {
099        public static final DateTimeToXSDateTime INSTANCE = new DateTimeToXSDateTime();
100    
101        public String func(final Object o) {
102          return EwsUtilities.dateTimeToXSDateTime((Date) o);
103        }
104      }
105    
106    }