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.uuid;
021
022import java.util.UUID;
023
024import org.apache.isis.applib.adapters.EncoderDecoder;
025import org.apache.isis.applib.adapters.Parser;
026import org.apache.isis.applib.profiles.Localization;
027import org.apache.isis.core.commons.config.IsisConfiguration;
028import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
029import org.apache.isis.core.metamodel.facetapi.Facet;
030import org.apache.isis.core.metamodel.facetapi.FacetHolder;
031import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
032import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
033
034public class UUIDValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<UUID> implements UUIDValueFacet {
035
036    public static Class<? extends Facet> type() {
037        return UUIDValueFacet.class;
038    }
039
040    public static final int TYPICAL_LENGTH = 36;
041    private static final UUID DEFAULT_VALUE = null; // no default
042
043    /**
044     * Required because implementation of {@link Parser} and
045     * {@link EncoderDecoder}.
046     */
047    public UUIDValueSemanticsProvider() {
048        this(null, null, null);
049    }
050
051    public UUIDValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
052        super(type(), holder, UUID.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context);
053    }
054
055    // //////////////////////////////////////////////////////////////////
056    // Parser
057    // //////////////////////////////////////////////////////////////////
058
059    @Override
060    protected UUID doParse(final Object context, final String entry) {
061        if (entry.trim().equals("")) {
062            return null;
063        } else {
064            return UUID.fromString(entry);
065        }
066    }
067
068    @Override
069    public String titleString(final Object object, final Localization localization) {
070        return object == null ? "" : object.toString();
071    }
072
073    @Override
074    public String titleStringWithMask(final Object object, final String usingMask) {
075        return titleString(object, null);
076    }
077
078    // //////////////////////////////////////////////////////////////////
079    // EncoderDecoder
080    // //////////////////////////////////////////////////////////////////
081
082    @Override
083    protected String doEncode(final Object object) {
084        return object.toString();
085    }
086
087    @Override
088    protected UUID doRestore(final String data) {
089        return UUID.fromString(data);
090    }
091
092    // //////////////////////////////////////////////////////////////////
093    // UuidValueFacet
094    // //////////////////////////////////////////////////////////////////
095
096    @Override
097    public UUID uuidValue(final ObjectAdapter object) {
098        return object == null ? null : (UUID) object.getObject();
099    }
100
101    @Override
102    public ObjectAdapter createValue(final UUID value) {
103        return getAdapterManager().adapterFor(value);
104    }
105
106    // /////// toString ///////
107
108    @Override
109    public String toString() {
110        return "UuidValueSemanticsProvider";
111    }
112
113}