日期:2014-05-16 浏览次数:20639 次
目的:为了生成一个不重复的主键,主键生成通过客户端程序生成。
规则主要是根据机器的网络接口信息、线程信息、时间和随机数生成一个不会重复的主键。
详细类如下,改了下名字,根据自己理解加了些注释
?
?
?
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.nio.*;
import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;
/**
* 全局对象唯一标示符
* A globally unique identifier for objects.
* <p>Consists of 12 bytes, divided as follows:
* <blockquote><pre>
* <table border="1">
* <tr><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>
* <td>7</td><td>8</td><td>9</td><td>10</td><td>11</td></tr>
* <tr><td colspan="4">time</td><td colspan="3">machine</td>
* <td colspan="2">pid</td><td colspan="3">inc</td></tr>
* </table>
* </pre></blockquote>
*
* @dochub CreateGuIds
*/
public class CreateGuId implements Comparable<CreateGuId> , java.io.Serializable {
private static final long serialVersionUID = -4415279469780082174L;
static final Logger LOGGER = Logger.getLogger( "com.easy.todo.util" );
public static void main(String args[]){
System.out.println(CreateGuId.get());
}
/** 获取一个新的对象id
* Gets a new object id.
* @return the new id
*/
public static CreateGuId get(){
return new CreateGuId();
}
/** Checks if a string could be an <code>CreateGuId</code>.
* @return whether the string could be an object id
*/
public static boolean isValid( String s ){
if ( s == null )
return false;
final int len = s.length();
if ( len != 24 )
return false;
for ( int i=0; i<len; i++ ){
char c = s.charAt( i );
if ( c >= '0' && c <= '9' )
continue;
if ( c >= 'a' && c <= 'f' )
continue;
if ( c >= 'A' && c <= 'F' )
continue;
return false;
}
return true;
}
/** Turn an object into an <code>CreateGuId</code>, if possible.
* Strings will be converted into <code>CreateGuId</code>s, if possible, and <code>CreateGuId</code>s will
* be cast and returned. Passing in <code>null</code> returns <code>null</code>.
* @param o the object to convert
* @return an <code>CreateGuId</code> if it can be massaged, null otherwise
*/
public static CreateGuId massageToCreateGuId( Object o ){
if ( o == null )
return null;
if ( o instanceof CreateGuId )
return (CreateGuId)o;
if ( o instanceof String ){
String s = o.toString();
if ( isValid( s ) )
return new CreateGuId( s );
}
return null;
}
public CreateGuId( Date time ){
this(time, _genmachine, _nextInc.getAndIncrement());//原子操作加1
}
public CreateGuId( Date time , int inc ){
this( time , _genmachine , inc );
}
public CreateGuId( Date time , int machine , int inc ){