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