-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d731ffe
commit f0dd99c
Showing
12 changed files
with
636 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/main/java/org/apache/activemq/artemis/utils/collections/ArrayResettableIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.activemq.artemis.utils.collections; | ||
|
||
public class ArrayResettableIterator<T> implements ResettableIterator<T> { | ||
|
||
private final Object[] array; | ||
private int cursor = 0; | ||
private int endPos = -1; | ||
private boolean hasNext; | ||
|
||
public ArrayResettableIterator(Object[] array) { | ||
this.array = array; | ||
reset(); | ||
} | ||
|
||
@Override | ||
public ResettableIterator<T> reset() { | ||
endPos = cursor; | ||
hasNext = array.length > 0; | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return hasNext; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!hasNext) { | ||
throw new IllegalStateException(); | ||
} | ||
@SuppressWarnings("unchecked") T result = (T) array[cursor]; | ||
cursor++; | ||
if (cursor == array.length) { | ||
cursor = 0; | ||
} | ||
if (cursor == endPos) { | ||
hasNext = false; | ||
} | ||
return result; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...is-commons/src/main/java/org/apache/activemq/artemis/utils/collections/MultiIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.activemq.artemis.utils.collections; | ||
|
||
import java.util.Iterator; | ||
|
||
public class MultiIterator<T> implements Iterator<T> { | ||
|
||
private final Iterator<T>[] iterators; | ||
int index = -1; | ||
|
||
public MultiIterator(Iterator<T>[] iterators) { | ||
this.iterators = iterators; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
while (true) { | ||
if (index != -1) { | ||
Iterator<T> currentIterator = get(index); | ||
if (currentIterator.hasNext()) { | ||
return true; | ||
} | ||
} | ||
int next = index + 1; | ||
if (next < iterators.length) { | ||
moveTo(next); | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public T next() { | ||
while (true) { | ||
if (index != -1) { | ||
Iterator<T> currentIterator = get(index); | ||
if (currentIterator.hasNext()) { | ||
return currentIterator.next(); | ||
} | ||
} | ||
int next = index + 1; | ||
if (next < iterators.length) { | ||
moveTo(next); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
protected void moveTo(int index) { | ||
this.index = index; | ||
} | ||
|
||
protected Iterator<T> get(int index) { | ||
return iterators[index]; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/org/apache/activemq/artemis/utils/collections/MultiResettableIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.activemq.artemis.utils.collections; | ||
|
||
public class MultiResettableIterator<T> extends MultiIterator<T> implements ResettableIterator<T> { | ||
|
||
public MultiResettableIterator(ResettableIterator<T>[] iterators) { | ||
super(iterators); | ||
} | ||
|
||
@Override | ||
protected void moveTo(int index) { | ||
super.moveTo(index); | ||
if (index > -1) { | ||
((ResettableIterator<T>) get(index)).reset(); | ||
} | ||
} | ||
|
||
@Override | ||
public ResettableIterator<T> reset() { | ||
moveTo(-1); | ||
return this; | ||
} | ||
} |
Oops, something went wrong.