001: /*
002: * $RCSfile: WakeupCriteriaEnumerator.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.5 $
028: * $Date: 2008/02/28 20:17:33 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.Enumeration;
035: import java.util.NoSuchElementException;
036:
037: /**
038: * A class that enumerates all wakeup criteria in a wakeup condition
039: */
040:
041: class WakeupCriteriaEnumerator implements Enumeration {
042:
043: // An array used for the current criteria in this object
044: WakeupCriterion[] criterion = null;
045:
046: // A pointer to the current criteria
047: int currentIndex = 0;
048:
049: // The number of valid criteria in the array, may be less than criterion.length
050: int length = 0;
051:
052: WakeupCriteriaEnumerator(WakeupCondition cond, int type) {
053: this .reset(cond, type);
054: }
055:
056: void reset(WakeupCondition cond, int type) {
057: int i, j;
058:
059: currentIndex = 0;
060: length = 0;
061: if (cond instanceof WakeupCriterion) {
062: WakeupCriterion crit = (WakeupCriterion) cond;
063:
064: if (criterion == null || criterion.length < 1) {
065: criterion = new WakeupCriterion[1];
066: }
067: if (crit.triggered || type == WakeupCondition.ALL_ELEMENTS) {
068: criterion[0] = crit;
069: length = 1;
070: }
071: } else {
072: if (cond instanceof WakeupAnd) {
073: WakeupAnd condAnd = (WakeupAnd) cond;
074:
075: if (criterion == null
076: || criterion.length < condAnd.conditions.length) {
077: criterion = new WakeupCriterion[condAnd.conditions.length];
078: }
079: for (i = 0; i < condAnd.conditions.length; i++) {
080: if (condAnd.conditions[i].triggered
081: || type == WakeupCondition.ALL_ELEMENTS) {
082: criterion[length++] = condAnd.conditions[i];
083: }
084: }
085: } else if (cond instanceof WakeupOr) {
086: WakeupOr condOr = (WakeupOr) cond;
087:
088: if (criterion == null
089: || criterion.length < condOr.conditions.length) {
090: criterion = new WakeupCriterion[condOr.conditions.length];
091: }
092: for (i = 0; i < condOr.conditions.length; i++) {
093: if (condOr.conditions[i].triggered
094: || type == WakeupCondition.ALL_ELEMENTS) {
095: criterion[length++] = condOr.conditions[i];
096: }
097: }
098: } else if (cond instanceof WakeupOrOfAnds) {
099: WakeupOrOfAnds condOrOfAnds = (WakeupOrOfAnds) cond;
100: int lengthNeeded = 0;
101:
102: for (i = 0; i < condOrOfAnds.conditions.length; i++) {
103: lengthNeeded += condOrOfAnds.conditions[i].conditions.length;
104: }
105:
106: if (criterion == null
107: || criterion.length < lengthNeeded) {
108: criterion = new WakeupCriterion[lengthNeeded];
109: }
110:
111: for (i = 0; i < condOrOfAnds.conditions.length; i++) {
112: for (j = 0; j < condOrOfAnds.conditions[i].conditions.length; j++) {
113: if (condOrOfAnds.conditions[i].conditions[j].triggered
114: || type == WakeupCondition.ALL_ELEMENTS) {
115: criterion[length++] = condOrOfAnds.conditions[i].conditions[j];
116: }
117: }
118: }
119: } else {
120: WakeupAndOfOrs condAndOfOrs = (WakeupAndOfOrs) cond;
121: int lengthNeeded = 0;
122:
123: for (i = 0; i < condAndOfOrs.conditions.length; i++) {
124: lengthNeeded += condAndOfOrs.conditions[i].conditions.length;
125: }
126:
127: if (criterion == null
128: || criterion.length < lengthNeeded) {
129: criterion = new WakeupCriterion[lengthNeeded];
130: }
131:
132: for (i = 0; i < condAndOfOrs.conditions.length; i++) {
133: for (j = 0; j < condAndOfOrs.conditions[i].conditions.length; j++) {
134: if (condAndOfOrs.conditions[i].conditions[j].triggered
135: || type == WakeupCondition.ALL_ELEMENTS) {
136: criterion[length++] = condAndOfOrs.conditions[i].conditions[j];
137: }
138: }
139: }
140: }
141: }
142: }
143:
144: public boolean hasMoreElements() {
145: if (currentIndex == length) {
146: return false;
147: }
148: return true;
149: }
150:
151: public Object nextElement() {
152: if (currentIndex < length) {
153: return ((Object) criterion[currentIndex++]);
154: } else {
155: throw new NoSuchElementException(J3dI18N
156: .getString("WakeupCriteriaEnumerator0"));
157: }
158: }
159: }
|