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