001/*
002 * This file is part of lzo-java, an implementation of LZO in Java.
003 * https://github.com/shevek/lzo-java
004 *
005 * The Java portion of this library is:
006 * Copyright (C) 2011 Shevek <shevek@anarres.org>
007 * All Rights Reserved.
008 *
009 * The preprocessed C portion of this library is:
010 * Copyright (C) 2006-2011 Markus Franz Xaver Johannes Oberhumer
011 * All Rights Reserved.
012 *
013 * This library is free software; you can redistribute it and/or
014 * modify it under the terms of the GNU General Public License 
015 * as published by the Free Software Foundation; either version 
016 * 2 of the License, or (at your option) any later version.
017 *
018 * This library is distributed in the hope that it will be useful, 
019 * but WITHOUT ANY WARRANTY; without even the implied warranty
020 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
021 * See the GNU General Public License for more details.
022 *
023 * You should have received a copy of the GNU General Public
024 * License along with the LZO library; see the file COPYING.
025 * If not, see <http://www.gnu.org/licenses/> or write to the
026 * Free Software Foundation, Inc., 51 Franklin Street, Fifth
027 * Floor, Boston, MA 02110-1301, USA.
028
029 * As a special exception, the copyright holders of this file
030 * give you permission to link this file with independent
031 * modules to produce an executable, regardless of the license 
032 * terms of these independent modules, and to copy and distribute
033 * the resulting executable under terms of your choice, provided
034 * that you also meet, for each linked independent module, 
035 * the terms and conditions of the license of that module. An
036 * independent module is a module which is not derived from or 
037 * based on this library or file. If you modify this file, you may 
038 * extend this exception to your version of the file, but
039 * you are not obligated to do so. If you do not wish to do so,
040 * delete this exception statement from your version.
041 */
042package org.anarres.lzo;
043
044import javax.annotation.CheckForNull;
045import javax.annotation.Nonnull;
046
047/**
048 *
049 * @author shevek
050 */
051public class LzoLibrary {
052
053    private static class Inner {
054
055        private static final LzoLibrary INSTANCE = new LzoLibrary();
056    }
057
058    @Nonnull
059    public static LzoLibrary getInstance() {
060        return Inner.INSTANCE;
061    }
062
063    /**
064     * Returns a new compressor for the given algorithm. 
065     *
066     * Currently the only available constraint is {@link LzoConstraint#COMPRESSION}. 
067     * Applied to {@link LzoAlgorithm#LZO1X} algorithm, it yields an LZO1X999 compressor with a compression level of 7.
068     */
069    @Nonnull
070    public LzoCompressor newCompressor(@CheckForNull LzoAlgorithm algorithm, @CheckForNull LzoConstraint constraint) {
071        if (algorithm == null)
072            return new LzoCompressor1x_1();
073        switch (algorithm) {
074
075            case LZO1X:
076                if (constraint == null)
077                   return new LzoCompressor1x_1();                
078                else if (constraint == LzoConstraint.COMPRESSION)
079                   return new LzoCompressor1x_999(7);
080                else 
081                   throw new UnsupportedOperationException("Unsupported combination " + algorithm + "/" + constraint);
082
083            case LZO1Y:
084                if (constraint == null)
085                   return new LzoCompressor1y_1();
086                else 
087                   throw new UnsupportedOperationException("Unsupported combination " + algorithm + "/" + constraint);
088
089            default:
090                throw new UnsupportedOperationException("Unsupported algorithm " + algorithm);
091
092        }
093    }
094
095    /**
096     * Returns a new decompressor for the given algorithm.
097     * The only constraint which makes sense is {@link LzoConstraint#SAFETY}.
098     */
099    @Nonnull
100    public LzoDecompressor newDecompressor(@Nonnull LzoAlgorithm algorithm, @CheckForNull LzoConstraint constraint) {
101        if (algorithm == null)
102            throw new NullPointerException("No algorithm specified.");
103        switch (algorithm) {
104
105            case LZO1X:
106                if (constraint == LzoConstraint.SAFETY)
107                    return new LzoDecompressor1x_safe();
108                else
109                    return new LzoDecompressor1x();
110
111            case LZO1Y:
112                if (constraint == LzoConstraint.SAFETY)
113                    return new LzoDecompressor1y_safe();
114                else
115                    return new LzoDecompressor1y();
116
117            case LZO1Z:
118                if (constraint == LzoConstraint.SAFETY)
119                    return new LzoDecompressor1z_safe();
120                else
121                    return new LzoDecompressor1z();
122
123            default:
124                throw new UnsupportedOperationException("Unsupported algorithm " + algorithm);
125
126        }
127    }
128}