PageRenderTime 36ms CodeModel.GetById 20ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-utils/src/main/java/org/hudsonci/utils/id/GUID.java

http://github.com/hudson/hudson
Java | 86 lines | 43 code | 14 blank | 29 comment | 6 complexity | 3034e670ce58b5ee1319e684415b8085 MD5 | raw file
 1/**
 2 * The MIT License
 3 *
 4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
 5 *
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25package org.hudsonci.utils.id;
26
27import com.thoughtworks.xstream.annotations.XStreamAlias;
28import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
29
30import java.util.UUID;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Globally Unique IDentifier; backed by {@link UUID}.
36 *
37 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
38 * @since 2.1.0
39 */
40@XStreamAlias("guid")
41public class GUID
42    implements ID
43{
44    @XStreamAsAttribute
45    private final UUID value;
46
47    public GUID(final UUID value) {
48        this.value = checkNotNull(value);
49    }
50
51    public GUID(final String value) {
52        this(UUID.fromString(value));
53    }
54
55    public GUID() {
56        this(UUID.randomUUID());
57    }
58
59    public UUID getValue() {
60        return value;
61    }
62
63    @Override
64    public boolean equals(final Object obj) {
65        if (this == obj) {
66            return true;
67        }
68        if (!(obj instanceof GUID)) {
69            return false;
70        }
71
72        GUID that = (GUID) obj;
73
74        return !(value != null ? !value.equals(that.value) : that.value != null);
75    }
76
77    @Override
78    public int hashCode() {
79        return value != null ? value.hashCode() : 0;
80    }
81
82    @Override
83    public String toString() {
84        return value.toString();
85    }
86}