日期:2014-05-20  浏览次数:20761 次

优化一段循环,求解!
Java code

    public static void mediate(final Event event) {
        final Scope scope = event.getScope();
        final DecomposedRoiEntry[] values = registry.get(scope.getAreaLevelHash());
        if (values != null) {
            final HashSet<EventConsumer> consumers = new HashSet<EventConsumer>(values.length);
            for (int i = 0; i < values.length; i++) {
                final DecomposedRoiEntry value = values[i];
                if (value.hasInterest(scope)) {
                    final EventConsumer eventConsumer = value.getEventConsumer();
                    if (!consumers.contains(eventConsumer)) {
                        consumers.add(eventConsumer);
                        eventConsumer.consume(event);
                    }
                }
            }
        }
    }



------解决方案--------------------
挺清晰了, 如果没有上下文,这样已经OK了
------解决方案--------------------
帮你顶一下。
------解决方案--------------------
没有上下文,确实是可以了。不过如果为了好看的话可以这么写,我个人爱好。

Java code


public static void mediate(final Event event) {
        final Scope scope = event.getScope();
        final DecomposedRoiEntry[] values = registry.get(scope.getAreaLevelHash());
        if (values == null) 
            return ;
          final HashSet<EventConsumer> consumers = new HashSet<EventConsumer>(values.length);
            for (int i = 0; i < values.length; i++) {
                final DecomposedRoiEntry value = values[i];
                if (!value.hasInterest(scope) || consumers.contains(getEventConsumer(event)))
                      continue;
                consumers.add(eventConsumer);
                getEventConsumer(event).consume(event);
                      
             }
    }

  private static getEventConsumer(DecomposedRoiEntry value){
     return value.getEventConsumer();
}