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.booleans;
021    
022    import org.apache.isis.applib.profiles.Localization;
023    import org.apache.isis.core.commons.config.IsisConfiguration;
024    import org.apache.isis.core.commons.exceptions.IsisException;
025    import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
026    import org.apache.isis.core.metamodel.adapter.util.AdapterUtils;
027    import org.apache.isis.core.metamodel.facetapi.Facet;
028    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029    import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
030    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
031    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
032    
033    public abstract class BooleanValueSemanticsProviderAbstract extends ValueSemanticsProviderAndFacetAbstract<Boolean>
034        implements BooleanValueFacet {
035    
036        private static Class<? extends Facet> type() {
037            return BooleanValueFacet.class;
038        }
039    
040        private static final int TYPICAL_LENGTH = 5;
041        private static final boolean IMMUTABLE = true;
042        private static final boolean EQUAL_BY_CONTENT = true;
043    
044        public BooleanValueSemanticsProviderAbstract(final FacetHolder holder, final Class<Boolean> adaptedClass,
045            final Boolean defaultValue, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
046            super(type(), holder, adaptedClass, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, defaultValue, configuration,
047                context);
048        }
049    
050        // //////////////////////////////////////////////////////////////////
051        // Parsing
052        // //////////////////////////////////////////////////////////////////
053    
054        @Override
055        protected Boolean doParse(final Object context, final String entry) {
056            final String compareTo = entry.trim().toLowerCase();
057            if ("true".equals(compareTo)) {
058                return Boolean.TRUE;
059            } else if ("false".startsWith(compareTo)) {
060                return Boolean.FALSE;
061            } else {
062                throw new TextEntryParseException("Not a logical value " + entry);
063            }
064        }
065    
066        @Override
067        public String titleString(final Object value, final Localization localization) {
068            return value == null ? "" : isSet(value) ? "True" : "False";
069        }
070    
071        @Override
072        public String titleStringWithMask(final Object value, final String usingMask) {
073            return titleString(value, null);
074        }
075    
076        // //////////////////////////////////////////////////////////////////
077        // Encode, Decode
078        // //////////////////////////////////////////////////////////////////
079    
080        @Override
081        protected String doEncode(final Object object) {
082            return isSet(object) ? "T" : "F";
083        }
084    
085        @Override
086        protected Boolean doRestore(final String data) {
087            if (data.length() != 1) {
088                throw new IsisException("Invalid data for logical, expected 1 byte, got " + data.length());
089            }
090            switch (data.charAt(0)) {
091                case 'T':
092                    return Boolean.TRUE;
093                case 'F':
094                    return Boolean.FALSE;
095                default:
096                    throw new IsisException("Invalid data for logical, expected 'T', 'F' or 'N, but got " + data.charAt(0));
097            }
098        }
099    
100        private boolean isSet(final Object value) {
101            return ((Boolean) value).booleanValue();
102        }
103    
104        // //////////////////////////////////////////////////////////////////
105        // BooleanValueFacet
106        // //////////////////////////////////////////////////////////////////
107    
108        @Override
109        public boolean isSet(final ObjectAdapter adapter) {
110            if (!AdapterUtils.exists(adapter)) {
111                return false;
112            }
113            final Object object = adapter.getObject();
114            final Boolean objectAsBoolean = (Boolean) object;
115            return objectAsBoolean.booleanValue();
116        }
117    
118        // /////// toString ///////
119    
120        @Override
121        public String toString() {
122            return "BooleanValueSemanticsProvider";
123        }
124    
125    }