001 /* 002 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//xmlio/src/java/org/apache/commons/xmlio/in/ConversionHelpers.java,v 1.1 2004/10/08 11:56:20 ozeigermann Exp $ 003 * $Revision: 155476 $ 004 * $Date: 2005-02-26 13:31:24 +0000 (Sat, 26 Feb 2005) $ 005 * 006 * ==================================================================== 007 * 008 * Copyright 2004 The Apache Software Foundation 009 * 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * 022 */ 023 024 package org.apache.commons.xmlio.in; 025 026 /** 027 * Collection of some simple conversion and fallback methods for convenience. 028 * 029 */ 030 public class ConversionHelpers { 031 032 /** Returns <code>value</code> if not null, otherwise <code>fallBack</code>. 033 */ 034 public static String getString(String value, String fallBack) { 035 if (value == null) 036 return fallBack; 037 return value; 038 } 039 040 /** Gets int value from a string value. 041 * @param value string value to get int from 042 * @return int representation of value or <code>-1</code> 043 * if it can not be converted to an int 044 */ 045 public static int getInt(String value) { 046 return getInt(value, -1); 047 } 048 049 /** Gets int value from a string value. 050 * @param value string value to get int from 051 * @param fallBack fall back value 052 * @return int representation of value or <code>fallBack</code> 053 * if it can not be converted to an int 054 */ 055 public static int getInt(String value, int fallBack) { 056 if (value == null) 057 return fallBack; 058 try { 059 return Integer.valueOf(value).intValue(); 060 } catch (NumberFormatException nfe) { 061 return fallBack; 062 } 063 } 064 065 /** Gets long value from a string value. 066 * @param value string value to get long from 067 * @return long representation of value or <code>-1L</code> 068 * if it can not be converted to a long 069 */ 070 public static long getLong(String value) { 071 return getLong(value, -1L); 072 } 073 074 /** Gets long value from a string value. 075 * @param value string value to get long from 076 * @param fallBack fall back value 077 * @return long representation of value or <code>fallBack</code> 078 * if it can not be converted to a long 079 */ 080 public static long getLong(String value, long fallBack) { 081 if (value == null) 082 return fallBack; 083 try { 084 return Long.valueOf(value).longValue(); 085 } catch (NumberFormatException nfe) { 086 return fallBack; 087 } 088 } 089 090 /** Gets boolean value from a string value. 091 * @param value string value to get boolean from 092 * @param fallBack fall back value 093 * @return boolean representation of value <code>fallBack</code> 094 * if it can not <em>properly</em> be converted to a boolean 095 */ 096 public static boolean getBoolean(String value, boolean fallBack) { 097 if (value == null) 098 return fallBack; 099 // do not use "Boolean.valueOf(" as this returns false on everything 100 // but "true" 101 if ("true".equalsIgnoreCase(value)) 102 return true; 103 if ("false".equalsIgnoreCase(value)) 104 return false; 105 return fallBack; 106 } 107 }