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.requireNonNullArgument; 023import static org.tquadrat.foundation.util.StringUtils.isNotEmpty; 024 025import java.util.prefs.Preferences; 026 027import org.apiguardian.api.API; 028import org.tquadrat.foundation.annotation.ClassVersion; 029import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException; 030import org.tquadrat.foundation.function.Getter; 031import org.tquadrat.foundation.function.Setter; 032 033/** 034 * The implementation of 035 * {@link PreferenceAccessor} 036 * for instances of 037 * {@link String}. 038 * 039 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 040 * @version $Id: StringAccessor.java 914 2021-05-07 21:22:12Z tquadrat $ 041 * @since 0.0.1 042 * 043 * @UMLGraph.link 044 */ 045@ClassVersion( sourceVersion = "$Id: StringAccessor.java 914 2021-05-07 21:22:12Z tquadrat $" ) 046@API( status = STABLE, since = "0.0.1" ) 047public final class StringAccessor extends PreferenceAccessor<String> 048{ 049 /*--------------*\ 050 ====** Constructors **===================================================== 051 \*--------------*/ 052 /** 053 * Creates a new {@code StringAccessor} instance. 054 * 055 * @param propertyName The name of the property. 056 * @param getter The property getter. 057 * @param setter The property setter. 058 */ 059 public StringAccessor( final String propertyName, final Getter<String> getter, final Setter<String> setter ) 060 { 061 super( propertyName, getter, setter ); 062 } // StringAccessor() 063 064 /*---------*\ 065 ====** Methods **========================================================== 066 \*---------*/ 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public final void readPreference( final Preferences node ) throws InvalidPreferenceValueException 072 { 073 setter().set( requireNonNullArgument( node, "node" ).get( getPropertyName(), getter().get() ) ); 074 } // readPreference() 075 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public final void writePreference( final Preferences node ) 081 { 082 requireNonNullArgument( node, "node" ); 083 final var value = getter().get(); 084 if( isNotEmpty( value ) ) 085 { 086 node.put( getPropertyName(), value ); 087 } 088 else 089 { 090 node.remove( getPropertyName() ); 091 } 092 } // writePreference() 093} 094// class StringAccessor 095 096/* 097 * End of File 098 */