View Javadoc
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  
19  import java.beans.PropertyChangeListener;
20  import java.beans.PropertyChangeSupport;
21  import java.io.IOException;
22  import java.util.SortedMap;
23  import java.util.TreeMap;
24  
25  /**
26   * Provides generic JavaBeans support for the Pack/UnpackAdapters
27   */
28  public abstract class Pack200Adapter {
29  
30      protected static final int DEFAULT_BUFFER_SIZE = 8192;
31  
32      private final PropertyChangeSupport support = new PropertyChangeSupport(this);
33  
34      private final SortedMap<String, String> properties = new TreeMap<>();
35  
36      public void addPropertyChangeListener(final PropertyChangeListener listener) {
37          support.addPropertyChangeListener(listener);
38      }
39  
40      /**
41       * Completion between 0..1.
42       *
43       * @param value Completion between 0..1.
44       * @throws IOException if the value cannot be parsed
45       */
46      protected void completed(final double value) throws IOException {
47          firePropertyChange("pack.progress", null, String.valueOf((int) (100 * value))); //$NON-NLS-1$
48      }
49  
50      /**
51       * Reports a property update to listeners
52       *
53       * @param propertyName name of property being updated
54       * @param oldValue old property value
55       * @param newValue new property value
56       * @throws IOException if the values cannot be parsed
57       */
58      protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
59          support.firePropertyChange(propertyName, oldValue, newValue);
60      }
61  
62      public SortedMap<String, String> properties() {
63          return properties;
64      }
65  
66      public void removePropertyChangeListener(final PropertyChangeListener listener) {
67          support.removePropertyChangeListener(listener);
68      }
69  }