001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.core.event.impl;
020
021import java.rmi.dgc.VMID;
022import java.util.ArrayList;
023import java.util.HashSet;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.event.Event;
029import org.nuxeo.ecm.core.event.EventBundle;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class EventBundleImpl implements EventBundle {
035
036    private static final long serialVersionUID = 1L;
037
038    // not final to allow modification derived classes
039    protected VMID vmid;
040
041    protected final List<Event> events;
042
043    protected final Set<String> eventNames;
044
045    public EventBundleImpl(VMID sourceVMID) {
046        events = new ArrayList<Event>();
047        eventNames = new HashSet<String>();
048        vmid = sourceVMID;
049    }
050
051    public EventBundleImpl() {
052        this(EventServiceImpl.VMID);
053    }
054
055    @Override
056    public boolean hasRemoteSource() {
057        return !vmid.equals(EventServiceImpl.VMID);
058    }
059
060    @Override
061    public String getName() {
062        if (events.isEmpty()) {
063            return null;
064        }
065        return events.get(0).getContext().getRepositoryName();
066    }
067
068    @Override
069    public boolean isEmpty() {
070        return events.isEmpty();
071    }
072
073    @Override
074    public Event peek() {
075        return events.get(0);
076    }
077
078    @Override
079    public void push(Event event) {
080        events.add(event);
081        String eventName = event.getName();
082        if (eventName != null) {
083            eventNames.add(eventName);
084        }
085    }
086
087    @Override
088    public int size() {
089        return events.size();
090    }
091
092    @Override
093    public Iterator<Event> iterator() {
094        return events.iterator();
095    }
096
097    @Override
098    public VMID getSourceVMID() {
099        return vmid;
100    }
101
102    @Override
103    public boolean containsEventName(String eventName) {
104        if (eventName == null) {
105            return false;
106        }
107        return eventNames.contains(eventName);
108    }
109
110}