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 LongValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Long> implements
036        LongValueFacet {
037    
038        public static Class<? extends Facet> type() {
039            return LongValueFacet.class;
040        }
041    
042        private static final Long DEFAULT_VALUE = Long.valueOf(0L);
043        private static final int TYPICAL_LENGTH = 18;
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 LongValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Long> 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.long");
054        }
055    
056        // //////////////////////////////////////////////////////////////////
057        // Parser
058        // //////////////////////////////////////////////////////////////////
059    
060        @Override
061        protected Long doParse(final Object context, final String entry) {
062            try {
063                return Long.valueOf(format.parse(entry).longValue());
064            } catch (final ParseException e) {
065                throw new TextEntryParseException("Not a whole number " + entry, e);
066            }
067        }
068    
069        @Override
070        public String titleString(final Object value, final Localization localization) {
071            return titleString(format, value);
072        }
073    
074        @Override
075        public String titleStringWithMask(final Object value, final String usingMask) {
076            return titleString(new DecimalFormat(usingMask), value);
077        }
078    
079        // //////////////////////////////////////////////////////////////////
080        // EncoderDecoder
081        // //////////////////////////////////////////////////////////////////
082    
083        @Override
084        protected String doEncode(final Object object) {
085            return object.toString();
086        }
087    
088        @Override
089        protected Long doRestore(final String data) {
090            return new Long(data);
091        }
092    
093        // //////////////////////////////////////////////////////////////////
094        // LongValueFacet
095        // //////////////////////////////////////////////////////////////////
096    
097        @Override
098        public Long longValue(final ObjectAdapter object) {
099            return (Long) (object == null ? null : object.getObject());
100        }
101    
102        @Override
103        public ObjectAdapter createValue(final Long value) {
104            return value == null ? null : getAdapterMap().adapterFor(value);
105        }
106    
107        // // ///// toString ///////
108        @Override
109        public String toString() {
110            return "LongValueSemanticsProvider: " + format;
111        }
112    
113    }