All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.web.bindery.event.shared.testing.CountingEventBus Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2011 Google Inc.
 * 
 * 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 com.google.web.bindery.event.shared.testing;

import com.google.web.bindery.event.shared.Event;
import com.google.web.bindery.event.shared.Event.Type;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.google.web.bindery.event.shared.SimpleEventBus;

import java.util.HashMap;
import java.util.Map;

/**
 * Wraps an {@link EventBus} to keep a count of registered handlers. Handy for
 * tests.
 */
public class CountingEventBus extends EventBus {
  private final Map, Integer> counts = new HashMap, Integer>();
  private final EventBus wrapped;

  public CountingEventBus() {
    this(new SimpleEventBus());
  }

  public CountingEventBus(EventBus wrapped) {
    this.wrapped = wrapped;
  }

  @Override
  public  HandlerRegistration addHandler(Type type, H handler) {
    increment(type);
    final HandlerRegistration superReg = wrapped.addHandler(type, handler);
    return makeReg(type, superReg);
  }

  @Override
  public  HandlerRegistration addHandlerToSource(final Type type, Object source, H handler) {
    increment(type);
    final HandlerRegistration superReg = wrapped.addHandlerToSource(type, source, handler);
    return makeReg(type, superReg);
  }

  @Override
  public void fireEvent(Event event) {
    wrapped.fireEvent(event);
  }

  @Override
  public void fireEventFromSource(Event event, Object source) {
    wrapped.fireEventFromSource(event, source);
  }

  public int getCount(Type type) {
    Integer count = counts.get(type);
    return count == null ? 0 : count;
  }

  private void decrement(Type type) {
    counts.put(type, getCount(type) - 1);
  }

  private  void increment(final Type type) {
    counts.put(type, getCount(type) + 1);
  }

  private  HandlerRegistration makeReg(final Type type, final HandlerRegistration superReg) {
    return new HandlerRegistration() {
      public void removeHandler() {
        decrement(type);
        superReg.removeHandler();
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy