001/*
002 * ============================================================================
003 *  Copyright © 2002-2021 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.CommonConstants.UTF8;
022import static org.tquadrat.foundation.lang.Objects.isNull;
023import static org.tquadrat.foundation.lang.Objects.nonNull;
024import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
025
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;
033import org.tquadrat.foundation.lang.StringConverter;
034
035/**
036 *  <p>{@summary The abstract implementation of
037 *  {@link BulkDataAccessorBase}
038 *  for bulk data where an instance of
039 *  {@link StringConverter}
040 *  can be provided for the property data type.}</p>
041 *  <p>The method
042 *  {@link Preferences#put(String, String) Preferences.put()}
043 *  limits the length for a value String to
044 *  {@value Preferences#MAX_VALUE_LENGTH}. If more data should be stored, the
045 *  method
046 *  {@link Preferences#putByteArray(String, byte[]) Preferences.putByteArray()}
047 *  can be used.</p>
048 *
049 *  @param  <T> The type of the property.
050 *
051 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
052 *  @version $Id: SimpleBulkDataAccessor.java 914 2021-05-07 21:22:12Z tquadrat $
053 *  @since 0.1.0
054 *
055 *  @UMLGraph.link
056 */
057@ClassVersion( sourceVersion = "$Id: SimpleBulkDataAccessor.java 914 2021-05-07 21:22:12Z tquadrat $" )
058@API( status = STABLE, since = "0.0.1" )
059public abstract class SimpleBulkDataAccessor<T> extends BulkDataAccessorBase<T>
060{
061        /*------------*\
062    ====** Attributes **=======================================================
063        \*------------*/
064    /**
065     *   The implementation of
066     *   {@link StringConverter}
067     *   that is used to convert the preference values back and force.
068     */
069    private final StringConverter<T> m_StringConverter;
070
071        /*--------------*\
072    ====** Constructors **=====================================================
073        \*--------------*/
074    /**
075     *  Creates a new {@code SimpleBulkDataAccessor} instance.
076     *
077     *  @param  propertyName    The name of the property.
078     *  @param  getter  The property getter.
079     *  @param  setter  The property setter.
080     *  @param  stringConverter The implementation of
081     *      {@link StringConverter}
082     *      that is used to convert the preference values back and force.
083     */
084    protected SimpleBulkDataAccessor( final String propertyName, final Getter<T> getter, final Setter<T> setter, final StringConverter<T> stringConverter )
085    {
086        super( propertyName, getter, setter );
087        m_StringConverter = requireNonNullArgument( stringConverter, "stringConverter" );
088    }   //  SimpleBulkDataAccessor()
089
090        /*---------*\
091    ====** Methods **==========================================================
092        \*---------*/
093    /**
094     *  {@inheritDoc}
095     */
096    @Override
097    protected final T fromByteArray( @SuppressWarnings( "unused" ) final Preferences node, final byte [] source) throws InvalidPreferenceValueException
098    {
099        T retValue = null;
100        if( nonNull( source ) )
101        {
102            final var string = new String( source, UTF8 );
103            retValue = m_StringConverter.fromString( string );
104        }
105
106        //---* Done *----------------------------------------------------------
107        return retValue;
108    }   //  fromByteArray()
109
110    /**
111     *  {@inheritDoc}
112     */
113    @Override
114    protected final byte [] toByteArray( @SuppressWarnings( "unused" ) final Preferences node, final T source ) throws InvalidPreferenceValueException
115    {
116        final var retValue = isNull( source ) ? null : m_StringConverter.toString( source ).getBytes( UTF8 );
117
118        //---* Done *----------------------------------------------------------
119        return retValue;
120    }   //  toByteArray()
121}
122//  class SimpleBulkDataAccessor
123
124/*
125 *  End of File
126 */