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

com.marvelution.hudson.plugins.apiv2.cache.activity.ActivitiesCache Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Marvelution under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Marvelution 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 com.marvelution.hudson.plugins.apiv2.cache.activity;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.list.SynchronizedList;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * @author Mark Rekveld
 *
 * @since 4.4.0
 */
public class ActivitiesCache implements Iterable, Collection {

	public static final long MERGE_TIMESPAN = 60 * 1000;

	private List activities = Lists.newArrayList();
	@XStreamOmitField
	private ActivityCache previousActivity = null;

	/**
	 * Getter for getActivities()
	 *
	 * @return the getActivities()
	 */
	@SuppressWarnings("unchecked")
	private List getActivities() {
		return SynchronizedList.decorate(activities);
	}

	/**
	 * Getter for the Sorted {@link List} of {@link ActivitiesCache}
	 * 
	 * @return the {@link List} of sorted {@link ActivityCache} objects
	 */
	public List getSortedActivities() {
		Ordering ordering =
			Ordering.natural().reverse().onResultOf(new Function() {
				@Override
				public Date apply(ActivityCache from) {
					return new Date(from.getTimestamp());
				}
			});
		return ordering.immutableSortedCopy(getActivities());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean add(ActivityCache activity) {
		if (previousActivity != null && previousActivity.isTheSame(activity)) {
			// The Activity was on the same object and executed by the same User, check to see if the activity needs
			// to be merged
			if (activity.getTimestamp() - previousActivity.getTimestamp() < MERGE_TIMESPAN) {
				return false;
			}
		}
		previousActivity = activity;
		return getActivities().add(activity);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int size() {
		return getActivities().size();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isEmpty() {
		return getActivities().isEmpty();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean contains(Object paramObject) {
		return getActivities().contains(paramObject);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Iterator iterator() {
		return getActivities().iterator();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object[] toArray() {
		return getActivities().toArray();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public  T[] toArray(T[] paramArrayOfT) {
		return getActivities().toArray(paramArrayOfT);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean remove(Object paramObject) {
		return getActivities().remove(paramObject);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean containsAll(Collection paramCollection) {
		return getActivities().containsAll(paramCollection);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean addAll(Collection paramCollection) {
		return getActivities().addAll(paramCollection);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean removeAll(Collection paramCollection) {
		return getActivities().removeAll(paramCollection);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean retainAll(Collection paramCollection) {
		return getActivities().retainAll(paramCollection);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void clear() {
		getActivities().clear();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object paramObject) {
		return getActivities().equals(paramObject);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return getActivities().hashCode();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy