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

com.hfg.util.ValueImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;


//------------------------------------------------------------------------------
/**
 Simple implementation of the Value interface.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class ValueImpl implements Value { private String mStringValue; private Number mNumberValue; private Boolean mBooleanValue; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public ValueImpl(Object inValue) { if (inValue != null) { if (inValue instanceof Number) { mNumberValue = (Number) inValue; } else if (inValue instanceof Boolean) { mBooleanValue = (Boolean) inValue; } else { mStringValue = inValue.toString(); } } } //--------------------------------------------------------------------------- public ValueImpl(String inValue) { mStringValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(Number inValue) { mNumberValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(boolean inValue) { mBooleanValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(int inValue) { mNumberValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(long inValue) { mNumberValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(float inValue) { mNumberValue = inValue; } //--------------------------------------------------------------------------- public ValueImpl(double inValue) { mNumberValue = inValue; } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public boolean isNull() { return (null == mStringValue && null == mNumberValue && null == mBooleanValue); } //--------------------------------------------------------------------------- @Override public String toString() { return stringValue(); } //--------------------------------------------------------------------------- @Override public String stringValue() { String stringValue = null; if (mStringValue != null) { stringValue = mStringValue; } else if (mNumberValue != null) { stringValue = mNumberValue.toString(); } else if (mBooleanValue != null) { stringValue = mBooleanValue.toString(); } return stringValue; } //--------------------------------------------------------------------------- @Override public Boolean booleanValue() { Boolean booleanValue = null; if (mBooleanValue != null) { booleanValue = mBooleanValue; } else if (mNumberValue != null) { booleanValue = BooleanUtil.valueOf(mNumberValue); } else if (mStringValue != null) { booleanValue = BooleanUtil.valueOf(mStringValue); } return booleanValue; } //--------------------------------------------------------------------------- @Override public Integer intValue() { Integer intValue = null; if (mNumberValue != null) { intValue = mNumberValue.intValue(); } else if (mBooleanValue != null) { intValue = (mBooleanValue ? 1 : 0); } else if (mStringValue != null) { try { intValue = Integer.valueOf(mStringValue); } catch (Exception e) { // Ignore } } return intValue; } //--------------------------------------------------------------------------- @Override public Long longValue() { Long longValue = null; if (mNumberValue != null) { longValue = mNumberValue.longValue(); } else if (mBooleanValue != null) { longValue = (mBooleanValue ? 1L : 0L); } else if (mStringValue != null) { try { longValue = Long.valueOf(mStringValue); } catch (Exception e) { // Ignore } } return longValue; } //--------------------------------------------------------------------------- @Override public Float floatValue() { Float floatValue = null; if (mNumberValue != null) { floatValue = mNumberValue.floatValue(); } else if (mBooleanValue != null) { floatValue = (mBooleanValue ? 1f : 0f); } else if (mStringValue != null) { try { floatValue = Float.valueOf(mStringValue); } catch (Exception e) { // Ignore } } return floatValue; } //--------------------------------------------------------------------------- @Override public Double doubleValue() { Double doubleValue = null; if (mNumberValue != null) { doubleValue = mNumberValue.doubleValue(); } else if (mBooleanValue != null) { doubleValue = (mBooleanValue ? 1d : 0d); } else if (mStringValue != null) { try { doubleValue = Double.valueOf(mStringValue); } catch (Exception e) { // Ignore } } return doubleValue; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy