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 */
017package org.apache.commons.compress.archivers.sevenz;
018
019import java.util.Objects;
020
021/**
022 * Combines a SevenZMethod with configuration options for the method.
023 *
024 * <p>The exact type and interpretation of options depends on the
025 * method being configured.  Currently supported are:</p>
026 *
027 * <table>
028 * <caption>Options</caption>
029 * <tr><th>Method</th><th>Option Type</th><th>Description</th></tr>
030 * <tr><td>BZIP2</td><td>Number</td><td>Block Size - an number between 1 and 9</td></tr>
031 * <tr><td>DEFLATE</td><td>Number</td><td>Compression Level - an number between 1 and 9</td></tr>
032 * <tr><td>LZMA2</td><td>Number</td><td>Dictionary Size - a number between 4096 and 768 MiB (768 &lt;&lt; 20)</td></tr>
033 * <tr><td>LZMA2</td><td>org.tukaani.xz.LZMA2Options</td><td>Whole set of LZMA2 options.</td></tr>
034 * <tr><td>DELTA_FILTER</td><td>Number</td><td>Delta Distance - a number between 1 and 256</td></tr>
035 * </table>
036 *
037 * @Immutable
038 * @since 1.8
039 */
040public class SevenZMethodConfiguration {
041    private final SevenZMethod method;
042    private final Object options;
043
044    /**
045     * Doesn't configure any additional options.
046     * @param method the method to use
047     */
048    public SevenZMethodConfiguration(final SevenZMethod method) {
049        this(method, null);
050    }
051
052    /**
053     * Specifies and method plus configuration options.
054     * @param method the method to use
055     * @param options the options to use
056     * @throws IllegalArgumentException if the method doesn't understand the options specified.
057     */
058    public SevenZMethodConfiguration(final SevenZMethod method, final Object options) {
059        this.method = method;
060        this.options = options;
061        if (options != null && !Coders.findByMethod(method).isOptionInstance(options)) {
062            throw new IllegalArgumentException("The " + method + " method doesn't support options of type "
063                                               + options.getClass());
064        }
065    }
066
067    @Override
068    public boolean equals(final Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        if (obj == null || getClass() != obj.getClass()) {
073            return false;
074        }
075        final SevenZMethodConfiguration other = (SevenZMethodConfiguration) obj;
076        return Objects.equals(method, other.method)
077            && Objects.equals(options, other.options);
078    }
079
080    /**
081     * The specified method.
082     * @return the method
083     */
084    public SevenZMethod getMethod() {
085        return method;
086    }
087
088    /**
089     * The specified options.
090     * @return the options
091     */
092    public Object getOptions() {
093        return options;
094    }
095
096    @Override
097    public int hashCode() {
098        return method == null ? 0 : method.hashCode();
099    }
100}