001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.datatype.adapter;
028
029import com.fasterxml.jackson.core.JsonGenerator;
030import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
031
032import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
033import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
034import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
035import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDecimalItem;
036import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
037import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
038import gov.nist.secauto.metaschema.core.util.ObjectUtils;
039
040import java.io.IOException;
041import java.math.BigDecimal;
042import java.math.MathContext;
043import java.util.List;
044
045import edu.umd.cs.findbugs.annotations.NonNull;
046
047public class DecimalAdapter
048    extends AbstractDataTypeAdapter<BigDecimal, IDecimalItem> {
049  public static final MathContext MATH_CONTEXT = MathContext.DECIMAL64;
050  @NonNull
051  private static final BigDecimal DECIMAL_BOOLEAN_TRUE = new BigDecimal("1.0");
052  @NonNull
053  private static final BigDecimal DECIMAL_BOOLEAN_FALSE = new BigDecimal("0.0");
054  @NonNull
055  private static final List<String> NAMES = ObjectUtils.notNull(
056      List.of("decimal"));
057
058  DecimalAdapter() {
059    super(BigDecimal.class);
060  }
061
062  @Override
063  public List<String> getNames() {
064    return NAMES;
065  }
066
067  @Override
068  public JsonFormatTypes getJsonRawType() {
069    return JsonFormatTypes.NUMBER;
070  }
071
072  @Override
073  public BigDecimal parse(String value) {
074    return new BigDecimal(value, MATH_CONTEXT);
075  }
076
077  @Override
078  public void writeJsonValue(Object value, JsonGenerator generator) throws IOException {
079    try {
080      generator.writeNumber((BigDecimal) value);
081    } catch (ClassCastException ex) {
082      throw new IOException(ex);
083    }
084  }
085
086  @Override
087  public BigDecimal copy(Object obj) {
088    // a BigDecimal is immutable
089    return (BigDecimal) obj;
090  }
091
092  @Override
093  public Class<IDecimalItem> getItemClass() {
094    return IDecimalItem.class;
095  }
096
097  @Override
098  public IDecimalItem newItem(Object value) {
099    BigDecimal item = toValue(value);
100    return IDecimalItem.valueOf(item);
101  }
102
103  @Override
104  protected IDecimalItem castInternal(@NonNull IAnyAtomicItem item) {
105    IDecimalItem retval;
106    if (item instanceof INumericItem) {
107      if (item instanceof IDecimalItem) {
108        retval = (IDecimalItem) item;
109      } else {
110        // must be an integer type
111        retval = newItem(((IIntegerItem) item).asDecimal());
112      }
113    } else if (item instanceof IBooleanItem) {
114      boolean value = ((IBooleanItem) item).toBoolean();
115      retval = newItem(value ? DECIMAL_BOOLEAN_TRUE : DECIMAL_BOOLEAN_FALSE);
116    } else {
117      retval = super.castInternal(item);
118    }
119    return retval;
120  }
121}