Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/util/ArrayValueHistory.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.util; | |
24 | |
25 import java.util.*; | |
26 | |
27 import com.sun.max.program.*; | |
28 | |
29 /** | |
30 * An array-based recording of the history of a value, with | |
31 * time expressed as the number of generations back from the current generation (0). | |
32 */ | |
33 public final class ArrayValueHistory<E> { | |
34 | |
35 private final ArrayDeque<E> generations; | |
36 private final int limit; | |
37 private int age = -1; | |
38 | |
39 public ArrayValueHistory(int limit) { | |
40 this.generations = new ArrayDeque<E>(); | |
41 this.limit = limit; | |
42 } | |
43 | |
44 public ArrayValueHistory() { | |
45 this (Integer.MAX_VALUE); | |
46 } | |
47 | |
48 /** | |
49 * Adds a new value, which becomes the current generation. | |
50 * The generation of all previously recorded values increases by 1. | |
51 */ | |
52 public void addNew(E newValue) { | |
53 // if (generations.size() > 0) { | |
54 // if (newValue.equals(generations.getFirst())) { | |
55 // if (age >= 0) { | |
56 // age++; | |
57 // } | |
58 // } else { | |
59 // age = 0; | |
60 // } | |
61 // } | |
62 generations.addFirst(newValue); | |
63 if (generations.size() > limit) { | |
64 generations.removeLast(); | |
65 } | |
66 this.age = currentAge(); | |
67 } | |
68 | |
69 /** | |
70 * Replaces the current value in the history. | |
71 * | |
72 * @param newValue value which becomes current | |
73 * @throws ProgramError if no values have been recorded. | |
74 */ | |
75 public void updateCurrent(E newValue) { | |
76 if (generations.size() > 0) { | |
77 if (!newValue.equals(generations.getFirst())) { | |
78 generations.pop(); | |
79 addNew(newValue); | |
80 } | |
81 } else { | |
82 throw ProgramError.unexpected("attempt to update empty history"); | |
83 } | |
84 } | |
85 | |
86 /** | |
87 * Gets the historical value at some generation, 0 is current. | |
88 * | |
89 * @return The value at a specified generation. | |
90 * @throws ProgramError if the index is out of range of the current history | |
91 */ | |
92 public E value(int generation) { | |
93 if (generation == 0 && generations.size() > 0) { | |
94 return generations.getFirst(); | |
95 } | |
96 final Iterator<E> iterator = generations.iterator(); | |
97 int index = 0; | |
98 while (iterator.hasNext()) { | |
99 if (index == generation) { | |
100 return iterator.next(); | |
101 } | |
102 index++; | |
103 } | |
104 throw ProgramError.unexpected("exceeded history"); | |
105 } | |
106 | |
107 /** | |
108 * @return the age, in generations, of the current value, since recording began. | |
109 * 0 if different from immediate predecessor; -1 if no different value ever recorded | |
110 * Comparison uses {@linkplain Object#equals(Object) equals}. | |
111 */ | |
112 public int currentValueAge() { | |
113 return age; | |
114 } | |
115 | |
116 /** | |
117 * @return the maximum number of generations that can be recorded. | |
118 */ | |
119 public int getLimit() { | |
120 return limit; | |
121 } | |
122 | |
123 /** | |
124 * @return the number of generations recorded; initially 0. | |
125 */ | |
126 public int size() { | |
127 return generations.size(); | |
128 } | |
129 | |
130 /** | |
131 * @return iteration of the values recorded in the history, starting with the current | |
132 * generation and proceeding backward in time. | |
133 */ | |
134 public Iterator<E> generations() { | |
135 return generations.iterator(); | |
136 } | |
137 | |
138 /** | |
139 * Computes the age of the current generation, defined to be the number of | |
140 * preceding values that are equal, or -1 if no different value exists. | |
141 */ | |
142 private int currentAge() { | |
143 assert generations.size() > 0; | |
144 final Iterator<E> iterator = generations.iterator(); | |
145 E currentValue = iterator.next(); | |
146 int duplicates = 0; | |
147 while (iterator.hasNext()) { | |
148 if (iterator.next().equals(currentValue)) { | |
149 duplicates++; | |
150 } else { | |
151 return duplicates; | |
152 } | |
153 } | |
154 return -1; | |
155 } | |
156 | |
157 } |