001/* 002 * ============================================================================ 003 * Copyright © 2002-2021 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * 007 * Licensed to the public under the agreements of the GNU Lesser General Public 008 * License, version 3.0 (the "License"). You may obtain a copy of the License at 009 * 010 * http://www.gnu.org/licenses/lgpl.html 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 015 * License for the specific language governing permissions and limitations 016 * under the License. 017 */ 018 019package org.tquadrat.foundation.config.spi.prefs; 020 021import static org.apiguardian.api.API.Status.STABLE; 022import static org.tquadrat.foundation.lang.Objects.isNull; 023import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 024 025import java.util.Date; 026import java.util.prefs.BackingStoreException; 027import java.util.prefs.Preferences; 028 029import org.apiguardian.api.API; 030import org.tquadrat.foundation.annotation.ClassVersion; 031import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException; 032import org.tquadrat.foundation.function.Getter; 033import org.tquadrat.foundation.function.Setter; 034 035/** 036 * <p>{@summary The implementation of 037 * {@link PreferenceAccessor} 038 * for instances of 039 * {@link Date}.}</p> 040 * <p>As instances of {@code Date} do not keep a time zone, and because there 041 * is no easy (and reliable for all locales) way to convert them to and from 042 * Strings, they are stored to the preferences as {@code long} values.</p> 043 * <p>If it is desired/required to store human readable time/date values to 044 * the preferences, the classes from the package 045 * {@link java.time} 046 * should be considered for the property type.</p> 047 * 048 * @see Date#Date(long) 049 * @see Date#getTime() 050 * 051 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 052 * @version $Id: DateAccessor.java 910 2021-05-06 21:38:06Z tquadrat $ 053 * @since 0.0.1 054 * 055 * @UMLGraph.link 056 */ 057@SuppressWarnings( "UseOfObsoleteDateTimeApi" ) 058@ClassVersion( sourceVersion = "$Id: DateAccessor.java 910 2021-05-06 21:38:06Z tquadrat $" ) 059@API( status = STABLE, since = "0.0.1" ) 060public final class DateAccessor extends PreferenceAccessor<Date> 061{ 062 /*--------------*\ 063 ====** Constructors **===================================================== 064 \*--------------*/ 065 /** 066 * Creates a new {@code DateAccessor} instance. 067 * 068 * @param propertyName The name of the property. 069 * @param getter The property getter. 070 * @param setter The property setter. 071 */ 072 public DateAccessor( final String propertyName, final Getter<Date> getter, final Setter<Date> setter ) 073 { 074 super( propertyName, getter, setter ); 075 } // DateAccessor() 076 077 /*---------*\ 078 ====** Methods **========================================================== 079 \*---------*/ 080 /** 081 * {@inheritDoc} 082 */ 083 @Override 084 public final void readPreference( final Preferences node ) throws BackingStoreException, InvalidPreferenceValueException 085 { 086 requireNonNullArgument( node, "node" ); 087 088 final var defaultValue = getter().get(); 089 Date propertyValue = null; 090 if( isNull( defaultValue ) ) 091 { 092 if( hasKey( node ) ) 093 { 094 propertyValue = new Date( node.getLong( getPropertyName(), 0 ) ); 095 } 096 } 097 else 098 { 099 propertyValue = new Date( node.getLong( getPropertyName(), defaultValue.getTime() ) ); 100 } 101 setter().set( propertyValue ); 102 } // readPreference() 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override 108 public final void writePreference( final Preferences node ) throws BackingStoreException 109 { 110 requireNonNullArgument( node, "node" ); 111 final var propertyValue = getter().get(); 112 if( isNull( propertyValue ) ) 113 { 114 node.remove( getPropertyName() ); 115 } 116 else 117 { 118 node.putLong( getPropertyName(), propertyValue.getTime() ); 119 } 120 } // writePreference() 121} 122// class DateAccessor 123 124/* 125 * End of File 126 */