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.nonNull;
023
024import javax.imageio.ImageIO;
025import java.awt.image.BufferedImage;
026import java.io.ByteArrayInputStream;
027import java.io.ByteArrayOutputStream;
028import java.io.IOException;
029import java.util.prefs.Preferences;
030
031import org.apiguardian.api.API;
032import org.tquadrat.foundation.annotation.ClassVersion;
033import org.tquadrat.foundation.config.spi.InvalidPreferenceValueException;
034import org.tquadrat.foundation.function.Getter;
035import org.tquadrat.foundation.function.Setter;
036
037/**
038 *  The implementation of
039 *  {@link org.tquadrat.foundation.config.spi.prefs.PreferenceAccessor}
040 *  for instances of
041 *  {@link BufferedImage}.
042 *
043 *  @note The image will be stored to the preferences as PNG, no matter what
044 *      the original image format was!
045 *
046 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
047 *  @version $Id: ImageAccessor.java 911 2021-05-06 22:07:00Z tquadrat $
048 *  @since 0.0.1
049 *
050 *  @UMLGraph.link
051 */
052@ClassVersion( sourceVersion = "$Id: ImageAccessor.java 911 2021-05-06 22:07:00Z tquadrat $" )
053@API( status = STABLE, since = "0.0.1" )
054@SuppressWarnings( "exports" )
055public final class ImageAccessor extends BulkDataAccessorBase<BufferedImage>
056{
057        /*--------------*\
058    ====** Constructors **=====================================================
059        \*--------------*/
060    /**
061     *  Creates a new {@code MapAccessor} instance.
062     *
063     *  @param  propertyName    The name of the property.
064     *  @param  getter  The property getter.
065     *  @param  setter  The property setter.
066     */
067    public ImageAccessor( final String propertyName, final Getter<BufferedImage> getter, final Setter<BufferedImage> setter )
068    {
069        super( propertyName, getter, setter );
070    }   //  ImageAccessor()
071
072        /*---------*\
073    ====** Methods **==========================================================
074        \*---------*/
075    /**
076     *  {@inheritDoc}
077     */
078    @Override
079    protected final BufferedImage fromByteArray( final Preferences node, final byte [] source ) throws InvalidPreferenceValueException
080    {
081        BufferedImage retValue = null;
082        if( nonNull( source ) )
083        {
084            try( final var inputStream = new ByteArrayInputStream( source ) )
085            {
086                retValue = ImageIO.read( inputStream );
087            }
088            catch( final IOException e )
089            {
090                throw new InvalidPreferenceValueException( node, getPropertyName(), e );
091            }
092        }
093
094        //---* Done *----------------------------------------------------------
095        return retValue;
096    }   //  fromByteArray()
097
098    /**
099     *  {@inheritDoc}
100     */
101    @Override
102    protected final byte [] toByteArray( final Preferences node, final BufferedImage source ) throws InvalidPreferenceValueException
103    {
104        byte [] retValue = null;
105        if( nonNull( source ) )
106        {
107            final var outputStream = new ByteArrayOutputStream();
108            try( outputStream )
109            {
110                ImageIO.write( source, "PNG", outputStream );
111            }
112            catch( final IOException e )
113            {
114                throw new InvalidPreferenceValueException( node, getPropertyName(), e );
115            }
116            retValue = outputStream.toByteArray();
117        }
118
119        //---* Done *----------------------------------------------------------
120        return retValue;
121    }   //  toByteArray()
122}
123//  class ImageAccessor
124
125/*
126 *  End of File
127 */