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