-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathOrderByList.java
625 lines (493 loc) · 13.9 KB
/
OrderByList.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package org.hy.common;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 排序集合。
*
* <K> 表示惟一标记的类型
* <E> 表示元素的附属信息。这个属性提供给实际使用者使用的类型
*
* 当元素排序序号动态变化时,能实时体显排序变化。
*
* 并且,List与Map的融合体。即有按 "排序顺序"找值的功能,又有按关键字找值的功能。
*
* 可分两种更新类型
* 1. 读快,写慢。即每次添加、删除后,立即更新排序
* 2. 读慢,写快。即每次添加、删除后,并不更新排序,而是在每次读取前更新排序。
*
* 可分两种排序规则
* 1. 升序
* 2. 降序
*
* @author ZhengWei(HY)
* @version v1.0
* @createDate 2012-10-22
*/
public class OrderByList<K ,E> extends ArrayList<OrderByElement<K ,E>> implements List<OrderByElement<K ,E>>
{
private static final long serialVersionUID = 777444952906646040L;
/** 更新排序类型。读快,写慢。即每次添加、删除后,立即更新排序 */
public final static int $RefreshType_Read = 1;
/** 更新排序类型。读慢,写快。即每次添加、删除后,并不更新排序,而是在每次读取前更新排序。 */
public final static int $RefreshType_Write = -1;
/** 排序类型。升序 */
public final static int $OrderType_Asc = 1;
/** 排序类型。降序 */
public final static int $OrderType_Desc = -1;
/** 按关键存储的排序数据。主要作用读取 */
private Map<K ,OrderByElement<K ,E>> values;
/**
* 更新排序类型。默认为 1 。
*
* 1 表示:读快,写慢。即每次添加、删除后,立即更新排序
* -1 表示:读慢,写快。即每次添加、删除后,并不更新排序,而是在每次读取前更新排序。
*/
private int refreshType;
/**
* 排序类型。默认为 1 。
*
* 1 表示:升序
* -1 表示:降序
*/
private int orderByType;
public OrderByList()
{
super();
this.refreshType = 1;
this.orderByType = 1;
this.values = new Hashtable<K ,OrderByElement<K ,E>>();
}
public OrderByList(int i_IinitialCapacity)
{
super(i_IinitialCapacity);
this.refreshType = 1;
this.orderByType = 1;
this.values = new Hashtable<K ,OrderByElement<K ,E>>();
}
/**
* 验证关键字所指元素是否存在
*
* @param i_Key
* @return
*/
public boolean containsKey(K i_Key)
{
return this.values.containsKey(i_Key);
}
/**
* 添加排序数据
*
* @param i_K
* @param i_E
*/
public boolean add(K i_K ,E i_E)
{
return add(new OrderByElement<K ,E>(i_K ,i_E));
}
/**
* 添加排序数据
*
* @param i_K
* @param i_E
*/
public void add(int i_OrderByID ,K i_K ,E i_E)
{
add(i_OrderByID ,new OrderByElement<K ,E>(i_K ,i_E));
}
/**
* 添加排序数据
*
* @param io_OrderByElement
*/
@Override
public synchronized boolean add(OrderByElement<K ,E> io_OrderByElement)
{
if ( io_OrderByElement == null )
{
throw new NullPointerException("Put OrderByElement is null.");
}
if ( io_OrderByElement.getKey() == null )
{
throw new NullPointerException("Put OrderByElement.key is null.");
}
if ( this.values.containsKey(io_OrderByElement) )
{
throw new SecurityException("OrderByElement.key is only.");
}
else
{
this.values.put(io_OrderByElement.getKey() ,io_OrderByElement);
super.add(io_OrderByElement);
io_OrderByElement.setOrderByList(this);
if ( this.refreshType >= 1 )
{
this.fireChange();
}
return true;
}
}
/**
* 添加排序数据
*
* @param i_OrderByIndex
* @param i_OrderByElement
*/
@Override
public void add(int i_OrderByIndex, OrderByElement<K ,E> io_OrderByElement)
{
io_OrderByElement.setOrderBy(i_OrderByIndex);
add(io_OrderByElement);
}
/**
* 添加排序数据
*
* @param i_OrderByElement
*/
@Override
@SuppressWarnings("unchecked")
public synchronized boolean addAll(Collection<? extends OrderByElement<K ,E>> i_Collection)
{
Iterator<OrderByElement<K ,E>> v_Iterator = (Iterator<OrderByElement<K ,E>>) i_Collection.iterator();
while ( v_Iterator.hasNext() )
{
this.add(v_Iterator.next());
}
return true;
}
/**
* 添加排序数据
*
* 不支持定点添加
*
* @param i_OrderByElement
*/
@Override
public synchronized boolean addAll(int i_Index ,Collection<? extends OrderByElement<K ,E>> i_Collection)
{
return this.addAll(i_Collection);
}
/**
* 不支持定点设置
*
* 此方法只能调用super.set()方法实现,否则 Collections.sort() 时,会出现 java.lang.StackOverflowError 异常
*/
@Override
public OrderByElement<K ,E> set(int i_Index ,OrderByElement<K ,E> i_OrderByElement)
{
return super.set(i_Index ,i_OrderByElement);
}
/**
* 删除排序数据
*/
@Override
public synchronized boolean remove(Object i_OrderByElement)
{
if ( i_OrderByElement == null )
{
return false;
}
else if ( i_OrderByElement instanceof OrderByElement )
{
boolean v_Ret = super.remove(i_OrderByElement);
if ( v_Ret )
{
this.values.remove(i_OrderByElement);
if ( this.refreshType >= 1 )
{
this.fireChange();
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/**
* 删除排序数据
*/
@Override
public synchronized OrderByElement<K ,E> remove(int i_Index)
{
OrderByElement<K ,E> v_Ret = super.remove(i_Index);
if ( v_Ret != null )
{
this.values.remove(v_Ret.getKey());
if ( this.refreshType >= 1 )
{
this.fireChange();
}
return v_Ret;
}
else
{
return null;
}
}
/**
* 删除所有排序数据
*/
@Override
public synchronized boolean removeAll(Collection<?> i_Collection)
{
boolean v_Modified = false;
Iterator<OrderByElement<K ,E>> v_Iterator = iterator();
while ( v_Iterator.hasNext() )
{
OrderByElement<K ,E> v_OrderByElement = v_Iterator.next();
if ( i_Collection.contains(v_OrderByElement) )
{
v_Iterator.remove();
this.values.remove(v_OrderByElement);
if ( this.refreshType >= 1 )
{
this.fireChange();
}
v_Modified = true;
}
}
return v_Modified;
}
/**
* 删除排序数据
*
* @param i_Key
* @return
*/
public synchronized OrderByElement<K ,E> removeByKey(K i_Key)
{
if ( i_Key == null )
{
throw new NullPointerException("Remove Key is null.");
}
else if ( !this.values.containsKey(i_Key) )
{
throw new SecurityException("Remove Key is not exist.");
}
else
{
OrderByElement<K ,E> v_Ret = this.values.remove(i_Key);
super.remove(v_Ret);
if ( this.refreshType >= 1 )
{
this.fireChange();
}
return v_Ret;
}
}
/**
* 清理所有排序数据
*/
@Override
public synchronized void clear()
{
if ( !Help.isNull(this.values) )
{
for (OrderByElement<K ,E> v_Item : this.values.values())
{
v_Item.clear();
}
this.values.clear();
}
super.clear();
}
/**
* 获取排序数据,按惟一标记Key
*
* @param i_Key
* @return
*/
public OrderByElement<K ,E> getByKey(K i_Key)
{
if ( i_Key == null )
{
throw new NullPointerException("Get Key is null.");
}
else if ( !this.values.containsKey(i_Key) )
{
throw new SecurityException("Get Key is not exist.");
}
else
{
return this.values.get(i_Key);
}
}
/**
* 获取排序数据,按排序顺序
*
* 下标从 0 开始
*
* @param i_Index
* @return
*/
@Override
public OrderByElement<K ,E> get(int i_Index)
{
if ( i_Index < 0 || i_Index >= this.size() )
{
return null;
}
return super.get(i_Index);
}
/**
* 获取元素的排序顺序
*
* 下标从 0 开始
*
* 返回 -1 表示元素不存在。
*
* @param i_OrderByElement
* @return
*/
@Override
public int indexOf(Object i_OrderByElement)
{
if ( i_OrderByElement == null )
{
return -1;
}
else if ( i_OrderByElement instanceof OrderByElement )
{
return super.indexOf(i_OrderByElement);
}
else
{
return -1;
}
}
/**
* 获取排序后的排序数据
*/
@Override
public Iterator<OrderByElement<K ,E>> iterator()
{
if ( this.refreshType < 1 )
{
this.fireChange();
}
return super.iterator();
}
/**
* 获取排序后的排序数据
*/
@Override
public Object [] toArray()
{
if ( this.refreshType < 1 )
{
this.fireChange();
}
return super.toArray();
}
/**
* 获取排序后的排序数据
*/
@Override
public List<OrderByElement<K ,E>> subList(int i_FromIndex ,int i_ToIndex)
{
if ( this.refreshType < 1 )
{
this.fireChange();
}
return super.subList(i_FromIndex ,i_ToIndex);
}
/**
* 触发数据集合排序动作
*
* 注:在OrderByElement中与有调用
*/
public synchronized void fireChange()
{
if ( this.size() >= 2 )
{
Collections.sort(this);
}
}
/**
* 更新排序类型。默认为 1 。
*
* 1 表示:读快,写慢。即每次添加、删除后,立即更新排序
* -1 表示:读慢,写快。即每次添加、删除后,并不更新排序,而是在每次读取前更新排序。
*/
public int getRefreshType()
{
return this.refreshType;
}
/**
* 更新排序类型。默认为 1 。
*
* 1 表示:读快,写慢。即每次添加、删除后,立即更新排序
* -1 表示:读慢,写快。即每次添加、删除后,并不更新排序,而是在每次读取前更新排序。
*/
public void setRefreshType(int i_RefreshType)
{
this.refreshType = i_RefreshType;
}
/**
* 排序类型。默认为 1 。
*
* 1 表示:升序
* -1 表示:降序
*/
public int getOrderByType()
{
return this.orderByType;
}
/**
* 排序类型。默认为 1 。
*
* 1 表示:升序
* -1 表示:降序
*/
public void setOrderByType(int i_OrderByType)
{
if ( i_OrderByType >= 1 && this.orderByType >= 1 )
{
this.orderByType = i_OrderByType;
}
else if ( i_OrderByType < 1 && this.orderByType < 1 )
{
this.orderByType = i_OrderByType;
}
else
{
this.orderByType = i_OrderByType;
this.fireChange();
}
}
@Override
public String toString()
{
StringBuilder v_Buffer = new StringBuilder();
for (int i=0; i<this.size() && i<10; i++)
{
v_Buffer.append(i).append(" ");
v_Buffer.append(this.get(i).getOrderBy());
v_Buffer.append("-");
v_Buffer.append(this.get(i).getKey());
v_Buffer.append("\n");
}
return v_Buffer.toString();
}
/*
ZhengWei(HY) Del 2016-07-30
不能实现这个方法。首先JDK中的Hashtable、ArrayList中也没有实现此方法。
它会在元素还有用,但集合对象本身没有用时,释放元素对象
一些与finalize相关的方法,由于一些致命的缺陷,已经被废弃了
protected void finalize() throws Throwable
{
this.clear();
super.finalize();
}
*/
}