文档章节

UUID.java-J2ME 中javax.bluetooth里的UUID生成类

Donald Yang
 Donald Yang
发布于 2016/06/20 19:13
字数 817
阅读 8
收藏 0

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

 

/*
 *
 *
 * Portions Copyright  2000-2007 Sun Microsystems, Inc. All Rights
 * Reserved.  Use is subject to license terms.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */

/*
 * (c) Copyright 2001, 2002 Motorola, Inc.  ALL RIGHTS RESERVED.
 */
package javax.bluetooth;

/**
 * This class is defined by the JSR-82 specification
 * <em>Java™ APIs for Bluetooth™ Wireless Technology,
 * Version 1.1.</em>
 */
// JAVADOC COMMENT ELIDED 
public class UUID {

    // JAVADOC COMMENT ELIDED
    private long highBits;

    // JAVADOC COMMENT ELIDED
    private long lowBits;

    // JAVADOC COMMENT ELIDED
    private static final long BASE_UUID_HIGHT = 0x1000L;
    
    // JAVADOC COMMENT ELIDED
    private static final long BASE_UUID_LOW = 0x800000805F9B34FBL;

    // JAVADOC COMMENT ELIDED
    private static final char[] digits = {
        '0', '1', '2', '3', '4', '5',
        '6', '7', '8', '9', 'A', 'B',
        'C', 'D', 'E', 'F'
    };

    // JAVADOC COMMENT ELIDED 
    public UUID(long uuidValue) {

        // check the specified value is out of range
        if (uuidValue < 0 || uuidValue > 0xffffffffL) {
            throw new IllegalArgumentException(
                    "The 'uuidValue' is out of [0, 2^32 - 1] range: "
                    + uuidValue);
        }

        /*
         * Create a UUID from 16/32 bits value.
         *
         * 128_bit_value = 16_bit_value * 2^96 + Bluetooth_Base_UUID
         * 128_bit_value = 32_bit_value * 2^96 + Bluetooth_Base_UUID
         *
         * No need to check the "overflow/negative", because
         * uuidValue is 32 bits & BASE_UUID_HIGHT is 16 bits.
         */
        highBits = (uuidValue << 32) | BASE_UUID_HIGHT;
        lowBits = BASE_UUID_LOW;
    }

    // JAVADOC COMMENT ELIDED 
    public UUID(String uuidValue, boolean shortUUID) {
        if (uuidValue == null) {
            throw new NullPointerException("Specified 'uuidValue' is null");
        }

        /*
         * The zero length is double checked by the parsing operation,
         * but the NumberFormatException is thrown in that case -
         * we need IllegalArgumentException according to spec.
         */
        if (uuidValue.length() == 0 || (shortUUID && uuidValue.length() > 8) ||
                uuidValue.length() > 32) {
            throw new IllegalArgumentException(
                    "Invalid length of specified 'uuidValue': "
                    + uuidValue.length());
        }

        // check if sign character presents
        if (uuidValue.indexOf('-') != -1) {
            throw new NumberFormatException(
                    "The '-' character is not allowed: " + uuidValue);
        }

        /*
         * 16-bit or 32-bit UUID case.
         */
        if (shortUUID) {

            // this checks the format and may throw a NumberFormatException
            long val = Long.parseLong(uuidValue, 16);

            /*
             * create a UUID from 16/32 bits value.
             *
             * No need to check the "overflow/negative", because
             * lVal is 32 bits & BASE_UUID_HIGHT is 16 bits.
             */
            highBits = (val << 32) | BASE_UUID_HIGHT;
            lowBits = BASE_UUID_LOW;
            return;
        }
        
        /*
         * 128-bit UUID case.
         */
        highBits = 0x0L;

        // simple case (optimization)
        if (uuidValue.length() < 16) {
            lowBits = Long.parseLong(uuidValue, 16);
            return;
        }

        /*
         * We have to do a 32 bits parsing, because the
         * Long.parseLong("ffff ffff ffff ffff") does not
         * parse such an unsigned number.
         */
        int l = uuidValue.length();
        lowBits = Long.parseLong(uuidValue.substring(l - 8), 16);
        lowBits |= (Long.parseLong(uuidValue.substring(l - 16, l - 8), 16)
                << 32);

        if (l == 16) {
            return;
        }

        if (l <= 24) {
            highBits = Long.parseLong(uuidValue.substring(0, l - 16), 16);
        } else {
            highBits = Long.parseLong(uuidValue.substring(l - 24, l - 16), 16);
            highBits |= (Long.parseLong(uuidValue.substring(0, l - 24), 16)
                    << 32);
        }
    }

    // JAVADOC COMMENT ELIDED 
    public String toString() {

        /*
         * This implementation is taken from cldc1.1 Integer#toUnsignedString
         * one. The implementation which uses Integer#toHexString() is
         * 2-3 times slower, so such a code duplication is required here.
         */
        int[] ints = new int[] {
            (int) (lowBits & 0xffffffffL),
            (int) (lowBits >>> 32 & 0xffffffffL),
            (int) (highBits & 0xffffffffL),
            (int) (highBits >>> 32 & 0xffffffffL)
        };
        int charPos = 32;
        char[] buf = new char[charPos];
        int shift = 4;
        int mask = 0xf;
        int needZerosIndex = -1;

        /*
         * check with part of value requires the zero characters.
         *
         * I.e. the original algorithm gives as an 1 character
         * for the value '1', but we may want 00000001.
         */
        for (int i = 3; i >= 0; i--) {
            if (ints[i] != 0) {
                needZerosIndex = i - 1;
                break;
            }
        }

        /*
         * Process parts of UUID from low parts to high ones.
         */
        for (int i = 0; i < ints.length; i++) {

            /*
             * The 16 bits are zero & no need to fill with 0,
             * and it's not a UUID with value '0' (i != 0).
             */
            if (ints[i] == 0 && needZerosIndex < i && i != 0) {
                continue;
            }

            if (needZerosIndex >= i) {

                // fill all 8 characters (even if allof them are '0')
                for (int j = 0; j < 8; j++) {
                    buf[--charPos] = digits[ints[i] & mask];
                    ints[i] >>>= shift;
                }
            } else {

                // fill until the highest valuable character
                do {
                    buf[--charPos] = digits[ints[i] & mask];
                    ints[i] >>>= shift;
                } while (ints[i] != 0);
            }
        }
        return new String(buf, charPos, (32 - charPos));
    }

    // JAVADOC COMMENT ELIDED 
    public boolean equals(Object value) {
        return value instanceof UUID &&
                lowBits == ((UUID) value).lowBits &&
                highBits == ((UUID) value).highBits;
    }

    // JAVADOC COMMENT ELIDED 
    public int hashCode() {
        return (int) (highBits ^ highBits >> 32 ^ lowBits ^ lowBits >> 32);
    }
} // end of class 'UUID' definition

本文转载自:http://blog.csdn.net/ffyd2000/article/details/4871063

Donald Yang
粉丝 2
博文 56
码字总数 139
作品 0
成都
私信 提问
J2me游戏如何快速移植到Android (2)

三 开始移植 小白找到Android中对应的J2ME相关的替代类和替代方法后,开始噼里啪啦的改代码了。没过多久,首个俄罗斯方块算是移植成功。当他开始移植下一款游戏时,发现又要重复的改那些代码...

JavaGG
2011/05/03
1K
0
J2ME游戏移植到Android平台的方法

本文主要介绍如何把J2ME游戏移植到Android平台的方法,如果你是个J2ME的游戏开发者,并且想把一些J2ME游戏快速地迁移到Android平台,那么相信本文会对你有所帮助。当然,如果您非说可以安装个...

JavaGG
2011/05/03
1K
1
J2ME Polish 并不适合在商业软件中使用

J2ME Polish 提供了对j2me一整套的支持。比如支持自定义UI,支持国际化,通过build.xml生成对各个厂商的手机,减少了程序员发布程序要对不同品牌的支持的繁琐劳动。支持采用css定制UI,自定义...

绿悠悠
2009/12/16
1K
0
J2me游戏如何快速移植到Android(1)

前言 小白:“老大,你让做的三个J2me游戏搞定了,请看DEMO。” 老大:“恩,不错,小白,你知道Android这个平台吧?” 小白:“恩,听过。听说和J2ME有很多共同点。” 老大:“(一阵奸笑)...

JavaGG
2011/05/03
974
1
改善你的J2ME程序界面-使用开源UI库

改善你的J2ME程序界面-使用开源UI库 J2ME自带UI不是太美观,使用起来也不太方面,为了解决这问题,很多UI库应势而出。使用这些库,可以大大简化开发,提高效率。当然有好必有坏,使用 这些库...

迷糊
2009/10/11
2.9K
0

没有更多内容

加载失败,请刷新页面

加载更多

如何更改iOS应用程序的名称?

我前几天用一个愚蠢的开发代码名称开始了一个iPhone项目,现在我想改变项目的名称,因为它已经接近完成了。 但是我不知道如何使用Xcode来做这件事,尝试在info.plist文件中更改应用程序的名称...

技术盛宴
30分钟前
3
0
关于win10tensorflow的配置(CPU+GPU)

主要内容 CPU篇 GPU篇 【前期准备与注意事项】 环境:window1064位+python36(37)+CUDA9.0+cuDNN7.6+tensorflow_gpu-1.12.0 软件:anaconda+pycharm 硬件:有nvidia显卡的笔记本或台式(非A...

放只虎归个山
39分钟前
3
0
C#中的多行字符串文字

有没有一种简单的方法可以在C#中创建多行字符串文字? 这是我现在所拥有的: string query = "SELECT foo, bar"+ " FROM table"+ " WHERE id = 42"; 我知道PHP有 <<<BLOCKBLOCK; C#是......

javail
46分钟前
4
0
微信支付之小微商户扫盲!支持信用卡,免营业执照!

微信支付商户申请面向线下小微商户开放,符合条件的微信支付服务商可为小微商户发起接入申请。无需营业执照。 小微商户日收款额度为5万元~30万元 。 信用卡支付日限额为1千;月限额1万。 结算...

吴伟祥
今天
4
0
大话SDWebImage(三)-- 图片下载层

四、图片下载层 SDWebImageDownloader是处理图片下载的类 4.1 图片下载步骤 首先介绍下dispatch_barrier,GCD中的dispatch_barrier目的是在并发队列实现串行的效果,创建下载任务SDWebImageD...

aron1992
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部