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.timestamp;
021    
022    import java.text.DateFormat;
023    import java.util.Date;
024    import java.util.Map;
025    
026    import org.apache.isis.applib.adapters.EncoderDecoder;
027    import org.apache.isis.applib.adapters.Parser;
028    import org.apache.isis.applib.value.TimeStamp;
029    import org.apache.isis.core.commons.config.IsisConfiguration;
030    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
031    import org.apache.isis.core.metamodel.facets.object.parseable.InvalidEntryException;
032    import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
033    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
034    
035    import com.google.inject.internal.Maps;
036    
037    public 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 {@link EncoderDecoder}.
051         */
052        public TimeStampValueSemanticsProvider() {
053            this(null, null, null);
054        }
055    
056        public TimeStampValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
057            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    }