1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc.event; |
|
27 |
|
28 public class IntervalEventQueue { |
|
29 // |
|
30 // Enums |
|
31 // |
|
32 |
|
33 public enum Type { |
|
34 ADD, |
|
35 REMOVE, |
|
36 } |
|
37 |
|
38 // |
|
39 // Instance data |
|
40 // |
|
41 |
|
42 private IntervalEventSource source; |
|
43 private IntervalListeners listeners; |
|
44 private Type type; |
|
45 private int first; |
|
46 private int last; |
|
47 |
|
48 // |
|
49 // Constructors |
|
50 // |
|
51 |
|
52 public IntervalEventQueue(IntervalEventSource source, |
|
53 IntervalListeners listeners) { |
|
54 |
|
55 this.source = source; |
|
56 this.listeners = listeners; |
|
57 } |
|
58 |
|
59 // |
|
60 // IntervalEventQueue methods |
|
61 // |
|
62 |
|
63 public synchronized void addInterval(int f, int l, Type t) { |
|
64 if (f > l || f < 0) |
|
65 throw new IllegalArgumentException(Integer.toString(f)); |
|
66 |
|
67 /* |
|
68 * We need to map l and f to pre-previous-removes values so we |
|
69 * can accurately compare them to first and last. (The tests we |
|
70 * perform are invariant under addition, so we only need to do |
|
71 * this for removes). |
|
72 */ |
|
73 int rawf = f; |
|
74 int rawl = l; |
|
75 if (type == Type.REMOVE && t == Type.REMOVE) { |
|
76 int total = last - first + 1; |
|
77 if (f >= first) |
|
78 f += total; |
|
79 if (l >= first) |
|
80 l += total; |
|
81 } |
|
82 |
|
83 /* |
|
84 * If this is a new event type or a disjoint interval, flush |
|
85 * and start with a fresh interval. |
|
86 */ |
|
87 if (t != type || l < first - 1 || f > last + 1) { |
|
88 flush(); |
|
89 first = rawf; |
|
90 last = rawl; |
|
91 type = t; |
|
92 return; |
|
93 } |
|
94 |
|
95 /* |
|
96 * Extend the range as necessary. |
|
97 */ |
|
98 if (type == Type.REMOVE) { |
|
99 /* |
|
100 * We abut or straddle the existing range; extend the |
|
101 * appropriate limits. |
|
102 */ |
|
103 first = Math.min(f, first); |
|
104 last = Math.max(l, last); |
|
105 } else { |
|
106 /* |
|
107 * We're inserting into the existing range; add our length |
|
108 * to its. |
|
109 */ |
|
110 last += (l - f + 1); |
|
111 } |
|
112 } |
|
113 |
|
114 public void addIndex(int index, Type type) { |
|
115 addInterval(index, index, type); |
|
116 } |
|
117 |
|
118 public synchronized void flush() { |
|
119 if (type != null) { |
|
120 switch (type) { |
|
121 case ADD: |
|
122 listeners.intervalAdded( |
|
123 new IntervalEvent(source, first, last)); |
|
124 break; |
|
125 |
|
126 case REMOVE: |
|
127 listeners.intervalRemoved( |
|
128 new IntervalEvent(source, first, last)); |
|
129 break; |
|
130 } |
|
131 } |
|
132 type = null; |
|
133 } |
|
134 |
|
135 public synchronized int getFirstIndex() { |
|
136 return first; |
|
137 } |
|
138 |
|
139 public synchronized int getLastIndex() { |
|
140 return last; |
|
141 } |
|
142 |
|
143 public synchronized Type getType() { |
|
144 return type; |
|
145 } |
|
146 } |
|