1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.id.uuid.clock; 19 20 /** 21 * <p><code>SystemClockImpl</code> provides a timing mechanism for returning the 22 * current time in 100-nano second intervals since 00:00:00.00, 15 October 1582. 23 * </p> 24 * 25 * @see org.apache.commons.id.uuid.clock.Clock 26 * @author Commons-Id Team 27 * @version $Revision: 480488 $ $Date: 2006-11-29 08:57:26 +0000 (Wed, 29 Nov 2006) $ 28 */ 29 30 public final class SystemClockImpl implements Clock { 31 /** Counter for the number of calls during the current millisecond */ 32 private long generatedThisMilli = 0; 33 34 /** The current time in milliseconds held in this clock thread. */ 35 private long currentTimeMillis; 36 37 /** 38 * <p>Public constructor.</p> 39 */ 40 public SystemClockImpl() { 41 super(); 42 } 43 44 /** @see org.apache.commons.id.uuid.clock.Clock#getUUIDTime() */ 45 public long getUUIDTime() throws OverClockedException { 46 return getTimeSynchronized(); 47 } 48 49 /** 50 * <p>Object synchronized method returns the current time in 100ns 51 * intervals since the Gregorian change offset.</p> 52 * 53 * @return the current time in 100ns intervals since the Gregorian change 54 * offset. 55 * @throws OverClockedException an exception raised if too many timestamps 56 * have been generated for the system time interval. 57 */ 58 private synchronized long getTimeSynchronized() 59 throws OverClockedException { 60 if (currentTimeMillis != System.currentTimeMillis()) { 61 currentTimeMillis = System.currentTimeMillis(); 62 generatedThisMilli = 0; 63 } 64 // Set time as current time millis plus offset times 100 ns ticks 65 long currentTime = 66 (currentTimeMillis + GREGORIAN_CHANGE_OFFSET) 67 * INTERVALS_PER_MILLI; 68 69 // Return the same time - generator/client code must check to see if overclocked 70 if (generatedThisMilli + 1 >= INTERVALS_PER_MILLI) { 71 throw new OverClockedException(); 72 } 73 // Return the uuid time plus the artifical tick incremented 74 return (currentTime + generatedThisMilli++); 75 } 76 }