All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.yarn.listener.OrderedComposite Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed 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.springframework.yarn.listener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;

/**
 * Composite item which can be used in other components which
 * may want to allow automatic and annotation based ordering.
 * Good use case is a list of listeners where user may want
 * to place some of them to be processed before the others.
 *
 * @author Janne Valkealahti
 *
 * @param  the type of the item
 */
public class OrderedComposite {

	private List unordered = new ArrayList();

	private List ordered = new ArrayList();

	private Comparator comparator = new AnnotationAwareOrderComparator();

	private List list = new ArrayList();

	/**
	 * Public setter for the listeners.
	 *
	 * @param items items
	 */
	public void setItems(List items) {
		unordered.clear();
		ordered.clear();
		for (S s : items) {
			add(s);
		}
	}

	/**
	 * Register additional item.
	 *
	 * @param item item
	 */
	public void add(S item) {
		if (item instanceof Ordered) {
			if (!ordered.contains(item)) {
				ordered.add(item);
			}
		} else if (AnnotationUtils.isAnnotationDeclaredLocally(Order.class, item.getClass())) {
			if (!ordered.contains(item)) {
				ordered.add(item);
			}
		} else if (!unordered.contains(item)) {
			unordered.add(item);
		}
		Collections.sort(ordered, comparator);
		list.clear();
		list.addAll(ordered);
		list.addAll(unordered);
	}

	/**
	 * Public getter for the list of items. The {@link Ordered} items come
	 * first, followed by any unordered ones.
	 *
	 * @return an iterator over the list of items
	 */
	public Iterator iterator() {
		return new ArrayList(list).iterator();
	}

	/**
	 * Public getter for the list of items in reverse. The {@link Ordered} items
	 * come last, after any unordered ones.
	 *
	 * @return an iterator over the list of items
	 */
	public Iterator reverse() {
		ArrayList result = new ArrayList(list);
		Collections.reverse(result);
		return result.iterator();
	}

}