001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric.netty.fileserver;
016    
017    import java.util.EnumSet;
018    import java.util.HashMap;
019    import java.util.Map;
020    import java.util.zip.Deflater;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public enum CompressionLevel {
026    
027            BEST_COMPRESSION(Deflater.BEST_COMPRESSION),
028            BEST_SPEED(Deflater.BEST_SPEED),
029            DEFAULT_COMPRESSION(Deflater.DEFAULT_COMPRESSION), LEVEL_2(2), LEVEL_3(3),
030            LEVEL_4(4), LEVEL_5(5), LEVEL_6(6), LEVEL_7(7), LEVEL_8(8),
031            NO_COMPRESSION(Deflater.NO_COMPRESSION);
032    
033            public static CompressionLevel getCompressionLevel(int level) {
034                    CompressionLevel compressionLevel = _compressionLevels.get(level);
035    
036                    if (compressionLevel == null) {
037                            throw new IllegalArgumentException(
038                                    "Compression level " + level +
039                                            " is not within the range of -1 and 9");
040                    }
041    
042                    return compressionLevel;
043            }
044    
045            public int getLevel() {
046                    return _level;
047            }
048    
049            private CompressionLevel(int level) {
050                    _level = level;
051            }
052    
053            private static final Map<Integer, CompressionLevel> _compressionLevels =
054                    new HashMap<>();
055    
056            static {
057                    for (CompressionLevel compressionLevel : EnumSet.allOf(
058                                    CompressionLevel.class)) {
059    
060                            _compressionLevels.put(compressionLevel._level, compressionLevel);
061                    }
062            }
063    
064            private final int _level;
065    
066    }