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.timesql; 021 022import java.sql.Time; 023import java.text.DateFormat; 024import java.util.Calendar; 025import java.util.Date; 026import java.util.Map; 027 028import com.google.common.collect.Maps; 029 030import org.apache.isis.applib.adapters.EncoderDecoder; 031import org.apache.isis.applib.adapters.Parser; 032import org.apache.isis.applib.clock.Clock; 033import org.apache.isis.core.commons.config.IsisConfiguration; 034import org.apache.isis.core.metamodel.facetapi.FacetHolder; 035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 036import org.apache.isis.core.progmodel.facets.value.time.TimeValueSemanticsProviderAbstract; 037 038/** 039 * Treats {@link java.sql.Time} as a time-only value type. 040 * 041 */ 042public class JavaSqlTimeValueSemanticsProvider extends TimeValueSemanticsProviderAbstract<java.sql.Time> { 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 JavaSqlTimeValueSemanticsProvider() { 054 this(null, null, null); 055 } 056 057 public JavaSqlTimeValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 058 super(holder, java.sql.Time.class, configuration, context); 059 } 060 061 @Override 062 public Time add(final Time original, final int years, final int months, final int days, final int hours, final int minutes) { 063 final java.sql.Time time = original; 064 final Calendar cal = Calendar.getInstance(); 065 cal.setTime(time); 066 cal.set(Calendar.YEAR, 0); 067 cal.set(Calendar.MONTH, 0); 068 cal.set(Calendar.DAY_OF_MONTH, 0); 069 cal.set(Calendar.MILLISECOND, 0); 070 071 cal.add(Calendar.HOUR, hours); 072 cal.add(Calendar.MINUTE, minutes); 073 074 return setDate(cal.getTime()); 075 } 076 077 @Override 078 public java.util.Date dateValue(final Object object) { 079 final java.sql.Time time = (Time) object; 080 return time == null ? null : new java.util.Date(time.getTime()); 081 } 082 083 @Override 084 protected Map<String, DateFormat> formats() { 085 return formats; 086 } 087 088 @Override 089 protected Time now() { 090 return new java.sql.Time(Clock.getTime()); 091 } 092 093 @Override 094 protected Time setDate(final Date date) { 095 return new java.sql.Time(date.getTime()); 096 } 097 098}