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.time;
021
022 import java.text.DateFormat;
023 import java.util.Calendar;
024 import java.util.Map;
025
026 import org.apache.isis.core.commons.config.ConfigurationConstants;
027 import org.apache.isis.core.commons.config.IsisConfiguration;
028 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029 import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
030 import org.apache.isis.core.progmodel.facets.value.ValueSemanticsProviderAbstractTemporal;
031
032 public abstract class TimeValueSemanticsProviderAbstract<T> extends ValueSemanticsProviderAbstractTemporal<T> {
033
034 private static final Object DEFAULT_VALUE = null; // no default
035 private static final int TYPICAL_LENGTH = 6;
036
037 private static final boolean IMMUTABLE = false;
038 private static final boolean EQUAL_BY_CONTENT = false;
039
040 protected static void initFormats(final Map<String, DateFormat> formats) {
041 formats.put("iso", createDateFormat("HH:mm"));
042 formats.put("iso_sec", createDateFormat("HH:mm:ss"));
043 formats.put("iso_milli", createDateFormat("HH:mm:ss.SSS"));
044 formats.put(ISO_ENCODING_FORMAT, createDateFormat("HHmmssSSS"));
045 formats.put("long", DateFormat.getTimeInstance(DateFormat.LONG));
046 formats.put("medium", DateFormat.getTimeInstance(DateFormat.MEDIUM));
047 formats.put("short", DateFormat.getTimeInstance(DateFormat.SHORT));
048 }
049
050 @SuppressWarnings("unchecked")
051 public TimeValueSemanticsProviderAbstract(final FacetHolder holder, final Class<T> adaptedClass,
052 final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
053 super("time", holder, adaptedClass, TYPICAL_LENGTH, IMMUTABLE, EQUAL_BY_CONTENT, (T) DEFAULT_VALUE,
054 configuration, context);
055
056 final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.time");
057 if (formatRequired == null) {
058 format = formats().get(defaultFormat());
059 } else {
060 setMask(formatRequired);
061 }
062 }
063
064 // //////////////////////////////////////////////////////////////////
065 // DateValueFacet
066 // //////////////////////////////////////////////////////////////////
067
068 @Override
069 public int getLevel() {
070 return TIME_ONLY;
071 }
072
073 // //////////////////////////////////////////////////////////////////
074 // temporal-specific stuff
075 // //////////////////////////////////////////////////////////////////
076
077 @Override
078 protected void clearFields(final Calendar cal) {
079 cal.set(Calendar.YEAR, 1970);
080 cal.set(Calendar.MONTH, 0);
081 cal.set(Calendar.DAY_OF_MONTH, 1);
082 }
083
084 @Override
085 protected String defaultFormat() {
086 return "short";
087 }
088
089 @Override
090 public String toString() {
091 return "TimeValueSemanticsProvider: " + format;
092 }
093
094 }