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.dateutil;
021    
022    import java.util.Calendar;
023    import java.util.Date;
024    
025    import org.apache.isis.applib.adapters.EncoderDecoder;
026    import org.apache.isis.applib.adapters.Parser;
027    import org.apache.isis.applib.clock.Clock;
028    import org.apache.isis.core.commons.config.IsisConfiguration;
029    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030    import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
031    import org.apache.isis.core.progmodel.facets.value.DateAndTimeValueSemanticsProviderAbstract;
032    import org.apache.isis.core.progmodel.facets.value.datesql.JavaSqlDateValueSemanticsProvider;
033    import org.apache.isis.core.progmodel.facets.value.timesql.JavaSqlTimeValueSemanticsProvider;
034    
035    /**
036     * An adapter that handles {@link java.util.Date} as both a date AND time component.
037     * 
038     * @see JavaSqlDateValueSemanticsProvider
039     * @see JavaSqlTimeValueSemanticsProvider
040     */
041    public class JavaUtilDateValueSemanticsProvider extends DateAndTimeValueSemanticsProviderAbstract<java.util.Date> {
042    
043        private static final boolean IMMUTABLE = false;
044        private static final boolean EQUAL_BY_CONTENT = false;
045    
046        /**
047         * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
048         */
049        public JavaUtilDateValueSemanticsProvider() {
050            this(null, null, null);
051        }
052    
053        public JavaUtilDateValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
054            final ValueSemanticsProviderContext context) {
055            super(holder, Date.class, IMMUTABLE, EQUAL_BY_CONTENT, configuration, context);
056        }
057    
058        @Override
059        protected Date dateValue(final Object value) {
060            return value == null ? null : (Date) value;
061        }
062    
063        @Override
064        protected Date add(final Date original, final int years, final int months, final int days, final int hours,
065            final int minutes) {
066            final Date date = original;
067            final Calendar cal = Calendar.getInstance();
068            cal.setTime(date);
069            cal.set(Calendar.SECOND, 0);
070            cal.set(Calendar.MILLISECOND, 0);
071    
072            cal.add(Calendar.YEAR, years);
073            cal.add(Calendar.MONTH, months);
074            cal.add(Calendar.DAY_OF_MONTH, days);
075            cal.add(Calendar.HOUR, hours);
076            cal.add(Calendar.MINUTE, minutes);
077    
078            return setDate(cal.getTime());
079        }
080    
081        @Override
082        protected Date now() {
083            return new Date(Clock.getTime());
084        }
085    
086        @Override
087        protected Date setDate(final Date date) {
088            return date;
089        }
090    }