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