org.gradle.api.internal.collections.DefaultCollectionEventRegister Maven / Gradle / Ivy
/*
* Copyright 2017 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.gradle.api.internal.collections;
import org.gradle.api.Action;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.internal.Cast;
import org.gradle.internal.ImmutableActionSet;
import java.util.HashSet;
import java.util.Set;
public class DefaultCollectionEventRegister implements CollectionEventRegister {
private final Class baseType;
private final CollectionCallbackActionDecorator decorator;
private ImmutableActionSet addActions = ImmutableActionSet.empty();
private ImmutableActionSet removeActions = ImmutableActionSet.empty();
private boolean baseTypeSubscribed;
private Set> subscribedTypes;
public DefaultCollectionEventRegister(Class baseType, CollectionCallbackActionDecorator decorator) {
this.baseType = baseType;
this.decorator = decorator;
}
@Override
public boolean isSubscribed(Class type) {
if (baseTypeSubscribed) {
return true;
}
if (subscribedTypes != null) {
if (type == null) {
return true;
}
for (Class subscribedType : subscribedTypes) {
if (subscribedType.isAssignableFrom(type)) {
return true;
}
}
}
return false;
}
@Override
public CollectionCallbackActionDecorator getDecorator() {
return decorator;
}
@Override
public ImmutableActionSet getAddActions() {
return addActions;
}
@Override
public void fireObjectAdded(T element) {
addActions.execute(element);
}
@Override
public void fireObjectRemoved(T element) {
removeActions.execute(element);
}
@Override
public Action registerEagerAddAction(Class type, Action addAction) {
return registerEagerAddDecoratedAction(type, addAction);
}
@Override
public Action registerLazyAddAction(Action addAction) {
return registerLazyAddDecoratedAction(addAction);
}
@Override
public void registerRemoveAction(Class type, Action removeAction) {
registerRemoveDecoratedAction(removeAction);
}
private Action registerEagerAddDecoratedAction(Class type, Action decorated) {
subscribe(type);
return registerLazyAddDecoratedAction(decorated);
}
private Action registerLazyAddDecoratedAction(Action decorated) {
addActions = addActions.add(decorated);
return decorated;
}
private void registerRemoveDecoratedAction(Action decorated) {
removeActions = removeActions.add(decorated);
}
@Override
public CollectionEventRegister filtered(CollectionFilter filter) {
return new FilteredEventRegister(filter, this);
}
private void subscribe(Class type) {
if (baseTypeSubscribed) {
return;
}
if (type.equals(baseType)) {
baseTypeSubscribed = true;
subscribedTypes = null;
} else {
if (subscribedTypes == null) {
subscribedTypes = new HashSet>();
}
subscribedTypes.add(type);
}
}
private class FilteredEventRegister implements CollectionEventRegister {
private final CollectionFilter filter;
private final CollectionEventRegister delegate;
FilteredEventRegister(CollectionFilter filter, CollectionEventRegister delegate) {
this.filter = filter;
this.delegate = Cast.uncheckedCast(delegate);
}
@Override
public CollectionCallbackActionDecorator getDecorator() {
return delegate.getDecorator();
}
@Override
public ImmutableActionSet getAddActions() {
throw new UnsupportedOperationException();
}
@Override
public void fireObjectAdded(S element) {
throw new UnsupportedOperationException();
}
@Override
public void fireObjectRemoved(S element) {
throw new UnsupportedOperationException();
}
@Override
public boolean isSubscribed(Class type) {
throw new UnsupportedOperationException();
}
@Override
public Action registerEagerAddAction(Class type, Action addAction) {
return delegate.registerEagerAddAction(type, filter.filtered(addAction));
}
@Override
public Action registerLazyAddAction(Action addAction) {
return delegate.registerLazyAddAction(filter.filtered(addAction));
}
@Override
public void registerRemoveAction(Class type, Action removeAction) {
delegate.registerRemoveAction(type, filter.filtered(removeAction));
}
@Override
public CollectionEventRegister filtered(CollectionFilter filter) {
return new FilteredEventRegister(filter, Cast.uncheckedNonnullCast(this));
}
}
}