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.harmony.pack200;
020
021import java.beans.PropertyChangeListener;
022import java.beans.PropertyChangeSupport;
023import java.io.IOException;
024import java.util.SortedMap;
025import java.util.TreeMap;
026
027/**
028 * Provides generic JavaBeans support for the Pack/UnpackAdapters
029 */
030public abstract class Pack200Adapter {
031
032    protected static final int DEFAULT_BUFFER_SIZE = 8192;
033
034    private final PropertyChangeSupport support = new PropertyChangeSupport(this);
035
036    private final SortedMap<String, String> properties = new TreeMap<>();
037
038    public void addPropertyChangeListener(final PropertyChangeListener listener) {
039        support.addPropertyChangeListener(listener);
040    }
041
042    /**
043     * Completion between 0..1.
044     *
045     * @param value Completion between 0..1.
046     * @throws IOException if the value cannot be parsed
047     */
048    protected void completed(final double value) throws IOException {
049        firePropertyChange("pack.progress", null, String.valueOf((int) (100 * value))); //$NON-NLS-1$
050    }
051
052    /**
053     * Reports a property update to listeners
054     *
055     * @param propertyName name of property being updated
056     * @param oldValue old property value
057     * @param newValue new property value
058     * @throws IOException if the values cannot be parsed
059     */
060    protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
061        support.firePropertyChange(propertyName, oldValue, newValue);
062    }
063
064    public SortedMap<String, String> properties() {
065        return properties;
066    }
067
068    public void removePropertyChangeListener(final PropertyChangeListener listener) {
069        support.removePropertyChangeListener(listener);
070    }
071}