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.timestamp;
021
022import java.text.DateFormat;
023import java.util.Date;
024import java.util.Map;
025
026import com.google.common.collect.Maps;
027
028import org.apache.isis.applib.adapters.EncoderDecoder;
029import org.apache.isis.applib.adapters.Parser;
030import org.apache.isis.applib.value.TimeStamp;
031import org.apache.isis.core.commons.config.IsisConfiguration;
032import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033import org.apache.isis.core.metamodel.facets.object.parseable.InvalidEntryException;
034import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
036
037public class TimeStampValueSemanticsProvider extends TimeStampValueSemanticsProviderAbstract<TimeStamp> {
038
039    public static final boolean isAPropertyDefaultFacet() {
040        return PropertyDefaultFacet.class.isAssignableFrom(TimeStampValueSemanticsProvider.class);
041    }
042
043    private static Map<String, DateFormat> formats = Maps.newHashMap();
044
045    static {
046        initFormats(formats);
047    }
048
049    /**
050     * Required because implementation of {@link Parser} and
051     * {@link EncoderDecoder}.
052     */
053    public TimeStampValueSemanticsProvider() {
054        this(null, null, null);
055    }
056
057    public TimeStampValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
058        super(holder, TimeStamp.class, configuration, context);
059    }
060
061    // //////////////////////////////////////////////////////////////////
062    // temporal-specific stuff
063    // //////////////////////////////////////////////////////////////////
064
065    @Override
066    protected Date dateValue(final Object value) {
067        return new Date(((TimeStamp) value).longValue());
068    }
069
070    @Override
071    protected Map<String, DateFormat> formats() {
072        return formats;
073    }
074
075    @Override
076    protected TimeStamp now() {
077        throw new InvalidEntryException("Can't change a timestamp.");
078    }
079
080    @Override
081    protected TimeStamp setDate(final Date date) {
082        return new TimeStamp(date.getTime());
083    }
084
085}