Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.olivergondza.dumpling.model;
import groovy.lang.Closure;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.annotation.Nonnull;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import com.github.olivergondza.dumpling.query.SingleThreadSetQuery;
/**
* Collection of threads in certain {@link ProcessRuntime}.
*
* @author ogondza
* @see ProcessRuntime#getThreads()
*/
public class ThreadSet implements Iterable {
private static final @Nonnull String NL = System.getProperty("line.separator", "\n");
private final @Nonnull ProcessRuntime runtime;
private final @Nonnull Set threads;
public ThreadSet(@Nonnull ProcessRuntime runtime, @Nonnull Set threads) {
this.runtime = runtime;
this.threads = Collections.unmodifiableSet(threads);
}
/**
* Enclosing runtime.
*/
public @Nonnull ProcessRuntime getProcessRuntime() {
return runtime;
}
/**
* Extract the only thread from set.
*
* @throws IllegalStateException if not exactly one thread present.
*/
public @Nonnull ProcessThread onlyThread() throws IllegalStateException {
if (size() != 1) throw new IllegalStateException(
"Exactly one thread expected in the set. Found " + size()
);
return threads.iterator().next();
}
/**
* Get threads blocked by any of current threads.
*/
public @Nonnull ThreadSet getBlockedThreads() {
Set acquired = new HashSet();
for (ProcessThread thread: threads) {
acquired.addAll(thread.getAcquiredLocks());
}
Set blocked = new HashSet();
for (ProcessThread thread: runtime.getThreads()) {
if (acquired.contains(thread.getWaitingToLock())) {
blocked.add(thread);
}
}
return runtime.getThreadSet(blocked);
}
/**
* Get threads blocking any of current threads.
*/
public @Nonnull ThreadSet getBlockingThreads() {
Set waitingOn = new HashSet();
for (ProcessThread thread: threads) {
if (thread.getWaitingToLock() != null) {
waitingOn.add(thread.getWaitingToLock());
}
}
Set blocking = new HashSet();
for (ProcessThread thread: runtime.getThreads()) {
Set threadHolding = thread.getAcquiredLocks();
threadHolding.retainAll(waitingOn);
if (!threadHolding.isEmpty()) {
blocking.add(thread);
}
}
return runtime.getThreadSet(blocking);
}
public @Nonnull ThreadSet ignoring(@Nonnull ThreadSet ignoredThreads) {
if (threads.isEmpty() || ignoredThreads.isEmpty()) return this;
HashSet newThreads = new HashSet(threads);
newThreads.removeAll(ignoredThreads.threads);
return runtime.getThreadSet(newThreads);
}
/**
* Get subset of current threads.
*
* @param pred Predicate to match.
* @return {@link ThreadSet} scoped to current runtime containing subset of threads that match the predicate.
*/
public @Nonnull ThreadSet where(@Nonnull ProcessThread.Predicate pred) {
HashSet subset = new HashSet(size() / 2);
for (@Nonnull ProcessThread thread: threads) {
if (pred.isValid(thread)) subset.add(thread);
}
return runtime.getThreadSet(subset);
}
/**
* Run query using this as an initial thread set.
*/
public @Nonnull T query(@Nonnull SingleThreadSetQuery query) {
return query.query(this);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (ProcessThread thread: threads) {
stringBuilder.append(thread).append(NL + NL);
}
return stringBuilder.toString();
}
@Override
public boolean equals(Object rhs) {
if (rhs == null) return false;
if (this == rhs) return true;
if (!this.getClass().equals(rhs.getClass())) return false;
ThreadSet other = (ThreadSet) rhs;
return runtime.equals(other.runtime) && threads.equals(other.threads);
}
@Override
public int hashCode() {
return runtime.hashCode() + threads.hashCode() * 31;
}
public int size() {
return threads.size();
}
public boolean isEmpty() {
return threads.isEmpty();
}
public boolean contains(Object o) {
return threads.contains(o);
}
public boolean containsAll(Collection> c) {
return threads.containsAll(c);
}
@Override
public Iterator iterator() {
return threads.iterator();
}
/**
* Create derived set from this one.
*
* @return New thread collection bound to same runtime.
*/
public @Nonnull ThreadSet derive(Collection threads) {
return runtime.getThreadSet(threads);
}
// Groovy interop
public @Nonnull ThreadSet grep() {
// Do not invoke grep(Collection) as it was added in 2.0
return runtime.getThreadSet(DefaultGroovyMethods.grep((Object) threads));
}
public @Nonnull ThreadSet grep(Object filter) {
// Do not invoke grep(Collection, Object) as it was added in 2.0
return runtime.getThreadSet(DefaultGroovyMethods.grep((Object) threads, filter));
}
public @Nonnull ThreadSet findAll() {
return runtime.getThreadSet(DefaultGroovyMethods.findAll(threads));
}
public @Nonnull ThreadSet findAll(Closure predicate) {
return runtime.getThreadSet(DefaultGroovyMethods.findAll(threads, predicate));
}
public @Nonnull ThreadSet asImmutable() {
return this;
}
public @Nonnull ThreadSet toSet() {
return this;
}
public @Nonnull ThreadSet intersect(ThreadSet other) {
if (!runtime.equals(other.runtime)) throw new IllegalStateException(
"Unable to intersect thread sets bound to different runtime"
);
return runtime.getThreadSet(DefaultGroovyMethods.intersect(threads, other.threads));
}
public @Nonnull ThreadSet plus(ThreadSet other) {
if (!runtime.equals(other.runtime)) throw new IllegalStateException(
"Unable to merge thread sets bound to different runtime"
);
return runtime.getThreadSet(DefaultGroovyMethods.plus(threads, other.threads));
}
}