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.prefs.BackingStoreException;
026import java.util.prefs.Preferences;
027
028import org.apiguardian.api.API;
029import org.tquadrat.foundation.annotation.ClassVersion;
030import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException;
031import org.tquadrat.foundation.function.Getter;
032import org.tquadrat.foundation.function.Setter;
033
034/**
035 *  The implementation of
036 *  {@link PreferenceAccessor}
037 *  for instances of
038 *  {@link Integer}.
039 *
040 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
041 *  @version $Id: IntegerAccessor.java 911 2021-05-06 22:07:00Z tquadrat $
042 *  @since 0.0.1
043 *
044 *  @UMLGraph.link
045 */
046@ClassVersion( sourceVersion = "$Id: IntegerAccessor.java 911 2021-05-06 22:07:00Z tquadrat $" )
047@API( status = STABLE, since = "0.0.1" )
048public final class IntegerAccessor extends PreferenceAccessor<Integer>
049{
050        /*--------------*\
051    ====** Constructors **=====================================================
052        \*--------------*/
053    /**
054     *  Creates a new {@code IntegerAccessor} 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 IntegerAccessor( final String propertyName, final Getter<Integer> getter, final Setter<Integer> setter )
061    {
062        super( propertyName, getter, setter );
063    }   //  IntegerAccessor()
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                setter().set( Integer.valueOf( node.getInt( getPropertyName(), Integer.MIN_VALUE ) ) );
081            }
082            else
083            {
084                setter().set( null );
085            }
086        }
087        else
088        {
089            setter().set( Integer.valueOf( node.getInt( getPropertyName(), defaultValue.intValue() ) ) );
090        }
091    }   //  readPreference()
092
093    /**
094     *  {@inheritDoc}
095     */
096    @Override
097    public final void writePreference( final Preferences node )
098    {
099        requireNonNullArgument( node, "node" );
100        final var propertyValue = getter().get();
101        if( isNull( propertyValue ) )
102        {
103            node.remove( getPropertyName() );
104        }
105        else
106        {
107            node.putInt( getPropertyName(), propertyValue.intValue() );
108        }
109    }   //  writePreference()
110}
111//  class IntegerAccessor
112
113/*
114 *  End of File
115 */