Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
131 lines (117 loc) · 4.53 KB

File metadata and controls

131 lines (117 loc) · 4.53 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.util;
import java.util.*;
import com.sun.max.*;
/**
* @see Enumerable
*
* @author Bernd Mathiske
*/
public class Enumerator<E extends Enum<E> & Enumerable<E>>
implements Symbolizer<E> {
private final Class<E> type;
private final E[] ordinalMap;
private final E[] valueMap;
private final int lowestValue;
public Enumerator(Class<E> type) {
this.type = type;
ordinalMap = type.getEnumConstants();
int lowValue = 0;
int highestValue = ordinalMap.length - 1;
boolean valuesAreSameAsOrdinals = true;
for (E e : ordinalMap) {
final int value = e.value();
if (value != e.ordinal()) {
valuesAreSameAsOrdinals = false;
}
if (value < lowValue) {
lowValue = value;
} else if (value > highestValue) {
highestValue = value;
}
}
if (valuesAreSameAsOrdinals) {
this.lowestValue = 0;
valueMap = ordinalMap;
} else {
final int valueMapLength = (highestValue - lowValue) + 1;
final Class<E[]> arrayType = null;
this.lowestValue = lowValue;
valueMap = Utils.cast(arrayType, new Enum[valueMapLength]);
for (E e : ordinalMap) {
final int value = e.value();
// The enumerable with the lowest ordinal is stored in the value map:
if (valueMap[value] == null) {
valueMap[value] = e;
}
}
}
}
public Class<E> type() {
return type;
}
public int numberOfValues() {
return ordinalMap.length;
}
/**
* Adds all the enumerable constants in this enumerator to a given set.
*
* @param set
* the set to which the enumerable constants are to be added
*/
public void addAll(Set<E> set) {
for (E e : this) {
set.add(e);
}
}
public int size() {
return ordinalMap.length;
}
public Iterator<E> iterator() {
return Arrays.asList(ordinalMap).iterator();
}
/**
* Gets the enumerable constant denoted by a given ordinal. Note that this differs from {@link #fromValue(int)} in
* that the latter retrieves an enumerable constant matching a given {@linkplain Enumerable#value() value}. An
* enumerable's value is not necessarily the same as its ordinal.
*
* @throws IndexOutOfBoundsException
* if {@code 0 < ordinal || ordinal >= length()}
*/
public E get(int ordinal) throws IndexOutOfBoundsException {
return ordinalMap[ordinal];
}
/**
* Gets the enumerable constant matching a given value. That is, this method gets an enumerable from this enumerator
* whose {@linkplain Enumerable#value() value} is equal to {@code value}. Note that the given value may not match
* any enumerable in this enumerator in which case null is returned. Additionally, there may be more than one
* enumerable with a matching value in which case the matching enumerable with the lowest
* {@linkplain Enum#ordinal() ordinal} is returned.
*/
public E fromValue(int value) {
final int index = value - lowestValue;
if (index >= 0 && index < valueMap.length) {
return valueMap[index];
}
return null;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.