001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.framework.commandexecutors.java;
021    
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.mactor.framework.MactorException;
027    import org.mactor.framework.ParseUtil;
028    import org.mactor.framework.TestContextImpl;
029    import org.mactor.framework.commandexecutors.ValueCommandExecutor;
030    
031    public class SequenceValueCommandExecutor implements ValueCommandExecutor {
032            private static Map<String, Sequence> seqMap = new HashMap<String, Sequence>();
033            private String prefix = "";
034            private String name;
035            private static Object lock = new Object();
036            public SequenceValueCommandExecutor(String command, List<String> params) {
037                    this.name = command;
038                    Integer start = null;
039                    if (params != null && params.size() > 0)
040                            start = ParseUtil.tryParseInt(params.get(0));
041                    long n = 0;
042                    if (start != null)
043                            n = start.longValue();
044                    else
045                            n = System.currentTimeMillis();
046                    synchronized (lock) {
047                            if (!seqMap.containsKey(name)) {
048                                    seqMap.put(name, new Sequence(n));
049                            }
050                    }
051            }
052            public String extractValue(TestContextImpl context) throws MactorException {
053                    Sequence seq = null;
054                    synchronized (lock) {
055                            seq = seqMap.get(name);
056                    }
057                    return prefix + seq.next();
058            }
059            private static class Sequence {
060                    private long seq;
061                    public Sequence(long start) {
062                            this.seq = start;
063                    }
064                    public synchronized long next() {
065                            return ++seq;
066                    }
067            }
068    }