01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.framework.commandexecutors.java;
20:
21: import java.util.HashMap;
22: import java.util.List;
23: import java.util.Map;
24: import org.mactor.framework.MactorException;
25: import org.mactor.framework.ParseUtil;
26: import org.mactor.framework.TestContextImpl;
27: import org.mactor.framework.commandexecutors.ValueCommandExecutor;
28:
29: public class SequenceValueCommandExecutor implements
30: ValueCommandExecutor {
31: private static Map<String, Sequence> seqMap = new HashMap<String, Sequence>();
32: private String prefix = "";
33: private String name;
34: private static Object lock = new Object();
35:
36: public SequenceValueCommandExecutor(String command,
37: List<String> params) {
38: this .name = command;
39: Integer start = null;
40: if (params != null && params.size() > 0)
41: start = ParseUtil.tryParseInt(params.get(0));
42: long n = 0;
43: if (start != null)
44: n = start.longValue();
45: else
46: n = System.currentTimeMillis();
47: synchronized (lock) {
48: if (!seqMap.containsKey(name)) {
49: seqMap.put(name, new Sequence(n));
50: }
51: }
52: }
53:
54: public String extractValue(TestContextImpl context)
55: throws MactorException {
56: Sequence seq = null;
57: synchronized (lock) {
58: seq = seqMap.get(name);
59: }
60: return prefix + seq.next();
61: }
62:
63: private static class Sequence {
64: private long seq;
65:
66: public Sequence(long start) {
67: this .seq = start;
68: }
69:
70: public synchronized long next() {
71: return ++seq;
72: }
73: }
74: }
|