1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.commons.compress.harmony.pack200;
20
21 import java.beans.PropertyChangeListener;
22 import java.beans.PropertyChangeSupport;
23 import java.io.IOException;
24 import java.util.SortedMap;
25 import java.util.TreeMap;
26
27 /**
28 * Provides generic JavaBeans support for the Pack/UnpackAdapters
29 */
30 public abstract class Pack200Adapter {
31
32 protected static final int DEFAULT_BUFFER_SIZE = 8192;
33
34 private final PropertyChangeSupport support = new PropertyChangeSupport(this);
35
36 private final SortedMap<String, String> properties = new TreeMap<>();
37
38 public void addPropertyChangeListener(final PropertyChangeListener listener) {
39 support.addPropertyChangeListener(listener);
40 }
41
42 /**
43 * Completion between 0..1.
44 *
45 * @param value Completion between 0..1.
46 * @throws IOException if the value cannot be parsed
47 */
48 protected void completed(final double value) throws IOException {
49 firePropertyChange("pack.progress", null, String.valueOf((int) (100 * value))); //$NON-NLS-1$
50 }
51
52 /**
53 * Reports a property update to listeners
54 *
55 * @param propertyName name of property being updated
56 * @param oldValue old property value
57 * @param newValue new property value
58 * @throws IOException if the values cannot be parsed
59 */
60 protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
61 support.firePropertyChange(propertyName, oldValue, newValue);
62 }
63
64 public SortedMap<String, String> properties() {
65 return properties;
66 }
67
68 public void removePropertyChangeListener(final PropertyChangeListener listener) {
69 support.removePropertyChangeListener(listener);
70 }
71 }