001/* 002 * ============================================================================ 003 * Copyright © 2002-2026 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.jsonbuilder.internal; 019 020import static org.apiguardian.api.API.Status.INTERNAL; 021import static org.tquadrat.foundation.lang.Objects.hash; 022import static org.tquadrat.foundation.lang.Objects.requireNotBlankArgument; 023 024import java.math.BigDecimal; 025import java.math.BigInteger; 026import java.util.Formatter; 027 028import org.apiguardian.api.API; 029import org.tquadrat.foundation.annotation.ClassVersion; 030import org.tquadrat.foundation.exception.ValidationException; 031import org.tquadrat.foundation.jsonbuilder.JSONNumber; 032 033/** 034 * <p>{@summary The implementation of the interface 035 * {@link JSONNumber}.}</p> 036 * 037 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 038 * @version $Id: JSONNumberImpl.java 1190 2026-04-08 13:27:20Z tquadrat $ 039 * @since 0.25.0 040 * 041 * @UMLGraph.link 042 */ 043@ClassVersion( sourceVersion = "$Id: JSONNumberImpl.java 1190 2026-04-08 13:27:20Z tquadrat $" ) 044@API( status = INTERNAL, since = "0.25.0" ) 045public final class JSONNumberImpl implements JSONNumber 046{ 047 /*------------*\ 048 ====** Attributes **======================================================= 049 \*------------*/ 050 /** 051 * The value. 052 */ 053 private final String m_Value; 054 055 /*--------------*\ 056 ====** Constructors **===================================================== 057 \*--------------*/ 058 /** 059 * Creates a new instance of {@link JSONNumber}. 060 * 061 * @param value The value. 062 */ 063 public JSONNumberImpl( final String value ) 064 { 065 m_Value = validateNumber( value ); 066 } // JSONNumberImpl() 067 068 /*---------*\ 069 ====** Methods **========================================================== 070 \*---------*/ 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 public final boolean equals( final Object o ) 076 { 077 var retValue = this == o; 078 if( !retValue && o instanceof final JSONNumberImpl other ) 079 { 080 retValue = m_Value.equals( other.m_Value ); 081 } 082 //---* Done *---------------------------------------------------------- 083 return retValue; 084 } // equals() 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public final void formatTo( final Formatter formatter, final int flags, final int width, final int precision ) 091 { 092 formatter.format( m_Value ); 093 } // formatTo() 094 095 /** 096 * {@inheritDoc} 097 */ 098 @Override 099 public final BigDecimal getBigDecimal() throws NumberFormatException 100 { 101 final var retValue = new BigDecimal( m_Value ); 102 103 //---* Done *---------------------------------------------------------- 104 return retValue; 105 } // getBigDecimal() 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public final BigInteger getBigInteger() throws NumberFormatException 112 { 113 final var retValue = new BigInteger( m_Value ); 114 115 //---* Done *---------------------------------------------------------- 116 return retValue; 117 } // getBigInteger() 118 119 /** 120 * {@inheritDoc} 121 */ 122 @Override 123 public final double getDouble() throws NumberFormatException 124 { 125 final var retValue = Double.parseDouble( m_Value ); 126 127 //---* Done *---------------------------------------------------------- 128 return retValue; 129 } // getDouble() 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public final float getFloat() throws NumberFormatException 136 { 137 final var retValue = Float.parseFloat( m_Value ); 138 139 //---* Done *---------------------------------------------------------- 140 return retValue; 141 } // getFloat() 142 143 /** 144 * {@inheritDoc} 145 */ 146 @Override 147 public final int getInt() throws NumberFormatException 148 { 149 final var retValue = Integer.parseInt( m_Value ); 150 151 //---* Done *---------------------------------------------------------- 152 return retValue; 153 } // getInt() 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public final long getLong() throws NumberFormatException 160 { 161 final var retValue = Long.parseLong( m_Value ); 162 163 //---* Done *---------------------------------------------------------- 164 return retValue; 165 } // getLong() 166 167 /** 168 * {@inheritDoc} 169 */ 170 @Override 171 public int hashCode() { return hash( m_Value ); } 172 173 /** 174 * Validates whether the given value is a valid numerical value. 175 * 176 * @param value The value. 177 * @return The given value. 178 * @throws ValidationException The given value is not a valid number. 179 */ 180 private static final String validateNumber( final String value ) 181 { 182 try 183 { 184 //noinspection ResultOfObjectAllocationIgnored 185 new BigDecimal( requireNotBlankArgument( value, "value" ) ); 186 } 187 catch( final NumberFormatException e ) 188 { 189 throw new ValidationException( "The given value '%s' is not a valid number".formatted( value ), e ); 190 } 191 192 //---* Done *---------------------------------------------------------- 193 return value; 194 } // validateNumber() 195 196 /** 197 * {@inheritDoc} 198 */ 199 @Override 200 public final String toString() { return m_Value; } 201} 202// class JSONNumberImpl 203 204/* 205 * End of File 206 */