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.CollectionAddedToEvent; 027import org.apache.isis.applib.services.eventbus.EventBusService; 028import org.apache.isis.applib.services.eventbus.PropertyChangedEvent; 029import org.apache.isis.core.metamodel.facetapi.MultiTypedFacet; 030import org.apache.isis.core.metamodel.facets.SingleValueFacet; 031import org.apache.isis.core.metamodel.facets.collections.modify.CollectionAddToFacet; 032 033/** 034 * Indicates that (the specified subclass of) {@link CollectionAddedToEvent} should be posted to the 035 * {@link EventBusService}. 036 */ 037public interface PostsCollectionAddedToEventFacet extends SingleValueFacet<Class<? extends CollectionAddedToEvent<?,?>>>, CollectionAddToFacet, MultiTypedFacet { 038 039 public static class Util { 040 private Util(){} 041 042 @SuppressWarnings("unchecked") 043 public static <S, T> CollectionAddedToEvent<S, T> newEvent( 044 final Class<? extends CollectionAddedToEvent<S, T>> type, 045 final S source, 046 final Identifier identifier, 047 final T value) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 048 final Constructor<?>[] constructors = type.getConstructors(); 049 for (final Constructor<?> constructor : constructors) { 050 final Class<?>[] parameterTypes = constructor.getParameterTypes(); 051 if(parameterTypes.length != 3) { 052 continue; 053 } 054 if(!parameterTypes[0].isAssignableFrom(source.getClass())) { 055 continue; 056 } 057 if(!parameterTypes[1].isAssignableFrom(Identifier.class)) { 058 continue; 059 } 060 if(value != null && !parameterTypes[2].isAssignableFrom(value.getClass())) { 061 continue; 062 } 063 final Object event = constructor.newInstance(source, identifier, value); 064 return (CollectionAddedToEvent<S, T>) event; 065 } 066 throw new NoSuchMethodException(type.getName()+".<init>(? super " + source.getClass().getName() + ", " + Identifier.class.getName() + ", java.lang.Object)"); 067 } 068 069 } 070} 071