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