001/**
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.kernel.utils;
017
018import static com.google.common.base.Functions.forMap;
019import static com.google.common.collect.ImmutableMap.builder;
020import java.util.Map;
021
022import com.google.common.collect.ImmutableMap;
023
024/**
025 * A convenient abstraction over JCR's integer-typed events.
026 *
027 * @author ajs6f
028 * @since Feb 7, 2013
029 */
030public enum EventType {
031    NODE_ADDED(javax.jcr.observation.Event.NODE_ADDED, "node added"),
032    NODE_REMOVED(javax.jcr.observation.Event.NODE_REMOVED, "node removed"),
033    PROPERTY_ADDED(javax.jcr.observation.Event.PROPERTY_ADDED, "property added"),
034    PROPERTY_REMOVED(javax.jcr.observation.Event.PROPERTY_REMOVED, "property removed"),
035    PROPERTY_CHANGED(javax.jcr.observation.Event.PROPERTY_CHANGED, "property changed"),
036    NODE_MOVED(javax.jcr.observation.Event.NODE_MOVED, "node moved"),
037    PERSIST(javax.jcr.observation.Event.PERSIST, "persist");
038
039    private static final Map<Integer, EventType> translation;
040
041    private final Integer jcrEventType;
042
043    private final String eventName;
044
045
046    /*
047     * Create a translation map
048     */
049    static {
050        final ImmutableMap.Builder<Integer, EventType> b = builder();
051        for (final EventType eventType : values()) {
052            b.put(eventType.jcrEventType, eventType);
053        }
054        translation = b.build();
055    }
056
057    EventType(final Integer eventType, final String eventName) {
058        this.jcrEventType = eventType;
059        this.eventName = eventName;
060    }
061
062    /**
063     * @return a human-readable name for this event
064     */
065    public String getName() {
066        return this.eventName;
067    }
068
069    /**
070     * Get the Fedora event type for a JCR type
071     *
072     * @param i the integer value of a JCR type
073     * @return EventType
074     */
075    public static EventType valueOf(final Integer i) {
076        return forMap(translation).apply(i);
077    }
078}