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.shortint;
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 class ShortValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Short> implements
036 ShortValueFacet {
037
038 public static Class<? extends Facet> type() {
039 return ShortValueFacet.class;
040 }
041
042 private static final Short DEFAULT_VALUE = Short.valueOf((short) 0);
043 private static final int TYPICAL_LENGTH = 6;
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 ShortValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Short> 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.short");
054 }
055
056 // //////////////////////////////////////////////////////////////////
057 // Parser
058 // //////////////////////////////////////////////////////////////////
059
060 @Override
061 protected Short doParse(final Object context, final String entry) {
062 try {
063 return Short.valueOf(format.parse(entry).shortValue());
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 Short doRestore(final String data) {
090 return new Short(data);
091 }
092
093 // //////////////////////////////////////////////////////////////////
094 // ShortValueFacet
095 // //////////////////////////////////////////////////////////////////
096
097 @Override
098 public ObjectAdapter createValue(final Short value) {
099 return getAdapterMap().adapterFor(value);
100 }
101
102 @Override
103 public Short shortValue(final ObjectAdapter object) {
104 return (Short) (object == null ? null : object.getObject());
105 }
106
107 // /////// toString ///////
108
109 @Override
110 public String toString() {
111 return "ShortValueSemanticsProvider: " + format;
112 }
113
114 }