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.bytes;
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 ByteValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Byte> implements
036        ByteValueFacet {
037    
038        private static Class<? extends Facet> type() {
039            return ByteValueFacet.class;
040        }
041    
042        private static final Byte DEFAULT_VALUE = Byte.valueOf((byte) 0);
043    
044        private static final int TYPICAL_LENGTH = 4;
045        private static final boolean IMMUTABLE = true;
046        private static final boolean EQUAL_BY_CONTENT = true;
047    
048        private final NumberFormat format;
049    
050        public ByteValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Byte> adaptedClass,
051            final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
052            super(type(), holder, adaptedClass, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, DEFAULT_VALUE, configuration,
053                context);
054            format = determineNumberFormat("value.format.byte");
055        }
056    
057        // //////////////////////////////////////////////////////////////////
058        // Parser
059        // //////////////////////////////////////////////////////////////////
060    
061        @Override
062        protected Byte doParse(final Object context, final String entry) {
063            try {
064                return Byte.valueOf(format.parse(entry).byteValue());
065            } catch (final ParseException e) {
066                throw new TextEntryParseException("Not a number " + entry, e);
067            }
068        }
069    
070        @Override
071        public String titleString(final Object value, final Localization localization) {
072            return titleString(format, value);
073        }
074    
075        // //////////////////////////////////////////////////////////////////
076        // EncoderDecoder
077        // //////////////////////////////////////////////////////////////////
078    
079        @Override
080        public String doEncode(final Object object) {
081            return object.toString();
082        }
083    
084        @Override
085        protected Byte doRestore(final String data) {
086            return new Byte(data);
087        }
088    
089        @Override
090        public String titleStringWithMask(final Object value, final String usingMask) {
091            return titleString(new DecimalFormat(usingMask), value);
092        }
093    
094        // //////////////////////////////////////////////////////////////////
095        // ByteValueFacet
096        // //////////////////////////////////////////////////////////////////
097    
098        @Override
099        public Byte byteValue(final ObjectAdapter object) {
100            return (Byte) object.getObject();
101        }
102    
103        @Override
104        public ObjectAdapter createValue(final Byte value) {
105            return getAdapterMap().adapterFor(value);
106        }
107    
108        // ///// toString ///////
109    
110        @Override
111        public String toString() {
112            return "ByteValueSemanticsProvider: " + format;
113        }
114    
115    }