001/* 002 * ============================================================================ 003 * Copyright © 2002-2023 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.config.spi.prefs; 019 020import static org.apiguardian.api.API.Status.STABLE; 021import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 022 023import java.util.prefs.Preferences; 024 025import org.apiguardian.api.API; 026import org.tquadrat.foundation.annotation.ClassVersion; 027import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException; 028import org.tquadrat.foundation.function.Getter; 029import org.tquadrat.foundation.function.Setter; 030import org.tquadrat.foundation.lang.StringConverter; 031 032/** 033 * The implementations of 034 * {@link PreferenceAccessor} 035 * that uses the given instance of 036 * {@link StringConverter} 037 * to translate the values back and forth. 038 * 039 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 040 * @version $Id: SimplePreferenceAccessor.java 1061 2023-09-25 16:32:43Z tquadrat $ 041 * @since 0.1.0 042 * 043 * @param <T> The type of the property value. 044 * 045 * @UMLGraph.link 046 */ 047@ClassVersion( sourceVersion = "$Id: SimplePreferenceAccessor.java 1061 2023-09-25 16:32:43Z tquadrat $" ) 048@API( status = STABLE, since = "0.1.0" ) 049public sealed class SimplePreferenceAccessor<T> extends PreferenceAccessorBase<T> 050 permits EnumAccessor 051{ 052 /*------------*\ 053 ====** Attributes **======================================================= 054 \*------------*/ 055 /** 056 * The implementation of 057 * {@link StringConverter} 058 * that is used to convert the preference values back and force. 059 */ 060 private final StringConverter<T> m_StringConverter; 061 062 /*--------------*\ 063 ====** Constructors **===================================================== 064 \*--------------*/ 065 /** 066 * Creates a new {@code SimplePreferenceAccessor} instance. 067 * 068 * @param propertyName The name of the property. 069 * @param getter The property getter. 070 * @param setter The property setter. 071 * @param stringConverter The implementation of 072 * {@link StringConverter} 073 * that is used to convert the preference values back and forth. 074 */ 075 public SimplePreferenceAccessor( final String propertyName, final Getter<T> getter, final Setter<T> setter, final StringConverter<T> stringConverter ) 076 { 077 super( propertyName, getter, setter ); 078 m_StringConverter = requireNonNullArgument( stringConverter, "stringConverter" ); 079 } // SimplePreferenceAccessorBase() 080 081 /*---------*\ 082 ====** Methods **========================================================== 083 \*---------*/ 084 /** 085 * {@inheritDoc} 086 */ 087 @Override 088 protected final T fromString( final Preferences node, final String s ) throws InvalidPreferenceValueException 089 { 090 final T retValue; 091 try 092 { 093 retValue = m_StringConverter.fromString( s ); 094 } 095 catch( final IllegalArgumentException e ) 096 { 097 throw new InvalidPreferenceValueException( node, getPropertyName(), s, e ); 098 } 099 //---* Done *---------------------------------------------------------- 100 return retValue; 101 } // fromString() 102 103 /** 104 * {@inheritDoc} 105 */ 106 @Override 107 protected String toString( final T t ) 108 { 109 final var retValue = m_StringConverter.toString( t ); 110 111 //---* Done *---------------------------------------------------------- 112 return retValue; 113 } // toString() 114} 115// class SimplePreferenceAccessor 116 117/* 118 * End of File 119 */