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.datetimejoda;
021
022import java.util.Date;
023
024import org.joda.time.DateTime;
025
026import org.apache.isis.applib.adapters.EncoderDecoder;
027import org.apache.isis.applib.adapters.Parser;
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;
031
032public class JodaDateTimeValueSemanticsProvider extends JodaDateTimeValueSemanticsProviderAbstract<DateTime> {
033
034    // no default
035    private static final DateTime DEFAULT_VALUE = null;
036
037
038    /**
039     * Required because implementation of {@link Parser} and
040     * {@link EncoderDecoder}.
041     */
042    public JodaDateTimeValueSemanticsProvider() {
043        this(null, null, null);
044    }
045
046    public JodaDateTimeValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
047        super(holder, DateTime.class, DEFAULT_VALUE, configuration, context);
048    }
049
050    @Override
051    protected DateTime add(final DateTime original, final int years, final int months, final int days, final int hours, final int minutes) {
052        if(hours != 0 || minutes != 0) {
053            throw new IllegalArgumentException("cannot add non-zero hours or minutes to a DateTime");
054        }
055        return original.plusYears(years).plusMonths(months).plusDays(days);
056    }
057
058    @Override
059    protected DateTime now() {
060        return new DateTime();
061    }
062
063    @Override
064    protected Date dateValue(final Object value) {
065        return ((DateTime) value).toDateTime().toDate();
066    }
067
068    @Override
069    protected DateTime setDate(final Date date) {
070        return new DateTime(date.getTime());
071    }
072}