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