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.longs; 021 022import java.text.DecimalFormat; 023import java.text.NumberFormat; 024import java.text.ParseException; 025 026import org.apache.isis.applib.profiles.Localization; 027import org.apache.isis.core.commons.config.IsisConfiguration; 028import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 029import org.apache.isis.core.metamodel.facetapi.Facet; 030import org.apache.isis.core.metamodel.facetapi.FacetHolder; 031import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException; 032import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract; 033import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 034 035public abstract class DoubleValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Double> implements DoubleFloatingPointValueFacet { 036 037 private static Class<? extends Facet> type() { 038 return DoubleFloatingPointValueFacet.class; 039 } 040 041 private static final Double DEFAULT_VALUE = new Double(0.0d); 042 private static final int TYPICAL_LENGTH = 10; 043 044 private final NumberFormat format; 045 046 public DoubleValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Double> adaptedClass, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 047 super(type(), holder, adaptedClass, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context); 048 format = determineNumberFormat("value.format.double"); 049 } 050 051 // ////////////////////////////////////////////////////////////////// 052 // Parser 053 // ////////////////////////////////////////////////////////////////// 054 055 @Override 056 protected Double doParse(final Object context, final String entry) { 057 try { 058 return new Double(format.parse(entry).doubleValue()); 059 } catch (final ParseException e) { 060 throw new TextEntryParseException("Not floating point number " + entry, e); 061 } 062 } 063 064 // /////////////////////////////////////////////////////////////////////////// 065 // TitleProvider 066 // /////////////////////////////////////////////////////////////////////////// 067 068 @Override 069 public String titleString(final Object value, final Localization localization) { 070 return titleString(format, value); 071 } 072 073 @Override 074 public String titleStringWithMask(final Object value, final String usingMask) { 075 return titleString(new DecimalFormat(usingMask), value); 076 } 077 078 // ////////////////////////////////////////////////////////////////// 079 // EncoderDecoder 080 // ////////////////////////////////////////////////////////////////// 081 082 @Override 083 protected String doEncode(final Object object) { 084 return object.toString(); 085 } 086 087 @Override 088 protected Double doRestore(final String data) { 089 return new Double(data); 090 } 091 092 // ////////////////////////////////////////////////////////////////// 093 // DoubleValueFacet 094 // ////////////////////////////////////////////////////////////////// 095 096 @Override 097 public Double doubleValue(final ObjectAdapter object) { 098 return (Double) (object == null ? null : object.getObject()); 099 } 100 101 @Override 102 public ObjectAdapter createValue(final Double value) { 103 return getAdapterManager().adapterFor(value); 104 } 105 106 // /////// toString /////// 107 @Override 108 public String toString() { 109 return "DoubleValueSemanticsProvider: " + format; 110 } 111 112}