| 1 | /* |
|---|
| 2 | * Cloud9: A MapReduce Library for Hadoop |
|---|
| 3 | * |
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
|---|
| 5 | * may not use this file except in compliance with the License. You may |
|---|
| 6 | * obtain a copy of the License at |
|---|
| 7 | * |
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 | * |
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
|---|
| 13 | * implied. See the License for the specific language governing |
|---|
| 14 | * permissions and limitations under the License. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | package tw.org.nchc.util; |
|---|
| 18 | |
|---|
| 19 | import java.util.HashMap; |
|---|
| 20 | import java.util.Map; |
|---|
| 21 | |
|---|
| 22 | import org.apache.hadoop.io.Writable; |
|---|
| 23 | import org.apache.hadoop.io.WritableComparable; |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * <p> |
|---|
| 27 | * Interface that defines the callback associated with |
|---|
| 28 | * {@link SequenceFileProcessor}. For each key-value pair, the |
|---|
| 29 | * <code>SequenceFileProcessor</code> calls {@link #process}; this needs to |
|---|
| 30 | * be instantiated by the user. After all the key-value pairs are processed, |
|---|
| 31 | * <code>SequenceFileProcessor</code> calls {@link #report}; this also needs |
|---|
| 32 | * to be instantiated by the user. Results of computations are retrieved using |
|---|
| 33 | * {@link #getProperty(String)}. |
|---|
| 34 | * </p> |
|---|
| 35 | * |
|---|
| 36 | * @param <K> |
|---|
| 37 | * type of key |
|---|
| 38 | * @param <V> |
|---|
| 39 | * type of value |
|---|
| 40 | */ |
|---|
| 41 | public abstract class KeyValueProcess<K extends WritableComparable, V extends Writable> { |
|---|
| 42 | private Map<String, Object> mHash = new HashMap<String, Object>(); |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Creates a new <code>KeyValueProcess</code> |
|---|
| 46 | */ |
|---|
| 47 | public KeyValueProcess() { |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Called by {@link SequenceFileProcessor} for every key-value pair. This |
|---|
| 52 | * method needs to be defined by the user. |
|---|
| 53 | * |
|---|
| 54 | * @param key |
|---|
| 55 | * the key |
|---|
| 56 | * @param value |
|---|
| 57 | * the value |
|---|
| 58 | */ |
|---|
| 59 | public abstract void process(K key, V value); |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Called by {@link SequenceFileProcessor} after all key-value pairs have |
|---|
| 63 | * been processed. This methods needs to be defined by the user; typical |
|---|
| 64 | * instantiations would record results of the computation using |
|---|
| 65 | * {@link #setProperty(String, Object)}. |
|---|
| 66 | */ |
|---|
| 67 | public abstract void report(); |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Sets a property. Used for recording the results of computational |
|---|
| 71 | * performed by this class. |
|---|
| 72 | * |
|---|
| 73 | * @param property |
|---|
| 74 | * property |
|---|
| 75 | * @param value |
|---|
| 76 | * value of the property |
|---|
| 77 | */ |
|---|
| 78 | public void setProperty(String property, Object value) { |
|---|
| 79 | mHash.put(property, value); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Retrieves a property. Used for retrieving results of a computational |
|---|
| 84 | * performed by this class. |
|---|
| 85 | * |
|---|
| 86 | * @param property |
|---|
| 87 | * property |
|---|
| 88 | * @return value of the property |
|---|
| 89 | */ |
|---|
| 90 | public Object getProperty(String property) { |
|---|
| 91 | return mHash.get(property); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|