001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.sevenz;
019
020import java.util.Objects;
021
022/**
023 * Combines a SevenZMethod with configuration options for the method.
024 *
025 * <p>The exact type and interpretation of options depends on the
026 * method being configured.  Currently supported are:</p>
027 *
028 * <table>
029 * <caption>Options</caption>
030 * <tr><th>Method</th><th>Option Type</th><th>Description</th></tr>
031 * <tr><td>BZIP2</td><td>Number</td><td>Block Size - an number between 1 and 9</td></tr>
032 * <tr><td>DEFLATE</td><td>Number</td><td>Compression Level - an number between 1 and 9</td></tr>
033 * <tr><td>LZMA2</td><td>Number</td><td>Dictionary Size - a number between 4096 and 768 MiB (768 &lt;&lt; 20)</td></tr>
034 * <tr><td>LZMA2</td><td>org.tukaani.xz.LZMA2Options</td><td>Whole set of LZMA2 options.</td></tr>
035 * <tr><td>DELTA_FILTER</td><td>Number</td><td>Delta Distance - a number between 1 and 256</td></tr>
036 * </table>
037 *
038 * @Immutable
039 * @since 1.8
040 */
041public class SevenZMethodConfiguration {
042    private final SevenZMethod method;
043    private final Object options;
044
045    /**
046     * Doesn't configure any additional options.
047     * @param method the method to use
048     */
049    public SevenZMethodConfiguration(final SevenZMethod method) {
050        this(method, null);
051    }
052
053    /**
054     * Specifies and method plus configuration options.
055     * @param method the method to use
056     * @param options the options to use
057     * @throws IllegalArgumentException if the method doesn't understand the options specified.
058     */
059    public SevenZMethodConfiguration(final SevenZMethod method, final Object options) {
060        this.method = method;
061        this.options = options;
062        if (options != null && !Coders.findByMethod(method).canAcceptOptions(options)) {
063            throw new IllegalArgumentException("The " + method + " method doesn't support options of type "
064                                               + options.getClass());
065        }
066    }
067
068    /**
069     * The specified method.
070     * @return the method
071     */
072    public SevenZMethod getMethod() {
073        return method;
074    }
075
076    /**
077     * The specified options.
078     * @return the options
079     */
080    public Object getOptions() {
081        return options;
082    }
083
084    @Override
085    public int hashCode() {
086        return method == null ? 0 : method.hashCode();
087    }
088
089    @Override
090    public boolean equals(final Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (obj == null || getClass() != obj.getClass()) {
095            return false;
096        }
097        final SevenZMethodConfiguration other = (SevenZMethodConfiguration) obj;
098        return Objects.equals(method, other.method)
099            && Objects.equals(options, other.options);
100    }
101}