001/* 002 * ============================================================================ 003 * Copyright © 2002-2024 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 org.apiguardian.api.API; 022import org.tquadrat.foundation.annotation.ClassVersion; 023import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException; 024import org.tquadrat.foundation.function.Getter; 025import org.tquadrat.foundation.function.Setter; 026 027import java.util.prefs.BackingStoreException; 028import java.util.prefs.Preferences; 029 030import static org.apiguardian.api.API.Status.STABLE; 031import static org.tquadrat.foundation.lang.Objects.isNull; 032import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 033 034/** 035 * The implementation of 036 * {@link PreferenceAccessor} 037 * for instance of 038 * {@link Byte}. 039 * 040 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 041 * @version $Id: ByteAccessor.java 1120 2024-03-16 09:48:00Z tquadrat $ 042 * @since 0.0.1 043 * 044 * @UMLGraph.link 045 */ 046@ClassVersion( sourceVersion = "$Id: ByteAccessor.java 1120 2024-03-16 09:48:00Z tquadrat $" ) 047@API( status = STABLE, since = "0.0.1" ) 048public final class ByteAccessor extends PreferenceAccessor<Byte> 049{ 050 /*--------------*\ 051 ====** Constructors **===================================================== 052 \*--------------*/ 053 /** 054 * Creates a new {@code ByteAccessor} instance. 055 * 056 * @param propertyName The name of the property. 057 * @param getter The property getter. 058 * @param setter The property setter. 059 */ 060 public ByteAccessor( final String propertyName, final Getter<Byte> getter, final Setter<Byte> setter ) 061 { 062 super( propertyName, getter, setter ); 063 } // ByteAccessor() 064 065 /*---------*\ 066 ====** Methods **========================================================== 067 \*---------*/ 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public final void readPreference( final Preferences node ) throws BackingStoreException, InvalidPreferenceValueException 073 { 074 requireNonNullArgument( node, "node" ); 075 final var defaultValue = getter().get(); 076 if( isNull( defaultValue ) ) 077 { 078 if( hasKey( node ) ) 079 { 080 //noinspection NumericCastThatLosesPrecision 081 setter().set( Byte.valueOf( (byte) node.getInt( getPropertyName(), 0 ) ) ); 082 } 083 else 084 { 085 setter().set( null ); 086 } 087 } 088 else 089 { 090 //noinspection NumericCastThatLosesPrecision 091 setter().set( Byte.valueOf( (byte) node.getInt( getPropertyName(), defaultValue.intValue() ) ) ); 092 } 093 } // readPreference() 094 095 /** 096 * {@inheritDoc} 097 */ 098 @Override 099 public final void writePreference( final Preferences node ) 100 { 101 requireNonNullArgument( node, "node" ); 102 final var value = getter().get(); 103 if( isNull( value ) ) 104 { 105 node.remove( getPropertyName() ); 106 } 107 else 108 { 109 node.putInt( getPropertyName(), value.byteValue()); 110 } 111 112 } // writePreference() 113} 114// class ByteAccessor 115 116/* 117 * End of File 118 */