Pack200Adapter.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.harmony.pack200;

  18. import java.beans.PropertyChangeListener;
  19. import java.beans.PropertyChangeSupport;
  20. import java.io.IOException;
  21. import java.util.SortedMap;
  22. import java.util.TreeMap;

  23. /**
  24.  * Provides generic JavaBeans support for the Pack/UnpackAdapters
  25.  */
  26. public abstract class Pack200Adapter {

  27.     protected static final int DEFAULT_BUFFER_SIZE = 8192;

  28.     private final PropertyChangeSupport support = new PropertyChangeSupport(this);

  29.     private final SortedMap<String, String> properties = new TreeMap<>();

  30.     public void addPropertyChangeListener(final PropertyChangeListener listener) {
  31.         support.addPropertyChangeListener(listener);
  32.     }

  33.     /**
  34.      * Completion between 0..1.
  35.      *
  36.      * @param value Completion between 0..1.
  37.      * @throws IOException if the value cannot be parsed
  38.      */
  39.     protected void completed(final double value) throws IOException {
  40.         firePropertyChange("pack.progress", null, String.valueOf((int) (100 * value))); //$NON-NLS-1$
  41.     }

  42.     /**
  43.      * Reports a property update to listeners
  44.      *
  45.      * @param propertyName name of property being updated
  46.      * @param oldValue old property value
  47.      * @param newValue new property value
  48.      * @throws IOException if the values cannot be parsed
  49.      */
  50.     protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
  51.         support.firePropertyChange(propertyName, oldValue, newValue);
  52.     }

  53.     public SortedMap<String, String> properties() {
  54.         return properties;
  55.     }

  56.     public void removePropertyChangeListener(final PropertyChangeListener listener) {
  57.         support.removePropertyChangeListener(listener);
  58.     }
  59. }