|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.util; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +import com.sun.max.program.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * An array-based recording of the history of a value, with |
| 29 | + * time expressed as the number of generations back from the current generation (0). |
| 30 | + * |
| 31 | + * @author Michael Van De Vanter |
| 32 | + */ |
| 33 | +public 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 add(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 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return the "current" value (at generation 0). |
| 70 | + * Error if no values have been recorded. |
| 71 | + */ |
| 72 | + public E get() { |
| 73 | + if (generations.size() > 0) { |
| 74 | + return generations.getFirst(); |
| 75 | + } |
| 76 | + ProgramError.unexpected("empty history"); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return The value at a specified generation. |
| 82 | + * Error if generation does not exist. |
| 83 | + */ |
| 84 | + public E get(int generation) { |
| 85 | + final Iterator<E> iterator = generations.iterator(); |
| 86 | + int index = 0; |
| 87 | + while (iterator.hasNext()) { |
| 88 | + if (index == generation) { |
| 89 | + return iterator.next(); |
| 90 | + } |
| 91 | + index++; |
| 92 | + } |
| 93 | + ProgramError.unexpected("exceeded history"); |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return the age, in generations, of the current value, since recording began. |
| 99 | + * 0 if different from immediate predecessor; -1 if no different value ever recorded |
| 100 | + * Comparison uses {@linkplain Object#equals(Object) equals}. |
| 101 | + */ |
| 102 | + public int getAge() { |
| 103 | + return age; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return the maximum number of generations that can be recorded. |
| 108 | + */ |
| 109 | + public int getLimit() { |
| 110 | + return limit; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return the number of generations recorded; initially 0. |
| 115 | + */ |
| 116 | + public int getSize() { |
| 117 | + return generations.size(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @return iteration of the values recorded in the history, starting with the current |
| 122 | + * generation and proceeding backward in time. |
| 123 | + */ |
| 124 | + public Iterator<E> values() { |
| 125 | + return generations.iterator(); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments