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.chars;
021
022import java.text.DecimalFormat;
023
024import org.apache.isis.applib.profiles.Localization;
025import org.apache.isis.core.commons.config.IsisConfiguration;
026import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
027import org.apache.isis.core.metamodel.facetapi.Facet;
028import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029import org.apache.isis.core.metamodel.facets.object.parseable.InvalidEntryException;
030import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
031import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
032
033public abstract class CharValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Character> implements CharValueFacet {
034
035    private static Class<? extends Facet> type() {
036        return CharValueFacet.class;
037    }
038
039    private static final Character DEFAULT_VALUE = Character.valueOf((char) 0);
040    private static final int TYPICAL_LENGTH = 1;
041
042    public CharValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Character> adaptedClass, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
043        super(type(), holder, adaptedClass, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context);
044    }
045
046    // //////////////////////////////////////////////////////////////////
047    // Parser
048    // //////////////////////////////////////////////////////////////////
049
050    @Override
051    public Character doParse(final Object context, final String entry) {
052        if (entry.length() > 1) {
053            throw new InvalidEntryException("Only a single character is required");
054        } else {
055            return Character.valueOf(entry.charAt(0));
056        }
057    }
058
059    @Override
060    public String titleString(final Object value, final Localization localization) {
061        return value == null ? "" : value.toString();
062    }
063
064    @Override
065    public String titleStringWithMask(final Object value, final String usingMask) {
066        return titleString(new DecimalFormat(usingMask), value);
067    }
068
069    // //////////////////////////////////////////////////////////////////
070    // EncoderDecoder
071    // //////////////////////////////////////////////////////////////////
072
073    @Override
074    protected String doEncode(final Object object) {
075        return object.toString();
076    }
077
078    @Override
079    protected Character doRestore(final String data) {
080        return Character.valueOf(data.charAt(0));
081    }
082
083    // //////////////////////////////////////////////////////////////////
084    // CharValueFacet
085    // //////////////////////////////////////////////////////////////////
086
087    @Override
088    public Character charValue(final ObjectAdapter object) {
089        return object == null ? null : (Character) object.getObject();
090    }
091
092    @Override
093    public ObjectAdapter createValue(final Character value) {
094        return getAdapterManager().adapterFor(value);
095    }
096
097    // /////// toString ///////
098
099    @Override
100    public String toString() {
101        return "CharacterValueSemanticsProvider";
102    }
103
104}