-
Notifications
You must be signed in to change notification settings - Fork 344
/
PropertyList.java
423 lines (355 loc) · 10.9 KB
/
PropertyList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.
This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
http://www.cs.umass.edu/~mccallum/mallet
This software is provided under the terms of the Common Public License,
version 1.0, as published by http://www.opensource.org. For further
information, see the file `LICENSE' included with this distribution. */
/**
@author Andrew McCallum <a href="mailto:[email protected]">[email protected]</a>
*/
package cc.mallet.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import com.google.errorprone.annotations.Var;
public class PropertyList implements Serializable
{
protected PropertyList next;
protected String key;
public static PropertyList add (String key, Object value,
PropertyList rest)
{
assert (key != null);
return new ObjectProperty (key, value, rest);
}
public static PropertyList add (String key, String value,
PropertyList rest)
{
assert (key != null);
return new ObjectProperty (key, value, rest);
}
public static PropertyList add (String key, double value,
PropertyList rest)
{
assert (key != null);
return new NumericProperty (key, value, rest);
}
public static PropertyList remove (String key,
PropertyList rest)
{
assert (key != null);
return new ObjectProperty (key, null, rest);
}
public Object lookupObject (String key)
{
if (this.key.equals (key)) {
if (this instanceof ObjectProperty)
return ((ObjectProperty)this).value;
else if (this instanceof NumericProperty)
return Double.valueOf(((NumericProperty)this).value);
else
throw new IllegalStateException ("Unrecognitized PropertyList entry.");
} else if (this.next == null) {
return null;
} else {
return next.lookupObject (key);
}
}
public double lookupNumber (String key)
{
if (this.key.equals (key)) {
if (this instanceof NumericProperty)
return ((NumericProperty)this).value;
else if (this instanceof ObjectProperty) {
Object obj = ((ObjectProperty)this).value;
if (obj == null) return 0;
// xxx Remove these? Use might ask for numericIterator expecting to get these (and not!)
if (obj instanceof Double) return ((Double)obj).doubleValue();
if (obj instanceof Integer) return ((Double)obj).intValue();
if (obj instanceof Float) return ((Double)obj).floatValue();
if (obj instanceof Short) return ((Double)obj).shortValue();
if (obj instanceof Long) return ((Double)obj).longValue();
// xxx? throw new IllegalStateException ("Property is not numeric.");
return 0;
} else
throw new IllegalStateException ("Unrecognitized PropertyList entry.");
} else if (this.next == null) {
return 0;
} else {
return next.lookupNumber (key);
}
}
public boolean hasProperty (String key)
{
if (this.key.equals (key)) {
if (this instanceof ObjectProperty && ((ObjectProperty)this).value == null)
return false;
else
return true;
} else if (this.next == null) {
return false;
} else {
return next.hasProperty (key);
}
}
public Iterator iterator ()
{
return new Iterator (this);
}
public static PropertyList sumDuplicateKeyValues (PropertyList pl ) {
return sumDuplicateKeyValues(pl,false);
}
// culotta 2/02/04: to increment counts of properties values.
public static PropertyList sumDuplicateKeyValues (PropertyList pl, boolean ignoreZeros) {
if (!(pl instanceof NumericProperty))
throw new IllegalArgumentException ("PropertyList must be Numeric to sum values");
HashMap key2value = new HashMap ();
Iterator iter = pl.numericIterator();
while (iter.hasNext()) {
iter.nextProperty ();
String key = iter.getKey();
double val = iter.getNumericValue();
Double storedValue = (Double)key2value.get (key);
if (storedValue == null)
key2value.put (key, Double.valueOf (val));
else // sum stored value with current value
key2value.put (key, Double.valueOf (storedValue.doubleValue() + val));
}
@Var
PropertyList ret = null;
java.util.Iterator hashIter = key2value.keySet().iterator();
while (hashIter.hasNext()) { // create new property list
String key = (String) hashIter.next();
double val = ((Double)key2value.get (key)).doubleValue();
if(ignoreZeros && val==0.0)
continue;
ret = PropertyList.add (key, val, ret);
}
return ret;
}
public Iterator numericIterator ()
{
return new NumericIterator (this);
}
public Iterator objectIterator ()
{
return new ObjectIterator (this);
}
protected PropertyList ()
{
throw new IllegalArgumentException ("Zero args constructor not allowed.");
}
protected PropertyList (String key, PropertyList rest)
{
this.key = key;
this.next = rest;
}
public void print ()
{
if (this instanceof NumericProperty)
System.out.println (this.key.toString() + "=" + ((NumericProperty)this).value);
else if (this instanceof ObjectProperty)
System.out.println (this.key.toString() + "=" + ((ObjectProperty)this).value);
else
throw new IllegalArgumentException ("Unrecognized PropertyList type");
if (this.next != null)
this.next.print();
}
// Serialization
// PropertyList
private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 0;
private void writeObject (ObjectOutputStream out) throws IOException {
out.writeInt (CURRENT_SERIAL_VERSION);
out.writeObject(next);
out.writeObject(key);
}
private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
int version = in.readInt ();
next = (PropertyList) in.readObject();
key = (String) in.readObject();
}
public int size ()
{
@Var
PropertyList pl = this;
@Var
int size = 1;
while (pl.next != null) {
pl = pl.next;
size++;
}
return size;
}
private static class NumericProperty extends PropertyList implements Serializable
{
protected double value;
public NumericProperty (String key, double value, PropertyList rest)
{
super (key, rest);
this.value = value;
}
// Serialization
// NumericProperty
private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 0;
private void writeObject (ObjectOutputStream out) throws IOException {
out.writeInt(CURRENT_SERIAL_VERSION);
out.writeDouble(value);
}
private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
int version = in.readInt();
value = in.readDouble();
}
}
private static class ObjectProperty extends PropertyList
{
protected Object value;
public ObjectProperty (String key, Object value, PropertyList rest)
{
super (key, rest);
this.value = value;
}
// Serialization
// ObjectProperty
private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 0;
private void writeObject (ObjectOutputStream out) throws IOException {
out.writeInt(CURRENT_SERIAL_VERSION);
out.writeObject(value);
}
private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
int version = in.readInt();
value = (Object) in.readObject();
}
}
public class Iterator implements java.util.Iterator, Serializable
{
PropertyList property, nextProperty;
HashSet deletedKeys = null;
boolean nextCalled = false;
boolean returnNumeric = true;
boolean returnObject = true;
public Iterator (PropertyList pl)
{
property = findReturnablePropertyAtOrAfter (pl);
if (property == null)
nextProperty = null;
else
nextProperty = findReturnablePropertyAtOrAfter (property.next);
}
private PropertyList findReturnablePropertyAtOrAfter (@Var PropertyList property)
{
while (property != null) {
if (property instanceof NumericProperty && returnNumeric) {
if (((NumericProperty)property).value == 0.0) {
if (deletedKeys == null) deletedKeys = new HashSet();
deletedKeys.add (property.key);
property = property.next;
} else
break;
} else if (property instanceof ObjectProperty && returnObject) {
if (((ObjectProperty)property).value == null) {
if (deletedKeys == null) deletedKeys = new HashSet();
deletedKeys.add (property.key);
property = property.next;
} else
break;
} else
throw new IllegalStateException ("Unrecognized property type "+property.getClass().getName());
}
return property;
}
public boolean hasNext ()
{
return ((nextCalled && nextProperty != null) || (!nextCalled && property != null));
}
public boolean isNumeric ()
{
return (property instanceof NumericProperty);
}
public double getNumericValue ()
{
return ((NumericProperty)property).value;
}
public Object getObjectValue ()
{
return ((ObjectProperty)property).value;
}
public String getKey ()
{
return property.key;
}
public PropertyList nextProperty ()
{
if (nextCalled) {
property = nextProperty;
nextProperty = findReturnablePropertyAtOrAfter (property.next);
} else
nextCalled = true;
return property;
}
public Object next ()
{
return nextProperty ();
}
public void remove ()
{
throw new UnsupportedOperationException ();
}
// Serialization
// Iterator
private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 0;
private void writeObject (ObjectOutputStream out) throws IOException {
out.writeInt (CURRENT_SERIAL_VERSION);
out.writeObject(property);
out.writeObject(nextProperty);
out.writeObject(deletedKeys);
out.writeBoolean(nextCalled);
out.writeBoolean(returnNumeric);
out.writeBoolean(returnObject);
}
private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
int version = in.readInt ();
property = (PropertyList) in.readObject();
nextProperty = (PropertyList) in.readObject();
deletedKeys = (HashSet) in.readObject();
nextCalled = in.readBoolean();
returnNumeric = in.readBoolean();
returnObject = in.readBoolean();
}
}
public class NumericIterator extends Iterator implements Serializable
{
public NumericIterator (PropertyList pl) {
super (pl);
this.returnObject = false;
}
}
public class ObjectIterator extends Iterator implements Serializable
{
public ObjectIterator (PropertyList pl) {
super (pl);
this.returnNumeric = false;
}
}
// for fast merging of PropertLists
// gmann 8/14/6
public PropertyList last(){
if (next == null){
return this;
}
else return next.last();
}
public PropertyList append(PropertyList nextPl) throws UnsupportedOperationException{
if (this.next != null){
throw new UnsupportedOperationException("PropertyList.java: Cannot append to middle of a list\n");
}
this.next = nextPl;
return last();
}
}