001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.progmodel.facets.value.bigdecimal; 021 022import java.math.BigDecimal; 023import java.text.DecimalFormat; 024import java.text.NumberFormat; 025import java.util.Locale; 026 027import org.apache.isis.applib.adapters.EncoderDecoder; 028import org.apache.isis.applib.adapters.Parser; 029import org.apache.isis.applib.profiles.Localization; 030import org.apache.isis.core.commons.config.IsisConfiguration; 031import org.apache.isis.core.commons.exceptions.IsisException; 032import org.apache.isis.core.metamodel.facetapi.Facet; 033import org.apache.isis.core.metamodel.facetapi.FacetHolder; 034import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException; 035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract; 036import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 037 038public class BigDecimalValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<BigDecimal> implements BigDecimalValueFacet { 039 040 private static Class<? extends Facet> type() { 041 return BigDecimalValueFacet.class; 042 } 043 044 private static final int TYPICAL_LENGTH = 10; 045 private static final BigDecimal DEFAULT_VALUE = new BigDecimal(0); 046 047 public static final int DEFAULT_LENGTH = 18; 048 public static final int DEFAULT_SCALE = 2; 049 050 private final NumberFormat format; 051 052 /** 053 * Required because implementation of {@link Parser} and 054 * {@link EncoderDecoder}. 055 */ 056 public BigDecimalValueSemanticsProvider() { 057 this(null, null, null); 058 } 059 060 public BigDecimalValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 061 super(type(), holder, BigDecimal.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context); 062 format = determineNumberFormat("value.format.decimal"); 063 } 064 065 public void setLocale(final Locale l) { 066 067 } 068 069 // ////////////////////////////////////////////////////////////////// 070 // Parser 071 // ////////////////////////////////////////////////////////////////// 072 073 @Deprecated 074 @Override 075 public Integer getLength() { 076 return getPrecision(); 077 } 078 079 @Override 080 public Integer getPrecision() { 081 return DEFAULT_LENGTH; 082 } 083 084 @Override 085 public Integer getScale() { 086 return DEFAULT_SCALE; 087 } 088 089 // ////////////////////////////////////////////////////////////////// 090 // Parser 091 // ////////////////////////////////////////////////////////////////// 092 093 @Override 094 protected BigDecimal doParse(final Object context, final String entry) { 095 try { 096 return new BigDecimal(entry); 097 } catch (final NumberFormatException e) { 098 throw new TextEntryParseException("Not an decimal " + entry, e); 099 } 100 } 101 102 @Override 103 public String titleString(final Object object, final Localization localization) { 104 return titleString(format, object); 105 } 106 107 @Override 108 public String titleStringWithMask(final Object value, final String usingMask) { 109 return titleString(new DecimalFormat(usingMask), value); 110 } 111 112 // ////////////////////////////////////////////////////////////////// 113 // EncoderDecoder 114 // ////////////////////////////////////////////////////////////////// 115 116 @Override 117 protected String doEncode(final Object object) { 118 // for dotnet compatibility - toString pre 1.3 was equivalent to 119 // toPlainString later. 120 try { 121 final Class<?> type = object.getClass(); 122 try { 123 return (String) type.getMethod("toPlainString", (Class[]) null).invoke(object, (Object[]) null); 124 } catch (final NoSuchMethodException nsm) { 125 return (String) type.getMethod("toString", (Class[]) null).invoke(object, (Object[]) null); 126 } 127 } catch (final Exception e) { 128 throw new IsisException(e); 129 } 130 131 } 132 133 @Override 134 protected BigDecimal doRestore(final String data) { 135 return new BigDecimal(data); 136 } 137 138 // /////// toString /////// 139 140 @Override 141 public String toString() { 142 return "BigDecimalValueSemanticsProvider: " + format; 143 } 144 145 146}