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
020 package org.apache.isis.core.progmodel.facets.value.biginteger;
021
022 import java.math.BigInteger;
023 import java.text.DecimalFormat;
024 import java.text.NumberFormat;
025
026 import org.apache.isis.applib.adapters.EncoderDecoder;
027 import org.apache.isis.applib.adapters.Parser;
028 import org.apache.isis.applib.profiles.Localization;
029 import org.apache.isis.core.commons.config.IsisConfiguration;
030 import org.apache.isis.core.metamodel.facetapi.Facet;
031 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
032 import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
033 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
034 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
035
036 public class BigIntegerValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<BigInteger> implements
037 BigIntegerValueFacet {
038
039 private static final int TYPICAL_LENGTH = 19;
040
041 private static Class<? extends Facet> type() {
042 return BigIntegerValueFacet.class;
043 }
044
045 private static final boolean IMMUTABLE = true;
046 private static final boolean EQUAL_BY_CONTENT = true;
047 private static final BigInteger DEFAULT_VALUE = BigInteger.valueOf(0);
048
049 private final NumberFormat format;
050
051 /**
052 * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
053 */
054 public BigIntegerValueSemanticsProvider() {
055 this(null, null, null);
056 }
057
058 public BigIntegerValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
059 final ValueSemanticsProviderContext context) {
060
061 super(type(), holder, BigInteger.class, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, DEFAULT_VALUE,
062 configuration, context);
063 format = determineNumberFormat("value.format.int");
064 }
065
066 // //////////////////////////////////////////////////////////////////
067 // Parser
068 // //////////////////////////////////////////////////////////////////
069
070 @Override
071 protected BigInteger doParse(final Object context, final String entry) {
072 try {
073 return new BigInteger(entry);
074 } catch (final NumberFormatException e) {
075 throw new TextEntryParseException("Not an integer " + entry, e);
076 }
077 }
078
079 @Override
080 public String titleString(final Object object, final Localization localization) {
081 return titleString(format, object);
082 }
083
084 @Override
085 public String titleStringWithMask(final Object value, final String usingMask) {
086 return titleString(new DecimalFormat(usingMask), value);
087 }
088
089 // //////////////////////////////////////////////////////////////////
090 // EncoderDecoder
091 // //////////////////////////////////////////////////////////////////
092
093 @Override
094 protected String doEncode(final Object object) {
095 return object.toString();
096 }
097
098 @Override
099 protected BigInteger doRestore(final String data) {
100 return new BigInteger(data);
101 }
102
103 // /////// toString ///////
104
105 @Override
106 public String toString() {
107 return "BigIntegerValueSemanticsProvider: " + format;
108 }
109
110 }