001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import org.apache.juneau.internal.*;
016
017/**
018 * Used to convert property values to standardized Boolean/Integer/Class/Object values in property store builders.
019 *
020 * @param <T> The normalized form.
021 */
022public interface PropertyConverter<T> {
023
024   /**
025    * Convert the value to normalized form.
026    *
027    * @param o The raw value.
028    * @return The converted value.
029    */
030   T convert(Object o);
031
032   /**
033    * Converts objects to strings.
034    */
035   static final PropertyConverter<String> STRING_CONVERTER = new PropertyConverter<String>() {
036      @Override
037      public String convert(Object o) {
038         return ClassUtils.toString(o);
039      }
040   };
041
042   /**
043    * Converts objects to integers.
044    */
045   static final PropertyConverter<Integer> INTEGER_CONVERTER = new PropertyConverter<Integer>() {
046      @Override
047      public Integer convert(Object o) {
048         try {
049            if (o instanceof Integer)
050               return (Integer)o;
051            return Integer.valueOf(o.toString());
052         } catch (Exception e) {
053            throw new ConfigException("Value ''{0}'' ({1}) cannot be converted to an Integer.", o, o.getClass().getSimpleName());
054         }
055      }
056   };
057
058   /**
059    * Converts objects to booleans.
060    */
061   static final PropertyConverter<Boolean> BOOLEAN_CONVERTER = new PropertyConverter<Boolean>() {
062      @Override
063      public Boolean convert(Object o) {
064         if (o instanceof Boolean)
065            return (Boolean)o;
066         return Boolean.parseBoolean(o.toString());
067      }
068   };
069
070   /**
071    * Converts objects to classes.
072    */
073   static final PropertyConverter<Class<?>> CLASS_CONVERTER = new PropertyConverter<Class<?>>() {
074      @Override
075      public Class<?> convert(Object o) {
076         try {
077            if (o instanceof Class)
078               return (Class<?>)o;
079            throw new ConfigException("Value ''{0}'' ({1}) cannot be converted to a Class.", o, o.getClass().getSimpleName());
080         } catch (Exception e) {
081            throw new ConfigException("Value ''{0}'' ({1}) cannot be converted to a Class.", o, o.getClass().getSimpleName());
082         }
083      }
084   };
085
086   /**
087    * Converts objects to objects.
088    */
089   static final PropertyConverter<Object> OBJECT_CONVERTER = new PropertyConverter<Object>() {
090      @Override
091      public Object convert(Object o) {
092         return o;
093      }
094   };
095}