Skip to content

Commit 869a25a

Browse files
author
hborders
committed
Added com.sun.max.util package from Maxine Base.
This should fix com.sun.max.io.
1 parent 919e488 commit 869a25a

16 files changed

Lines changed: 1409 additions & 0 deletions
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
/**
26+
* Deferred Runnables.
27+
*
28+
* Creating a Deferrable either causes immediate execution of its 'run()' method
29+
* or queues it for deferred execution later on when 'runAll()' is called.
30+
*
31+
* @author Bernd Mathiske
32+
*/
33+
public abstract class Deferrable implements Runnable {
34+
35+
public Deferrable(Queue queue) {
36+
queue.handle(this);
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return super.toString();
42+
}
43+
44+
public static final class Queue {
45+
46+
private List<Deferrable> deferrables;
47+
48+
private Queue() {
49+
}
50+
51+
synchronized void handle(Deferrable deferrable) {
52+
if (deferrables != null) {
53+
deferrables.add(deferrable);
54+
} else {
55+
deferrable.run();
56+
}
57+
}
58+
59+
public synchronized void deferAll() {
60+
deferrables = new LinkedList<Deferrable>();
61+
}
62+
63+
public synchronized void runAll() {
64+
while (deferrables != null) {
65+
final List<Deferrable> oldDeferrables = this.deferrables;
66+
this.deferrables = new LinkedList<Deferrable>();
67+
for (Deferrable deferrable : oldDeferrables) {
68+
deferrable.run();
69+
}
70+
if (oldDeferrables.isEmpty()) {
71+
this.deferrables = null;
72+
}
73+
}
74+
}
75+
}
76+
77+
public static Queue createRunning() {
78+
return new Queue();
79+
}
80+
81+
public static Queue createDeferred() {
82+
final Queue queue = new Queue();
83+
queue.deferAll();
84+
return queue;
85+
}
86+
87+
public abstract static class Block implements Runnable {
88+
public Block(Queue queue) {
89+
queue.deferAll();
90+
run();
91+
queue.runAll();
92+
}
93+
}
94+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
/**
24+
* Java enums are insufficient in that their ordinals have to be successive.
25+
* An Enumerable has an additional arbitrary int "value",
26+
* which may incur gaps between ordinal-successive Enumerables.
27+
* <p>
28+
* An Enumerator can be called upon to provide the respective Enumerable matching a given value.
29+
* <p>
30+
* See <a href="http://www.ejournal.unam.mx/cys/vol07-02/CYS07205.pdf">"Inheritance, Generics and Binary Methods in Java"</a>
31+
* for an explanation of how to interpret a recursive generic type.
32+
* <p>
33+
*
34+
* @see Enumerator
35+
*
36+
* @author Bernd Mathiske
37+
*/
38+
public interface Enumerable<E extends Enum<E> & Enumerable<E>> extends Symbol {
39+
40+
// We are merely declaring this method to lock in the same parameter type for the corresponding enumerator,
41+
// not for any actual use
42+
Enumerator<E> enumerator();
43+
44+
}

0 commit comments

Comments
 (0)