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