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.object.choices.enums;
021    
022    import org.apache.isis.applib.adapters.EncoderDecoder;
023    import org.apache.isis.applib.adapters.Parser;
024    import org.apache.isis.applib.profiles.Localization;
025    import org.apache.isis.core.commons.config.IsisConfiguration;
026    import org.apache.isis.core.metamodel.facetapi.Facet;
027    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
028    import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
029    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
030    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
031    
032    public class EnumValueSemanticsProvider<T extends Enum<T>> extends ValueSemanticsProviderAndFacetAbstract<T> implements
033        EnumFacet {
034    
035        private static final boolean IMMUTABLE = true;
036        private static final boolean EQUAL_BY_CONTENT = true;
037        private static final int TYPICAL_LENGTH = 8;
038    
039        private static Class<? extends Facet> type() {
040            return EnumFacet.class;
041        }
042    
043        /**
044         * Required because {@link Parser} and {@link EncoderDecoder}.
045         */
046        public EnumValueSemanticsProvider() {
047            this(null, null, null, null);
048        }
049    
050        public EnumValueSemanticsProvider(final FacetHolder holder, final Class<T> adaptedClass,
051            final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
052            this(type(), holder, adaptedClass, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT,
053                adaptedClass.getEnumConstants()[0], configuration, context);
054        }
055    
056        private EnumValueSemanticsProvider(final Class<? extends Facet> adapterFacetType, final FacetHolder holder,
057            final Class<T> adaptedClass, final int typicalLength, final boolean immutable, final boolean equalByContent,
058            final T defaultValue, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
059            super(adapterFacetType, holder, adaptedClass, typicalLength, immutable, equalByContent, defaultValue,
060                configuration, context);
061        }
062    
063        @Override
064        protected T doParse(final Object context, final String entry) {
065            final T[] enumConstants = getAdaptedClass().getEnumConstants();
066            for (final T enumConstant : enumConstants) {
067                if (enumConstant.toString().equals(entry)) {
068                    return enumConstant;
069                }
070            }
071            throw new TextEntryParseException("Unknown enum constant '" + entry + "'");
072        }
073    
074        @Override
075        protected String doEncode(final Object object) {
076            return titleString(object, null);
077        }
078    
079        @Override
080        protected T doRestore(final String data) {
081            return doParse(null, data);
082        }
083    
084        @Override
085        protected String titleString(final Object object, final Localization localization) {
086            return object.toString();
087        }
088    
089        @Override
090        public String titleStringWithMask(final Object value, final String usingMask) {
091            return titleString(value, null);
092        }
093    
094    }