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.Preferences;
028
029import static org.apiguardian.api.API.Status.STABLE;
030import static org.tquadrat.foundation.lang.Objects.isNull;
031import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
032
033/**
034 *  <p>{@summary The implementation of
035 *  {@link PreferenceAccessor}
036 *  for {@code byte}.}</p>
037 *  <p>This differs from
038 *  {@link ByteAccessor}
039 *  as it forces 0 (zero) as the default value, while the other
040 *  implementation has {@code null} as the default value.</p>
041 *
042 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
043 *  @version $Id: PrimitiveByteAccessor.java 1120 2024-03-16 09:48:00Z tquadrat $
044 *  @since 0.0.1
045 *
046 *  @UMLGraph.link
047 */
048@ClassVersion( sourceVersion = "$Id: PrimitiveByteAccessor.java 1120 2024-03-16 09:48:00Z tquadrat $" )
049@API( status = STABLE, since = "0.0.1" )
050public final class PrimitiveByteAccessor extends PreferenceAccessor<Byte>
051{
052        /*--------------*\
053    ====** Constructors **=====================================================
054        \*--------------*/
055    /**
056     *  Creates a new {@code PrimitiveByteAccessor} instance.
057     *
058     *  @param  propertyName    The name of the property.
059     *  @param  getter  The property getter.
060     *  @param  setter  The property setter.
061     */
062    public PrimitiveByteAccessor( final String propertyName, final Getter<Byte> getter, final Setter<Byte> setter )
063    {
064        super( propertyName, getter, setter );
065    }   //  PrimitiveByteAccessor()
066
067        /*---------*\
068    ====** Methods **==========================================================
069        \*---------*/
070    /**
071     *  {@inheritDoc}
072     */
073    @Override
074    public final void readPreference( final Preferences node ) throws InvalidPreferenceValueException
075    {
076        final var defaultValue = getter().get();
077        //noinspection NumericCastThatLosesPrecision
078        setter().set( Byte.valueOf( (byte) requireNonNullArgument( node, "node" ).getInt( getPropertyName(), isNull( defaultValue ) ? 0 : defaultValue.intValue() ) ) );
079    }   //  readPreference()
080
081    /**
082     *  {@inheritDoc}
083     */
084    @Override
085    public final void writePreference( final Preferences node )
086    {
087        node.putInt( getPropertyName(), getter().get().byteValue());
088    }   //  writePreference()
089}
090//  class PrimitiveByteAccessor
091
092/*
093 *  End of File
094 */