11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.eventbus; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.InvocationTargetException; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.Method; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 230888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Wraps a single-argument subscriber method on a specific object, and ensures 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that only one thread may enter the method at a time. 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Beyond synchronization, this class behaves identically to 270888a09821a98ac0680fad765217302858e70fa4Paul Duffin * {@link EventSubscriber}. 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Cliff Biffle 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 310888a09821a98ac0680fad765217302858e70fa4Paul Duffinfinal class SynchronizedEventSubscriber extends EventSubscriber { 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 330888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Creates a new SynchronizedEventSubscriber to wrap {@code method} on 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code target}. 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param target object to which the method applies. 370888a09821a98ac0680fad765217302858e70fa4Paul Duffin * @param method subscriber method. 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 390888a09821a98ac0680fad765217302858e70fa4Paul Duffin public SynchronizedEventSubscriber(Object target, Method method) { 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super(target, method); 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 437dd252788645e940eada959bdde927426e2531c9Paul Duffin @Override 440888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void handleEvent(Object event) throws InvocationTargetException { 450888a09821a98ac0680fad765217302858e70fa4Paul Duffin // https://code.google.com/p/guava-libraries/issues/detail?id=1403 460888a09821a98ac0680fad765217302858e70fa4Paul Duffin synchronized (this) { 470888a09821a98ac0680fad765217302858e70fa4Paul Duffin super.handleEvent(event); 480888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 51