001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keystore.prop;
019    
020    /**
021     * {@link Property} implementation for the value type {@link Long}.
022     *
023     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
024     */
025    public class LongProperty extends Property<Long>
026    {
027            /**
028             * {@inheritDoc}
029             * <p>
030             * The implementation in <code>LongProperty</code> returns either <code>null</code> or 8 bytes containing
031             * the long value in a pretty raw form (copied byte-by-byte).
032             * </p>
033             */
034            @Override
035            public byte[] getValueEncoded()
036            {
037                    Long value = getValue();
038                    if (value == null)
039                            return null;
040                    else {
041                            long val = value;
042                            byte[] result = new byte[8];
043                            for (int i = 0; i < result.length; ++ i)
044                                    result[i] = (byte)(val >>> (i * 8));
045    
046                            return result;
047                    }
048            }
049    
050            /**
051             * {@inheritDoc}
052             * @throws IllegalArgumentException if the <code>encodedValue</code> is neither <code>null</code> nor a byte-array
053             * with a length of exactly 8.
054             */
055            @Override
056            public void setValueEncoded(byte[] encodedValue)
057            throws IllegalArgumentException
058            {
059                    if (encodedValue == null)
060                            setValue(null);
061                    else {
062                            if (encodedValue.length != 8)
063                                    throw new IllegalArgumentException("encodedValue.length != 8 :: encodedValue must either be null or an array with the correct length!");
064    
065                            long val = 0;
066                            for (int i = 0; i < encodedValue.length; ++ i)
067                                    val |= ((long)encodedValue[i] & 0xff) << (i * 8);
068    
069                            setValue(val);
070                    }
071            }
072    
073    }