/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * $Header:$
 */
package service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Implementation of service provided by the EventRecorderServiceProvider service provider.
 * This service provides a simple mechanism to log control events.
 */
public class EventRecorderService implements Serializable {

    private static final long serialVersionUID = 6527468764100208475L;
    private LinkedList<String> _events = new LinkedList<String>();

    /**
     * Enum for event types.
     */
    public enum EventType {
        ON_CREATE("onCreate"),
        ON_ACQUIRE("onAcquire"),
        ON_RELEASE("onRelease");

        private final String _text;

        EventType(String text) {
            _text = text;
        }

        public String toString() {
            return _text;
        }
    }

    /**
     * Log an event with a specified type.
     *
     * @param source Event source.
     * @param type   Event type.
     */
    public void logEvent(Object source, EventType type) {
        StringBuilder sb = new StringBuilder();
        sb.append(getTimeStamp());
        sb.append(": ");
        sb.append(source.getClass().getSimpleName());
        sb.append(":");
        sb.append(type.toString());
        _events.add(sb.toString());
    }

    /**
     * Log a user event.
     *
     * @param source Event source.
     * @param msg    Message to log.
     */
    public void logUserEvent(Object source, String msg) {
        StringBuilder sb = new StringBuilder();
        sb.append(getTimeStamp());
        sb.append(": ");
        sb.append(source.getClass().getSimpleName());
        sb.append(":");
        sb.append(msg);
        _events.add(sb.toString());
    }

    /**
     * Get an Iterator for all events logged.
     *
     * @return Iterator.
     */
    public Iterator<String> getEvents() {
        return (Iterator<String>) _events.iterator();
    }

    /**
     * Generate a time stamp string.
     *
     * @return String.
     */
    private String getTimeStamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
        return sdf.format(new Date());
    }
}
