    001// Copyright 2006, 2007, 2010 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import static org.apache.tapestry5.commons.util.CollectionFactory.newStack;
    018
    019import java.util.List;
    020
    021import org.apache.tapestry5.commons.util.CollectionFactory;
    022import org.apache.tapestry5.commons.util.Stack;
    023import org.apache.tapestry5.services.Heartbeat;
    024
    025public class HeartbeatImpl implements Heartbeat
    026{
    027    private final Stack<List<Runnable>> stack = newStack();
    028
    029    public void begin()
    030    {
    031        List<Runnable> beat = CollectionFactory.newList();
    032
    033        stack.push(beat);
    034    }
    035
    036    public void defer(Runnable command)
    037    {
    038        assert command != null;
    039
    040        stack.peek().add(command);
    041
    042    }
    043
    044    public void end()
    045    {
    046        List<Runnable> beat = stack.pop();
    047
    048        for (Runnable r : beat)
    049            r.run();
    050    }
    051
    052}