001/**
002 * Copyright (C) 2012 FuseSource, Inc.
003 * http://fusesource.com
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.fusesource.hawtdispatch;
019
020import java.util.HashSet;
021import java.util.LinkedList;
022
023/**
024 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
025 */
026public class EventAggregators {
027
028    /**
029     * An EventAggregator that coalesces integer data obtained via calls to
030     * {@link CustomDispatchSource#merge(Object)}. Addition is used to coalesce the data.
031     */
032    public static final EventAggregator<Integer,Integer> INTEGER_ADD = new EventAggregator<Integer,Integer>() {
033        public Integer mergeEvent(Integer previous, Integer event) {
034            if( previous == null ) {
035                return event;
036            }
037            return previous + event;
038        }
039
040        public Integer mergeEvents(Integer previous, Integer events) {
041            return previous + events;
042        }
043    };
044
045    /**
046     * An EventAggregator that coalesces long data obtained via calls to
047     * {@link CustomDispatchSource#merge(Object)}. Addition is used to coalesce the data.
048     */
049    public static final EventAggregator<Long,Long> LONG_ADD = new EventAggregator<Long,Long>() {
050        public Long mergeEvent(Long previous, Long event) {
051            if( previous == null ) {
052                return event;
053            }
054            return previous + event;
055        }
056
057        public Long mergeEvents(Long previous, Long events) {
058            return previous + events;
059        }
060    };
061
062    /**
063     * An EventAggregator that coalesces integer data obtained via calls to
064     * {@link CustomDispatchSource#merge(Object)}. Bit-wise or is used to coalesce the data.
065     */
066    public static final EventAggregator<Integer,Integer> INTEGER_OR = new EventAggregator<Integer,Integer>() {
067        public Integer mergeEvent(Integer previous, Integer event) {
068            if( previous == null ) {
069                return event;
070            }
071            return previous | event;
072        }
073
074        public Integer mergeEvents(Integer previous, Integer events) {
075            return previous | events;
076        }
077    };
078
079    /**
080     * An EventAggregator that coalesces long data obtained via calls to
081     * {@link CustomDispatchSource#merge(Object)}. Bit-wise or is used to coalesce the data.
082     */
083    public static final EventAggregator<Long,Long> LONG_OR = new EventAggregator<Long,Long>() {
084        public Long mergeEvent(Long previous, Long event) {
085            if( previous == null ) {
086                return event;
087            }
088            return previous | event;
089        }
090
091        public Long mergeEvents(Long previous, Long events) {
092            return previous | events;
093        }
094    };
095
096
097    /**
098     * An EventAggregator that coalesces object data obtained via calls to
099     * {@link CustomDispatchSource#merge(Object)} into a linked list.
100     */
101    public static <T> EventAggregator<T, LinkedList<T>> linkedList(){
102        return new OrderedEventAggregator<T, LinkedList<T>>() {
103            public LinkedList<T> mergeEvent(LinkedList<T> previous, T event) {
104                if( previous == null ) {
105                    previous = new LinkedList<T>();
106                }
107                previous.add(event);
108                return previous;
109            }
110
111            public LinkedList<T> mergeEvents(LinkedList<T> previous, LinkedList<T> events) {
112                previous.addAll(events);
113                return previous;
114            }
115        };
116    }
117
118    /**
119     * An EventAggregator that coalesces object data obtained via calls to
120     * {@link CustomDispatchSource#merge(Object)} into a hash set.
121     */
122    public static <T> EventAggregator<T, HashSet<T>> hashSet(){
123        return new EventAggregator<T, HashSet<T>>() {
124            public HashSet<T> mergeEvent(HashSet<T> previous, T event) {
125                if( previous == null ) {
126                    previous = new HashSet<T>();
127}
128                previous.add(event);
129                return previous;
130            }
131
132            public HashSet<T> mergeEvents(HashSet<T> previous, HashSet<T> events) {
133                previous.addAll(events);
134                return previous;
135            }
136
137            public boolean ordered() {
138                return false;
139            }
140        };
141    }
142}