Skip to content

Commit

Permalink
Extract
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelandrepearce committed Jan 7, 2019
1 parent d731ffe commit f0dd99c
Show file tree
Hide file tree
Showing 12 changed files with 636 additions and 590 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.core.server;

import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
package org.apache.activemq.artemis.core;

public interface PriorityAware {

default int getPriority() {
return ActiveMQDefaultConfiguration.getDefaultConsumerPriority();
}
int getPriority();
}
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;
}
}
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];
}
}
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;
}
}
Loading

0 comments on commit f0dd99c

Please sign in to comment.