1 | /** |
---|
2 | * Copyright 2007 The Apache Software Foundation |
---|
3 | * |
---|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
5 | * or more contributor license agreements. See the NOTICE file |
---|
6 | * distributed with this work for additional information |
---|
7 | * regarding copyright ownership. The ASF licenses this file |
---|
8 | * to you under the Apache License, Version 2.0 (the |
---|
9 | * "License"); you may not use this file except in compliance |
---|
10 | * with the License. You may obtain a copy of the License at |
---|
11 | * |
---|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
13 | * |
---|
14 | * Unless required by applicable law or agreed to in writing, software |
---|
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
17 | * See the License for the specific language governing permissions and |
---|
18 | * limitations under the License. |
---|
19 | */ |
---|
20 | package org.apache.hadoop.io; |
---|
21 | |
---|
22 | import java.io.DataInput; |
---|
23 | import java.io.DataOutput; |
---|
24 | import java.io.IOException; |
---|
25 | import java.util.Collection; |
---|
26 | import java.util.Comparator; |
---|
27 | import java.util.Map; |
---|
28 | import java.util.Set; |
---|
29 | import java.util.SortedMap; |
---|
30 | import java.util.TreeMap; |
---|
31 | |
---|
32 | import org.apache.hadoop.util.ReflectionUtils; |
---|
33 | |
---|
34 | /** |
---|
35 | * A Writable SortedMap. |
---|
36 | */ |
---|
37 | public class SortedMapWritable extends AbstractMapWritable |
---|
38 | implements SortedMap<WritableComparable, Writable> { |
---|
39 | |
---|
40 | private SortedMap<WritableComparable, Writable> instance; |
---|
41 | |
---|
42 | /** default constructor. */ |
---|
43 | public SortedMapWritable() { |
---|
44 | super(); |
---|
45 | this.instance = new TreeMap<WritableComparable, Writable>(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Copy constructor. |
---|
50 | * |
---|
51 | * @param other the map to copy from |
---|
52 | */ |
---|
53 | public SortedMapWritable(SortedMapWritable other) { |
---|
54 | this(); |
---|
55 | copy(other); |
---|
56 | } |
---|
57 | |
---|
58 | /** {@inheritDoc} */ |
---|
59 | public Comparator<? super WritableComparable> comparator() { |
---|
60 | // Returning null means we use the natural ordering of the keys |
---|
61 | return null; |
---|
62 | } |
---|
63 | |
---|
64 | /** {@inheritDoc} */ |
---|
65 | public WritableComparable firstKey() { |
---|
66 | return instance.firstKey(); |
---|
67 | } |
---|
68 | |
---|
69 | /** {@inheritDoc} */ |
---|
70 | public SortedMap<WritableComparable, Writable> |
---|
71 | headMap(WritableComparable toKey) { |
---|
72 | |
---|
73 | return instance.headMap(toKey); |
---|
74 | } |
---|
75 | |
---|
76 | /** {@inheritDoc} */ |
---|
77 | public WritableComparable lastKey() { |
---|
78 | return instance.lastKey(); |
---|
79 | } |
---|
80 | |
---|
81 | /** {@inheritDoc} */ |
---|
82 | public SortedMap<WritableComparable, Writable> |
---|
83 | subMap(WritableComparable fromKey, WritableComparable toKey) { |
---|
84 | |
---|
85 | return instance.subMap(fromKey, toKey); |
---|
86 | } |
---|
87 | |
---|
88 | /** {@inheritDoc} */ |
---|
89 | public SortedMap<WritableComparable, Writable> |
---|
90 | tailMap(WritableComparable fromKey) { |
---|
91 | |
---|
92 | return instance.tailMap(fromKey); |
---|
93 | } |
---|
94 | |
---|
95 | /** {@inheritDoc} */ |
---|
96 | public void clear() { |
---|
97 | instance.clear(); |
---|
98 | } |
---|
99 | |
---|
100 | /** {@inheritDoc} */ |
---|
101 | public boolean containsKey(Object key) { |
---|
102 | return instance.containsKey(key); |
---|
103 | } |
---|
104 | |
---|
105 | /** {@inheritDoc} */ |
---|
106 | public boolean containsValue(Object value) { |
---|
107 | return instance.containsValue(value); |
---|
108 | } |
---|
109 | |
---|
110 | /** {@inheritDoc} */ |
---|
111 | public Set<java.util.Map.Entry<WritableComparable, Writable>> entrySet() { |
---|
112 | return instance.entrySet(); |
---|
113 | } |
---|
114 | |
---|
115 | /** {@inheritDoc} */ |
---|
116 | public Writable get(Object key) { |
---|
117 | return instance.get(key); |
---|
118 | } |
---|
119 | |
---|
120 | /** {@inheritDoc} */ |
---|
121 | public boolean isEmpty() { |
---|
122 | return instance.isEmpty(); |
---|
123 | } |
---|
124 | |
---|
125 | /** {@inheritDoc} */ |
---|
126 | public Set<WritableComparable> keySet() { |
---|
127 | return instance.keySet(); |
---|
128 | } |
---|
129 | |
---|
130 | /** {@inheritDoc} */ |
---|
131 | public Writable put(WritableComparable key, Writable value) { |
---|
132 | addToMap(key.getClass()); |
---|
133 | addToMap(value.getClass()); |
---|
134 | return instance.put(key, value); |
---|
135 | } |
---|
136 | |
---|
137 | /** {@inheritDoc} */ |
---|
138 | public void putAll(Map<? extends WritableComparable, ? extends Writable> t) { |
---|
139 | for (Map.Entry<? extends WritableComparable, ? extends Writable> e: |
---|
140 | t.entrySet()) { |
---|
141 | |
---|
142 | instance.put(e.getKey(), e.getValue()); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /** {@inheritDoc} */ |
---|
147 | public Writable remove(Object key) { |
---|
148 | return instance.remove(key); |
---|
149 | } |
---|
150 | |
---|
151 | /** {@inheritDoc} */ |
---|
152 | public int size() { |
---|
153 | return instance.size(); |
---|
154 | } |
---|
155 | |
---|
156 | /** {@inheritDoc} */ |
---|
157 | public Collection<Writable> values() { |
---|
158 | return instance.values(); |
---|
159 | } |
---|
160 | |
---|
161 | /** {@inheritDoc} */ |
---|
162 | @SuppressWarnings("unchecked") |
---|
163 | @Override |
---|
164 | public void readFields(DataInput in) throws IOException { |
---|
165 | super.readFields(in); |
---|
166 | |
---|
167 | // Read the number of entries in the map |
---|
168 | |
---|
169 | int entries = in.readInt(); |
---|
170 | |
---|
171 | // Then read each key/value pair |
---|
172 | |
---|
173 | for (int i = 0; i < entries; i++) { |
---|
174 | WritableComparable key = |
---|
175 | (WritableComparable) ReflectionUtils.newInstance(getClass( |
---|
176 | in.readByte()), getConf()); |
---|
177 | |
---|
178 | key.readFields(in); |
---|
179 | |
---|
180 | Writable value = (Writable) ReflectionUtils.newInstance(getClass( |
---|
181 | in.readByte()), getConf()); |
---|
182 | |
---|
183 | value.readFields(in); |
---|
184 | instance.put(key, value); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /** {@inheritDoc} */ |
---|
189 | @Override |
---|
190 | public void write(DataOutput out) throws IOException { |
---|
191 | super.write(out); |
---|
192 | |
---|
193 | // Write out the number of entries in the map |
---|
194 | |
---|
195 | out.writeInt(instance.size()); |
---|
196 | |
---|
197 | // Then write out each key/value pair |
---|
198 | |
---|
199 | for (Map.Entry<WritableComparable, Writable> e: instance.entrySet()) { |
---|
200 | out.writeByte(getId(e.getKey().getClass())); |
---|
201 | e.getKey().write(out); |
---|
202 | out.writeByte(getId(e.getValue().getClass())); |
---|
203 | e.getValue().write(out); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|