001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.metamodel.facets.collections.event; 021 022import java.lang.reflect.Constructor; 023import java.lang.reflect.InvocationTargetException; 024 025import org.apache.isis.applib.Identifier; 026import org.apache.isis.applib.services.eventbus.CollectionRemovedFromEvent; 027import org.apache.isis.applib.services.eventbus.EventBusService; 028import org.apache.isis.core.metamodel.facetapi.MultiTypedFacet; 029import org.apache.isis.core.metamodel.facets.SingleValueFacet; 030import org.apache.isis.core.metamodel.facets.collections.modify.CollectionRemoveFromFacet; 031 032/** 033 * Indicates that (the specified subclass of) {@link CollectionRemovedFromEvent} should be posted to the 034 * {@link EventBusService}. 035 */ 036public interface PostsCollectionRemovedFromEventFacet extends SingleValueFacet<Class<? extends CollectionRemovedFromEvent<?,?>>>, CollectionRemoveFromFacet, MultiTypedFacet { 037 038 039 public static class Util { 040 private Util(){} 041 042 @SuppressWarnings("unchecked") 043 public static <S, T> CollectionRemovedFromEvent<S, T> newEvent( 044 final Class<? extends CollectionRemovedFromEvent<S, T>> type, 045 final S source, 046 final Identifier identifier, 047 final T value) 048 throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { 049 final Constructor<?>[] constructors = type.getConstructors(); 050 for (final Constructor<?> constructor : constructors) { 051 final Class<?>[] parameterTypes = constructor.getParameterTypes(); 052 if(parameterTypes.length != 3) { 053 continue; 054 } 055 if(!parameterTypes[0].isAssignableFrom(source.getClass())) { 056 continue; 057 } 058 if(!parameterTypes[1].isAssignableFrom(Identifier.class)) { 059 continue; 060 } 061 if(value != null && !parameterTypes[2].isAssignableFrom(value.getClass())) { 062 continue; 063 } 064 final Object event = constructor.newInstance(source, identifier, value); 065 return (CollectionRemovedFromEvent<S, T>) event; 066 } 067 throw new NoSuchMethodException(type.getName()+".<init>(? super " + source.getClass().getName() + ", " + Identifier.class.getName() + ", java.lang.Object)"); 068 } 069 } 070 071} 072