All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.core.variable.ValueMapper Maven / Gradle / Ivy

package org.snapscript.core.variable;

import org.snapscript.core.error.InternalStateException;

public class ValueMapper {

   public static Character toCharacter(Object value) {
      return toCharacter(value, null);
   }

   public static Number toNumber(Object value) {
      return toNumber(value, null);
   }
   
   private static Character toCharacter(Object value, Exception cause) {
      try {
         return (Character)value;
      } catch(Exception e) {
         if(cause == null) {
            return (char)toNumber(value, e).intValue();
         }
         throw new InternalStateException("Conversion failure for " + value, cause);
      }
   }
   
   private static Number toNumber(Object value, Exception cause) {
      try {
         return (Number)value;
      } catch(Exception e) {
         if(cause == null) {
            return (int)toCharacter(value, e).charValue();
         }
         throw new InternalStateException("Conversion failure for " + value, cause);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy