001// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package org.pgpainless.algorithm;
006
007import java.util.Map;
008import java.util.concurrent.ConcurrentHashMap;
009
010import org.bouncycastle.bcpg.CompressionAlgorithmTags;
011
012/**
013 * Enumeration of possible compression algorithms.
014 *
015 * @see <a href="https://tools.ietf.org/html/rfc4880#section-9.3">RFC4880: Compression Algorithm Tags</a>
016 */
017public enum CompressionAlgorithm {
018
019    UNCOMPRESSED   (CompressionAlgorithmTags.UNCOMPRESSED),
020    ZIP            (CompressionAlgorithmTags.ZIP),
021    ZLIB           (CompressionAlgorithmTags.ZLIB),
022    BZIP2          (CompressionAlgorithmTags.BZIP2),
023    ;
024
025    private static final Map<Integer, CompressionAlgorithm> MAP = new ConcurrentHashMap<>();
026
027    static {
028        for (CompressionAlgorithm c : CompressionAlgorithm.values()) {
029            MAP.put(c.algorithmId, c);
030        }
031    }
032
033    /**
034     * Return the {@link CompressionAlgorithm} value that corresponds to the provided numerical id.
035     * If an invalid id is provided, null is returned.
036     *
037     * @param id id
038     * @return compression algorithm
039     */
040    public static CompressionAlgorithm fromId(int id) {
041        return MAP.get(id);
042    }
043
044    private final int algorithmId;
045
046    CompressionAlgorithm(int id) {
047        this.algorithmId = id;
048    }
049
050    /**
051     * Return the numerical algorithm tag corresponding to this compression algorithm.
052     * @return id
053     */
054    public int getAlgorithmId() {
055        return algorithmId;
056    }
057}