[1991] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * <p> |
---|
| 19 | * This is a simple id generator that uses the timestamps as id. If {@link #getNextId()} is called |
---|
| 20 | * twice in the same millisecond, than just the next millisecond is used. |
---|
| 21 | * </p> |
---|
| 22 | * |
---|
| 23 | * @author Patrick Harms |
---|
| 24 | */ |
---|
| 25 | public class SimpleIdGenerator implements IdGenerator { |
---|
| 26 | |
---|
| 27 | /** */ |
---|
| 28 | private long lastId; |
---|
| 29 | |
---|
| 30 | /* (non-Javadoc) |
---|
| 31 | * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#init() |
---|
| 32 | */ |
---|
| 33 | @Override |
---|
| 34 | public void init() throws IllegalStateException, HttpMonitorException { |
---|
| 35 | lastId = System.currentTimeMillis(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /* (non-Javadoc) |
---|
| 39 | * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#start() |
---|
| 40 | */ |
---|
| 41 | @Override |
---|
| 42 | public void start() throws IllegalStateException, HttpMonitorException { |
---|
| 43 | // nothing to do |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /* (non-Javadoc) |
---|
| 47 | * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#stop() |
---|
| 48 | */ |
---|
| 49 | @Override |
---|
| 50 | public void stop() { |
---|
| 51 | // nothing to do |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /* (non-Javadoc) |
---|
| 55 | * @see de.ugoe.cs.autoquest.httpmonitor.IdGenerator#getNextId() |
---|
| 56 | */ |
---|
| 57 | @Override |
---|
| 58 | public synchronized long getNextId() { |
---|
| 59 | long newId = System.currentTimeMillis(); |
---|
| 60 | |
---|
| 61 | if (newId <= lastId) { |
---|
| 62 | newId = lastId + 1; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | lastId = newId; |
---|
| 66 | |
---|
| 67 | return newId; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | } |
---|